Skip to content

Commit

Permalink
added support for mapping array responses
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanhimmelman committed May 10, 2015
1 parent f583be1 commit 9f47677
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
60 changes: 50 additions & 10 deletions AlamofireObjectMapper/AlmofireObjectMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,8 @@ extension Request {
:returns: The request.
*/
public func responseObject<T: Mappable>(completionHandler: (T?, NSError?) -> Void) -> Self {
return response(queue: nil, serializer: Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)) { (request, response, object, error) -> Void in

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

let parsedObject = Mapper<T>().map(object)

dispatch_async(dispatch_get_main_queue()) {
completionHandler(parsedObject, error)
}
}
return responseObject(nil) { (request, response, object, data, error) -> Void in
completionHandler(object, error)
}
}

Expand Down Expand Up @@ -65,6 +57,54 @@ extension Request {
}
}
}
}

// MARK: Array responses

/**
Adds a handler to be called once the request has finished.

:param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 2 arguments: the response array (of type Mappable) and any error produced while making the request

:returns: The request.
*/
public func responseArray<T: Mappable>(completionHandler: ([T]?, NSError?) -> Void) -> Self {
return responseArray(nil) { (request, response, object, data, error) -> Void in
completionHandler(object, error)
}
}

/**
Adds a handler to be called once the request has finished.

:param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 5 arguments: the URL request, the URL response, the response array (of type Mappable), the raw response data, and any error produced making the request.

:returns: The request.
*/
public func responseArray<T: Mappable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, AnyObject?, NSError?) -> Void) -> Self {
return responseArray(nil, completionHandler: completionHandler)
}

/**
Adds a handler to be called once the request has finished.

:param: queue The queue on which the completion handler is dispatched.
:param: completionHandler A closure to be executed once the request has finished and the data has been mapped to a swift Object. The closure takes 5 arguments: the URL request, the URL response, the response array (of type Mappable), the raw response data, and any error produced making the request.

:returns: The request.
*/
public func responseArray<T: Mappable>(queue: dispatch_queue_t?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, AnyObject?, NSError?) -> Void) -> Self {

return response(queue: queue, serializer: Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)) { (request, response, data, error) -> Void in

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

let parsedObject = Mapper<T>().mapArray(data)

dispatch_async(queue ?? dispatch_get_main_queue()) {
completionHandler(self.request, self.response, parsedObject, data, error)
}
}
}
}
}
44 changes: 44 additions & 0 deletions AlamofireObjectMapperTests/AlamofireObjectMapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ class AlamofireObjectMapperTests: XCTestCase {
XCTAssertNil(error, "\(error)")
})
}

func testArrayResponseObject() {
// This is an example of a functional test case.
let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
let expectation = expectationWithDescription("\(URL)")

Alamofire.request(.GET, URL, parameters: nil).responseArray { (response: [Forecast]?, error: NSError?) in
expectation.fulfill()

XCTAssertNotNil(response, "Response should not be nil")

for forecast in response! {
XCTAssertNotNil(forecast.day, "day should not be nil")
XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
}
}

waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in
XCTAssertNil(error, "\(error)")
})
}

func testArrayResponseObject2() {
// This is an example of a functional test case.
let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/f583be1121dbc5e9b0381b3017718a70c31054f7/sample_array_json"
let expectation = expectationWithDescription("\(URL)")

Alamofire.request(.GET, URL, parameters: nil).responseArray { (request: NSURLRequest, HTTPURLResponse: NSHTTPURLResponse?, response: [Forecast]?, data: AnyObject?, error: NSError?) in
expectation.fulfill()

XCTAssertNotNil(response, "Response should not be nil")

for forecast in response! {
XCTAssertNotNil(forecast.day, "day should not be nil")
XCTAssertNotNil(forecast.conditions, "conditions should not be nil")
XCTAssertNotNil(forecast.temperature, "temperature should not be nil")
}
}

waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in
XCTAssertNil(error, "\(error)")
})
}
}

class WeatherResponse: Mappable {
Expand Down

0 comments on commit 9f47677

Please sign in to comment.