Skip to content

Commit

Permalink
extra debug var plus fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Jun 5, 2016
1 parent a0e0f2c commit d30d525
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion AlamofireXmlToObjects.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "AlamofireXmlToObjects"
s.version = "1.1.0"
s.version = "1.2.0"
s.summary = "An Alamofire extension which converts XML response data into swift objects using EVReflection and XMLDictionary"
s.description = "An Alamofire extension which converts XML response data into swift objects using EVReflection and XMLDictionary. "
s.homepage = "https://github.com/evermeer/AlamofireXmlToObjects"
Expand Down
32 changes: 18 additions & 14 deletions AlamofireXmlToObjects/AlamofireXmlToObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,53 @@ import XMLDictionary
import Alamofire

extension Request {

static var outputDictionary: Bool = false

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

- parameter 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 object (of type Mappable) and any error produced while making the request

- returns: The request.
*/
public func responseObject<T:EVObject>(completionHandler: (Result<T, NSError>) -> Void) -> Self {
public func responseObject<T: EVObject>(completionHandler: (Result<T, NSError>) -> Void) -> Self {
return responseObject(nil) { (request, response, data) in
completionHandler(data)
}
}

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

- parameter 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 object (of type Mappable), the raw response data, and any error produced making the request.

- returns: The request.
*/
public func responseObject<T:EVObject>(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<T, NSError>) -> Void) -> Self {
public func responseObject<T: EVObject>(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<T, NSError>) -> Void) -> Self {
return responseObject(nil) { (request, response, data) in
completionHandler(request, response, data)
}
}

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

- parameter queue: The queue on which the completion handler is dispatched.
- parameter 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 object (of type Mappable), the raw response data, and any error produced making the request.

- returns: The request.
*/
public func responseObject<T:EVObject>(queue: dispatch_queue_t?, completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<T, NSError>) -> Void) -> Self {
public func responseObject<T: EVObject>(queue: dispatch_queue_t?, completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<T, NSError>) -> Void) -> Self {
return responseString(completionHandler: { (response) -> Void in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
dispatch_async(queue ?? dispatch_get_main_queue()) {
switch response.result {
case .Success(let xml):
let t = T()
if let result = NSDictionary(XMLString: xml) {
if Request.outputDictionary {
print("Dictionary from XML = \(result)")
}
EVReflection.setPropertiesfromDictionary(result, anyObject: t)
completionHandler(self.request, self.response, Result.Success(t))
} else {
Expand All @@ -65,6 +69,6 @@ extension Request {
}
}
}
} )
})
}
}
}
35 changes: 30 additions & 5 deletions AlamofireXmlToObjectsTests/AlamofireXmlToObjects3Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,33 @@ import Alamofire
import XMLDictionary
import EVReflection


class AllGames: EVObject {
var __name: String?
var StateProv: StateProvObject?
}

class StateProvObject: EVObject {
var __name: String?
var _stateprov_name: String?
var _stateprov_id: String?
var game: [Game] = []
}

class Game: EVObject {
var __name: String?
var _game_id: Int = 0
var _game_name: String?
var _update_time: NSDate?
var lastdraw_date: String?
var lastdraw_numbers: String?
var nextdraw_date: String?
var jackpot: Jackpot?
}

class Jackpot: EVObject {
var __text: String?
var _date: String?
}

class AlamofireXmlToObjects3Tests: XCTestCase {

Expand All @@ -37,22 +58,26 @@ class AlamofireXmlToObjects3Tests: XCTestCase {
let URL: URLStringConvertible = "http://raw.githubusercontent.com/evermeer/AlamofireXmlToObjects/master/AlamofireXmlToObjectsTests/sample3_xml"
let expectation = expectationWithDescription("\(URL)")

Request.outputDictionary = true

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.dateFormat = "ddd yyyy'-'MM'-'dd' 'HH':'mm':'ss Z"
EVReflection.setDateFormatter(dateFormatter)

Alamofire.request(.GET, URL)
.responseObject { (response: Result<AllGames, NSError>) in

expectation.fulfill()
if let error = response.error {
XCTAssert(false, "ERROR: \(error.description)")
} else {
if let result = response.value {
print("\(result.description)")

} else {
XCTAssert(false, "no result from service")
}
}


}

waitForExpectationsWithTimeout(10, handler: { (error: NSError?) -> Void in
Expand Down
6 changes: 3 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Alamofire (3.4.0)
- EVReflection (2.31.0)
- EVReflection (2.34.0)
- XMLDictionary (1.4)

DEPENDENCIES:
Expand All @@ -10,9 +10,9 @@ DEPENDENCIES:

SPEC CHECKSUMS:
Alamofire: c19a627cefd6a95f840401c49ab1f124e07f54ee
EVReflection: 94295813d37aa31feb6f9941476a3874b445b19e
EVReflection: 20e23d83fe7e0b3cd79410562c04969b3861a104
XMLDictionary: f9f51601ba76678b594ab485f2b00ec1fed23724

PODFILE CHECKSUM: 252ce6299ebc88fc63aa2f98748cf9d35ff9987b

COCOAPODS: 1.0.0.beta.8
COCOAPODS: 1.0.0

0 comments on commit d30d525

Please sign in to comment.