Code Monkey home page Code Monkey logo

moya-argo's Introduction

Moya-Argo

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.

moya-argo's People

Contributors

juvs avatar kingiol avatar shanev avatar vendruscolo avatar wattson12 avatar werediver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

moya-argo's Issues

Carthage update error

*** Cloning Moya-Argo
*** Checking out Moya-Argo at "3.0.0"
*** xcodebuild output can be found in /var/folders/jk/cd0fk3hx45jblh4c88jm_4000000gn/T/carthage-xcodebuild.uheOfD.log
*** Skipped building Moya-Argo due to the error:
Dependency "Moya-Argo" has no shared framework schemes

If you believe this to be an error, please file an issue with the maintainers at https://github.com/wattson12/Moya-Argo/issues/new

Ambiguous reference to member "request"

I'm using Argo + Moya + Moya-Argo + ReactiveCocoa but I get this error:

Ambiguous reference to member "request"

My code is:

let provider:ReactiveCocoaMoyaProvider<MyAPI> = ReactiveCocoaMoyaProvider(stubClosure: { _ in return .Immediate })
provider.request(.User(id: "24"))
        .mapObject("user")
        .observeOn(UIScheduler())
        .start { event in
            expectation.fulfill()
            switch event {
            case .Next(let user):
                XCTAssertNotNil(user)
                XCTAssertEqual(user.id, 1)
            case .Failed(let error):
                XCTFail("Error mapping user: \(error)")
            default: break
            }

    }

And yes, It's running inside a XCTest.

Issue is fixed if I don't import Moya and only import Moya_Argo but then I get the error that ReactiveCocoaMoyaProvider doesn't exist.

Any idea on how to fix it?

Fix CI

See #16 for a short discussion

Failed build https://travis-ci.org/wattson12/Moya-Argo/builds/291330746

The last failure I see is

5.55s$ pod install
Analyzing dependencies
Fetching podspec for `Moya-Argo` from `../`
[!] Unable to satisfy the following requirements:
- `Cartography` required by `Podfile`
- `Cartography (= 2.1.0)` required by `Podfile.lock`

No real need for cartography to be in this demo, removing that could be the first step

Not published to cocoapods

The version cocoapods install doesn't use decode. Seems like last code version still not pushed to cocoapods. Installed using git url and pulled the right version:

pod 'Moya-Argo', :git => 'https://github.com/wattson12/Moya-Argo.git'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.