Skip to content

Instantly share code, notes, and snippets.

@erikolsson
Created March 1, 2017 16:25
Show Gist options
  • Save erikolsson/79df08a4af15bc4f87cf10507537168f to your computer and use it in GitHub Desktop.
Save erikolsson/79df08a4af15bc4f87cf10507537168f to your computer and use it in GitHub Desktop.
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