Coder Social home page Coder Social logo

moya-modelmapper's Introduction

Moya-ModelMapper

============

CocoaPods

ModelMapper bindings for Moya for easier JSON serialization with RxSwift and ReactiveCocoa bindings.

Installation

CocoaPods

pod 'Moya-ModelMapper', '~> 2.0.0'

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

pod 'Moya-ModelMapper/RxSwift', '~> 2.0.0'

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

pod 'Moya-ModelMapper/ReactiveCocoa', '~> 2.0.0'

Usage

Create a model struct or class. It needs to implement protocol Mappable.

import Foundation
import Mapper

struct Repository: Mappable {

    let identifier: Int
    let language: String
    let url: String? // Optional property

    init(map: Mapper) throws {
        try identifier = map.from("id")
        try language = map.from("language")
        url = map.optionalFrom("url") // Optional property
    }

}

Then you have methods that extends the response from Moya. These methods are:

mapObject()
mapObject(withKeyPath:)
mapArray()
mapArray(withKeyPath:)

While using mapObject() tries to map whole response data to object, with mapObject(withKeyPath:) you can specify nested object in a response to fetch. For example mapObject(withKeyPath: "data.response.user") will go through dictionary of data, through dictionary of response to dictionary of user, which it will parse. RxSwift and ReactiveCocoa extensions also have all of the methods, but RxSwift have optional mapping additionally. See examples below, or in a Demo project.

1. Normal usage (without RxSwift or ReactiveCocoa)

provider = MoyaProvider(endpointClosure: endpointClosure)
provider.request(GitHub.Repos("mjacko")) { (result) in
    if case .Success(let response) = result {
        do {
            let repos = try response.mapArray() as [Repository]
            print(repos)
        } catch Error.JSONMapping(let error) {
            print(try? error.mapString())
        } catch {
            print(":(")
        }
    }
}

2. RxSwift

provider = RxMoyaProvider(endpointClosure: endpointClosure)
provider
    .request(GitHub.Repo("Moya/Moya"))
    .mapObject(User.self, keyPath: "owner")
    .subscribe { event in
        switch event {
        case .Next(let user):
            print(user)
        case .Error(let error):
            print(error)
        default: break
        }
}

Additionally, modules for RxSwift contains optional mappings. It basically means that if the mapping fails, mapper doesn't throw errors but returns nil. For instance:

provider = RxMoyaProvider(endpointClosure: endpointClosure)
provider
    .request(GitHub.Repos("mjacko"))
    .mapArrayOptional(Repository.self)
    .subscribe { event in
        switch event {
        case .Next(let repos):
            // Here we can have either nil or [Repository] object.
            print(repos)
        case .Error(let error):
            print(error)
        default: break
        }
}

3. ReactiveCocoa

provider = ReactiveCocoaMoyaProvider(endpointClosure: endpointClosure)
provider
    .request(GitHub.Repos("mjacko"))
    .mapArray(Repository.self)
    .observeOn(UIScheduler())
    .start { event in
        switch event {
        case .Next(let repos):
            print(repos)
        case .Failed(let error):
            print(error)
        default: break
        }
}

Author

Sunshinejr, [email protected], @thesunshinejr

License

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

moya-modelmapper's People

Contributors

sunshinejr avatar juangdelvalle avatar solidfox avatar readmecritic avatar

Watchers

 avatar

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.