Created
March 1, 2017 16:25
-
-
Save erikolsson/79df08a4af15bc4f87cf10507537168f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import Moya | |
import Unbox | |
public extension ObservableType where E == Response { | |
public func mapObject<T: Unboxable>(type: T.Type) -> Observable<T> { | |
return flatMap { response -> Observable<T> in | |
return Observable.just(try response.mapObject(type: T.self)) | |
} | |
} | |
public func mapArray<T: Unboxable>(type: T.Type) -> Observable<[T]> { | |
return flatMap { response -> Observable<[T]> in | |
return Observable.just(try response.mapArray(type: T.self)) | |
} | |
} | |
} | |
public extension Response { | |
public func mapObject<T: Unboxable>(type: T.Type) throws -> T { | |
guard let json = try mapJSON() as? UnboxableDictionary else { | |
throw MoyaError.jsonMapping(self) | |
} | |
return try unbox(dictionary: json) | |
} | |
public func mapArray<T: Unboxable>(type: T.Type) throws -> [T] { | |
guard let jsonArray = try mapJSON() as? [UnboxableDictionary] else { | |
throw MoyaError.jsonMapping(self) | |
} | |
return try unbox(dictionaries: jsonArray) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment