Commit fa39df7d authored by Mauro E. Bender's avatar Mauro E. Bender

Close #6 - Destroy a resource item

parent 046e086b
......@@ -99,6 +99,15 @@ extension Resource {
}
}
public func destroy( id: AnyObject, parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, completion: ( ( error: ApiError? ) -> Void ) )
{
delete( "\(id)", parameters: parameters, headers: headers ) { response in
self.handleEmptyResult( response, handler: completion )
}
}
// MARK: UTILS
private func handleItemResult<T: JSONSerializable>( response: Response<AnyObject, NSError>,
handler: ( T?, error: ApiError? ) -> Void )
......@@ -125,6 +134,16 @@ extension Resource {
}
}
private func handleEmptyResult( response: Response<AnyObject, NSError>, handler: ( error: ApiError? ) -> Void )
{
switch(response.result) {
case .Success(let JSON):
handler( error: nil )
case .Failure(let error):
handler( error: handleError( response ) )
}
}
private func handleError( response: Response<AnyObject, NSError> ) -> ApiError {
let error = response.result.error!
......
......@@ -90,20 +90,33 @@ class ResourceTests: QuickSpec {
}
}
describe( "create" ) {
describe( "list" ) {
context( "when the request is successful" ) {
let itemToCreate = Item( id: 0, name: "Item 2" )
it( "should call the correct endpoint and return the created item" ) {
it( "should call the correct endpoint and return the item" ) {
waitUntil { done in
resource.create( itemToCreate ) { ( item: Item?, error: ApiError? ) in
expect( item?.id ) == 2
expect( item?.name ) == "Item 2"
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( "destroy" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return the created item" ) {
waitUntil { done in
resource.destroy( 1 ) { ( error: ApiError? ) in
done()
}
}
}
}
context( "when there's an error" ) {
let failResource : Resource = api.resource( "failedItems" )
......
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