Latest 5.0.0
Homepage https://github.com/wattson12/Moya-Argo
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0
Authors

CI Status
Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

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

pod "Moya-Argo"

Subspec

There are subspecs available for RxSwift and Reactive cocoa if you are using Moya/RxSwift or Moya/ReactiveSwift. The pod names are Moya-Argo/RxSwift and Moya-Argo/ReactiveSwift respectively

Usage

Argo

The first step is having a class / struct which can be mapped by Argo

struct ArgoUser: Argo.Decodable {

    let id: Int
    let name: String

    let birthdate: String?

    static func decode(json: JSON) -> Decoded<ArgoUser> {
        return curry(ArgoUser.init)
            <^> json <| "id"
            <*> json <| "name"
            <*> json <|? "birthdate"
    }
}

Moya.Response mapping

If you have a request setup with Moya already, you can use the mapObject and mapArray methods on the response:

provider
    .request(.AllUsers) { result in

        if case let .Success(response) = result {

            do {
                let argoUsers:[ArgoUser] = try response.mapArrayWithRootKey("users")
                print("mapped to users: (argoUsers)")
            } catch {
                print("Error mapping users: (error)")
            }
        }
    }

RxSwift

If you are using the Moya RxSwift extensions, there is an extension on Observable which will simplify the mapping:

provider
    .rx
    .request(.AllUsers)
    .mapArray(ArgoUser.self, rootKey: "users")
    .observeOn(MainScheduler.instance)
    .subscribeNext { users in

        self.users = users
        self.tableView.reloadData()

    }.addDisposableTo(disposeBag)

ReactiveSwift

Or for ReactiveSwift, there are similar extensions on SignalProducer:

provider
    .reactive
    .request(.AllUsers)
    .mapArray(ArgoUser.self, rootKey: "users")
    .observeOn(UIScheduler())
    .start { event in

        switch event {
        case .Next(let users):
            self.users = users
            self.tableView.reloadData()
        case .Failed(let error):
            print("error: (error)")
        default: break
        }
    }

Helpers

The example project shows some example methods which can be used to improve the readability of your code

Contributing

Issues / Pull Requests / Feedback welcome

Thanks

I took a lot of guidance from the Moya-ObjectMapper project. If you are using ObjectMapper for your serialisation you should definitely check them out.

Author

Sam Watts, [email protected]

License

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

Latest podspec

{
    "name": "Moya-Argo",
    "version": "5.0.0",
    "summary": "Argo mappings for Moya network requests",
    "description": "Extensions to simplify mapping Moya responses using Argo",
    "homepage": "https://github.com/wattson12/Moya-Argo",
    "license": "MIT",
    "authors": {
        "Sam Watts": "[email protected]"
    },
    "source": {
        "git": "https://github.com/wattson12/Moya-Argo.git",
        "tag": "5.0.0"
    },
    "social_media_url": "https://twitter.com/wattson12",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Pod/Classes/*.swift",
            "dependencies": {
                "Moya": [
                    "~> 11.0.0"
                ],
                "Argo": [
                    "~> 4.1.2"
                ]
            },
            "frameworks": "Foundation"
        },
        {
            "name": "ReactiveSwift",
            "source_files": "Pod/Classes/ReactiveSwift/*.swift",
            "dependencies": {
                "Moya-Argo/Core": [],
                "Moya/ReactiveSwift": []
            }
        },
        {
            "name": "RxSwift",
            "source_files": "Pod/Classes/RxSwift/*.swift",
            "dependencies": {
                "Moya-Argo/Core": [],
                "Moya/RxSwift": []
            }
        }
    ]
}

Pin It on Pinterest

Share This