Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
/ Moya-Marshal Public archive

Marshal bindings for Moya, RxSwift and ReactiveCocoa

License

Notifications You must be signed in to change notification settings

JARMourato/Moya-Marshal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moya-Marshal

CocoaPods Swift 3.0.x

CI Status Version License Platform

Example

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

Installation

CocoaPods

pod 'Moya-Marshal'

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

pod 'Moya-Marshal/RxSwift'

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

pod 'Moya-Marshal/ReactiveSwift'

Usage

Create a Class or Struct which implements the Unmarshaling protocol.

import Foundation
import Marshal

struct User: Unmarshaling {
  var id: Int
  var name: String
  var email: String

  init(object: MarshaledObject) throws {
    id = try object.value(for: "id")
    name = try object.value(for: "name")
    email = try object.value(for: "email")
  }
}

Moya.Response mapping

provider
    .request(.AllUsers) { result in
        if case let .Success(response) = result {
            do {
                let argoUsers:[ArgoUser] = try response.mapArray(MarshalUser.self)
                print("mapped to users: \(argoUsers)")
            } catch {
                print("Error mapping users: \(error)")
            }
        }
    }

RxSwift

provider
    .request(.AllUsers)
    .mapArray(MarshalUser.self)
    .observeOn(MainScheduler.instance)
    .subscribeNext { users in
        self.users = users
        self.tableView.reloadData()
    }.addDisposableTo(disposeBag)

ReactiveSwift

provider
    .request(.AllUsers)
    .mapArray(MarshalUser.self)
    .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
        }
    }

Contributing

Feedback, issues or pull requests are welcomed!

Thanks

This project tries to follow the Moya-ObjectMapper project standards.

Author

JARMourato, joao.armourato@gmail.com

License

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