Skip to content

TomVanDerSpek/Moya-JASONMapper

 
 

Repository files navigation

Moya-JASONMapper

Version Build Status Language License Platform Twitter

Installation

Moya-JASONMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Moya-JASONMapper"

The subspec if you want to use the bindings over RxSwift.

pod "Moya-JASONMapper/RxSwift"

And the subspec if you want to use the bindings over ReactiveCocoa.

pod "Moya-JASONMapper/ReactiveCocoa"

Usage

Example project

To run the example project, clone the repo, and run pod install from the Example directory first. It includes sample code and unit tests.

Model definitions

Create a Class or Struct which implements the Mappable protocol.

import Foundation
import Moya_JASONMapper
import JASON

final class GetResponse : ALJSONAble {
    
    let url:NSURL?
    let origin:String
    let args:[String: String]?
    
    required init?(jsonData:JSON){
        self.url = jsonData["url"].nsURL
        self.origin = jsonData["origin"].stringValue
        self.args = jsonData["args"].object as? [String : String]
    }
    
}

1. Without RxSwift or ReactiveCocoa

stubbedProvider.request(ExampleAPI.GetObject) { (result) -> () in
    switch result {
    case let .Success(response):
        do {
            let getResponseObject = try response.mapObject(GetResponse)
            print(getResponseObject)
        } catch {
            print(error)
        }
    case let .Failure(error):
        print(error)
    }
}

2. With ReactiveCocoa

RCStubbedProvider.request(ExampleAPI.GetObject).mapObject(GetResponse).on(failed: { (error) -> () in
    print(error)
}) { (response) -> () in
    print(response)
}.start()

3. With RxSwift

let disposeBag = DisposeBag()

RXStubbedProvider.request(ExampleAPI.GetObject).mapObject(GetResponse).subscribe(onNext: { (response) -> Void in
    print(response)
}, onError: { (error) -> Void in
    print(error)
}).addDisposableTo(disposeBag)

Other repo's

If you're using SwiftyJSON, checkout Moya-SwiftyJSONMapper

Author

Antoine van der Lee

Mail: [email protected]
Home: www.avanderlee.com
Twitter: @twannl

License

Moya-JASONMapper is available under the MIT license. See the LICENSE file for more info.

About

Map objects through JASON in combination with Moya

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 78.7%
  • Ruby 21.3%