Skip to content

Commit

Permalink
Merge pull request #75 from pelias/create-error
Browse files Browse the repository at this point in the history
Create NSError if not valid geojson
  • Loading branch information
msmollin authored Apr 19, 2017
2 parents dab07d1 + 5c2b826 commit 4b3d8a8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
10 changes: 6 additions & 4 deletions pelias-ios-sdkTests/PeliasSearchManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ class PeliasSearchManagerTests: XCTestCase {
// }

func testResponseObjectEncoding() {
let seed: NSDictionary = ["SuperSweetKey": "SuperSweetValue"]
let seed: Dictionary = ["SuperSweetKey": "SuperSweetValue"]

let testResponse = PeliasSearchResponse(parsedResponse: seed)

PeliasSearchResponse.encode(testResponse)

let decodedObject = PeliasSearchResponse.decode()
XCTAssert(testResponse.parsedResponse == decodedObject?.parsedResponse)
guard let response = decodedObject?.parsedResponse else { return }
XCTAssertTrue(testResponse.parsedResponse.elementsEqual(response, by: { (obj1, obj2) -> Bool in
return obj1.key == obj2.key && obj1.value as? String == obj2.value as? String
}))
}



}
47 changes: 24 additions & 23 deletions src/PeliasSearchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,45 +157,46 @@ open class PeliasResponse: APIResponse {
/// The url response if the request completed successfully.
open let response: URLResponse?
/// The error if an error occured executing the operation.
open let error: NSError?
open var error: NSError?
///
open var parsedResponse: PeliasSearchResponse?


private static let validTypes: Set = ["Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", "GeometryCollection", "Feature", "FeatureCollection"]

public init(data: Data?, response: URLResponse?, error: NSError?) {
self.data = data
self.response = response
self.error = error
if let dictResponse = parseData(data) {
parsedResponse = PeliasSearchResponse(parsedResponse: dictResponse)
if let dictResponse: Dictionary<String, Any> = parseData(data) {
if let type = dictResponse["type"] as? String {
if PeliasResponse.validTypes.contains(type) {
parsedResponse = PeliasSearchResponse(parsedResponse: dictResponse)
}
} else {
let meta = dictResponse["meta"] as? NSDictionary
guard let code = meta?["status_code"] as? Int else { return }
let results = dictResponse["results"] as? NSDictionary
let error = results?["error"] as? NSDictionary
guard let message = error?["message"] else { return }
self.error = NSError.init(domain: "Pelias", code: code, userInfo: [NSLocalizedDescriptionKey:message])
}
}
}

fileprivate func parseData(_ data: Data?) -> NSDictionary? {
guard let JSONData = data else { return nil }
do {
let JSON = try JSONSerialization.jsonObject(with: JSONData, options:JSONSerialization.ReadingOptions(rawValue: 0))
guard let JSONDictionary :NSDictionary = JSON as? NSDictionary else {
print("Not a Dictionary")
// put in function
return nil
}
print("JSONDictionary! \(JSONDictionary)")
return JSONDictionary
}
catch let JSONError as NSError {
print("\(JSONError)")
}
return nil
fileprivate func parseData<T>(_ data: Data?) -> T? {
guard let jsonData = data else { return nil }
let jsonObj = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
return jsonObj as? T
}
}

/// Response that can be saved to disk
public struct PeliasSearchResponse {
/// Response data that will be saved to disk.
public let parsedResponse: NSDictionary
public let parsedResponse: Dictionary<String, Any>

/// Constructs a new response given a response dictionary from the server.
public init(parsedResponse: NSDictionary) {
public init(parsedResponse: Dictionary<String, Any>) {
self.parsedResponse = parsedResponse
}

Expand Down Expand Up @@ -236,7 +237,7 @@ extension PeliasSearchResponse {
}

public required init?(coder aDecoder: NSCoder) {
guard let parsedResponse = aDecoder.decodeObject(forKey: "parsedResponse") as? NSDictionary else { response = nil; super.init(); return nil }
guard let parsedResponse = aDecoder.decodeObject(forKey: "parsedResponse") as? Dictionary<String, Any> else { response = nil; super.init(); return nil }

response = PeliasSearchResponse(parsedResponse: parsedResponse)

Expand Down

0 comments on commit 4b3d8a8

Please sign in to comment.