Commit 163aef24 authored by Ezequiel R. Aguerre's avatar Ezequiel R. Aguerre

Merge branch '5-mb-list_resource_items' into 'master'

List resource items

Closes #5

See merge request !5
parents 3d853ac6 046e086b
......@@ -27,30 +27,6 @@ public class Resource {
self.api = api
self.path = path
}
// MARK: UTILS
private func handleItemResult<T: JSONSerializable>( response: Response<AnyObject, NSError>,
handler: ( T?, error: ApiError? ) -> Void )
{
switch(response.result) {
case .Success(let JSON):
handler( T( fromJSON: JSON as! [String : AnyObject] ), error: nil )
case .Failure(let error):
handler( nil, error: handleError( response ) )
}
}
private func handleError( response: Response<AnyObject, NSError> ) -> ApiError {
let error = response.result.error!
if let statusCode = response.response?.statusCode where !(200...299).contains( statusCode ) {
return .Network( error: error, response: response.response! )
} else if error.code == -6006 {
return .JSONSerialization( error: error )
}
return .Unknown( error: error )
}
}
// MARK: REQUESTS
......@@ -103,6 +79,14 @@ extension Resource {
}
}
public func list<T: JSONSerializable>( parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, completion: ( ( [ T ]?, error: ApiError? ) -> Void ) )
{
get( parameters: parameters, headers: headers ) { response in
self.handleListResult( response, handler: completion )
}
}
public func create<T: JSONSerializable>( item: T, parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, encoding: ParameterEncoding = .JSON,
completion: ( ( T?, error: ApiError? ) -> Void ) )
......@@ -114,6 +98,44 @@ extension Resource {
self.handleItemResult( response, handler: completion )
}
}
// MARK: UTILS
private func handleItemResult<T: JSONSerializable>( response: Response<AnyObject, NSError>,
handler: ( T?, error: ApiError? ) -> Void )
{
switch(response.result) {
case .Success(let JSON):
handler( T( fromJSON: JSON as! [String : AnyObject] ), error: nil )
case .Failure(let error):
handler( nil, error: handleError( response ) )
}
}
private func handleListResult<T: JSONSerializable>( response: Response<AnyObject, NSError>,
handler: ( [ T ]?, error: ApiError? ) -> Void )
{
switch(response.result) {
case .Success(let JSON):
if let JSONList = JSON as? [ [String: AnyObject] ] {
let list = JSONList.map { T( fromJSON: $0 ) }
handler( list, error: nil )
}
case .Failure(let error):
handler( nil, error: handleError( response ) )
}
}
private func handleError( response: Response<AnyObject, NSError> ) -> ApiError {
let error = response.result.error!
if let statusCode = response.response?.statusCode where !(200...299).contains( statusCode ) {
return .Network( error: error, response: response.response! )
} else if error.code == -6006 {
return .JSONSerialization( error: error )
}
return .Unknown( error: error )
}
}
// MARK: NESTED RESOURCES
......
......@@ -22,6 +22,7 @@ class ResourceTests: QuickSpec {
stub( matchApiPath( "items/1", method: .DELETE ), builder: http( 200 ) )
stub( matchApiPath( "items/1", method: .PUT ), builder: http( 200 ) )
stub( matchApiPath( "failedItems", method: .POST ), builder: http( 422 ) )
stub( matchApiPath( "items" ), builder: json( [ [ "id": 1, "name": "Item 1" ], [ "id": 2, "name": "Item 2" ] ] ) )
}
override func spec() {
......@@ -72,6 +73,23 @@ class ResourceTests: QuickSpec {
}
}
describe( "list" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return the item" ) {
waitUntil { done in
resource.list() { ( items: [ Item ]?, error: ApiError? ) in
expect( items?[0].id ) == 1
expect( items?[0].name ) == "Item 1"
expect( items?[1].id ) == 2
expect( items?[1].name ) == "Item 2"
done()
}
}
}
}
}
describe( "create" ) {
context( "when the request is successful" ) {
let itemToCreate = Item( id: 0, name: "Item 2" )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment