Commit 981e1391 authored by Mauro E. Bender's avatar Mauro E. Bender

Close #8 - Edit resource item

parent d9af1e47
......@@ -16,7 +16,7 @@ public protocol JSONSerializable {
public class Resource {
public let api: Api
public let path: String
public var responseKeyPath: String? = "response"
public var responseKeyPath: String? = "response"
public var paginationKeyPath: String? = "pagination"
init( api: Api, path: String ) {
......@@ -57,6 +57,13 @@ extension Resource {
headers: headers, completion: completion)
}
public func patch( path: String? = nil, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil, completion: Response<AnyObject, NSError> -> Void )
{
return request( .PATCH, path: path, parameters: parameters, encoding: encoding,
headers: headers, completion: completion)
}
public func delete( path: String? = nil, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL,
headers: [String: String]? = nil, completion: Response<AnyObject, NSError> -> Void )
{
......@@ -104,10 +111,21 @@ extension Resource {
}
}
public func edit<T: JSONSerializable>( id: AnyObject, item: T, parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, encoding: ParameterEncoding = .JSON,
completion: ( ( ApiResult< T? > ) -> Void ) )
{
var requestParameters = item.toJSON()
if parameters != nil { requestParameters += parameters! }
patch( "\(id)", parameters: requestParameters, encoding: encoding, headers: headers ) { response in
completion( itemResult( response, keyPath: self.responseKeyPath ) )
}
}
public func destroy( id: AnyObject, parameters: [ String: String ]? = nil,
headers: [ String: String]? = nil, completion: ( ( EmptyResult ) -> Void ) )
{
ApiResult.Success()
delete( "\(id)", parameters: parameters, headers: headers ) { response in
completion( emptyResult( response ) )
}
......
......@@ -60,7 +60,7 @@ func createItemWithIDFromRequest( id: Int, request: NSURLRequest ) -> NSData? {
guard json != nil else { return nil }
json![ "id" ] = id
return try? NSJSONSerialization.dataWithJSONObject( json!, options: .PrettyPrinted )
return try? NSJSONSerialization.dataWithJSONObject( [ "response": json! ], options: .PrettyPrinted )
}
func jsonFromRequest( request: NSURLRequest ) -> [ String: AnyObject ]? {
......
......@@ -20,6 +20,7 @@ class ResourceTests: QuickSpec {
stub( matchApiPath( "items/1/components/1" ), builder: json( [ "response": [ "id": 1, "name": "Component 1" ] ] ) )
stub( matchApiPath( "items", method: .POST ), builder: buildItemFromRequest( 2 ) )
stub( matchApiPath( "items/1", method: .DELETE ), builder: http( 200 ) )
stub( matchApiPath( "items/1", method: .PATCH ), builder: buildItemFromRequest( 1 ) )
stub( matchApiPath( "items/1", method: .PUT ), builder: http( 200 ) )
stub( matchApiPath( "failedItems", method: .POST ), builder: http( 422 ) )
stub( matchApiPath( "items" ), builder: json( [ "response": [ [ "id": 1, "name": "Item 1" ], [ "id": 2, "name": "Item 2" ] ] ] ) )
......@@ -122,11 +123,20 @@ class ResourceTests: QuickSpec {
}
}
describe( "destroy" ) {
describe( "create" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return an empty result" ) {
it( "should call the correct endpoint and return the created item" ) {
waitUntil { done in
resource.destroy( 1 ) { ( emptyResult: EmptyResult ) in
let item = Item(id: 0, name: "Item 2")
resource.create( item ) { ( result: ApiResult<Item?> ) in
switch result {
case .Success( let item ):
expect( item?.id ) == 2
expect( item?.name ) == "Item 2"
case .Failure(_):
fail()
}
done()
}
}
......@@ -152,6 +162,39 @@ class ResourceTests: QuickSpec {
}
}
}
describe( "edit" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return the edited item" ) {
waitUntil { done in
let item = Item(id: 1, name: "Other name")
resource.edit( 1, item: item ) { ( result: ApiResult<Item?> ) in
switch result {
case .Success( let item ):
expect( item?.id ) == 1
expect( item?.name ) == "Other name"
case .Failure(_):
fail()
}
done()
}
}
}
}
}
describe( "destroy" ) {
context( "when the request is successful" ) {
it( "should call the correct endpoint and return an empty result" ) {
waitUntil { done in
resource.destroy( 1 ) { ( emptyResult: EmptyResult ) in
done()
}
}
}
}
}
}
describe( "nested resources methods" ) {
......
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