GithubHelp home page GithubHelp logo

moyaunbox's Introduction

MoyaUnbox

Unbox mappings for Moya network requests, includes ReactiveCocoa and RxSwift support.

Installation

Carthage

Add github JaviLorbada/MoyaUnbox to your Cartfile.

Carthage will build three frameworks, MoyaUnbox, RxMoyaUnbox and ReactiveMoyaUnbox, once then just import the one you need depends on your preference.

Usage

Unbox Model

First you need to create your model, for example a GitHub user based on their user API.

struct GHUser {
  let login: String
  let identifier: Int
  let avatarURL: NSURL}

extension GHUser: Unboxable {
  init(unboxer: Unboxer) {
    self.login = unboxer.unbox("login")
    self.identifier = unboxer.unbox("id")
    self.avatarURL = unboxer.unbox("avatar_url")
    
  }
}

Then you need do the mappings.

1. Moya

After you create your own API provider like

let provider = MoyaProvider<GitHubAPI>(plugins: [NetworkLoggerPlugin(verbose: true)])

you could use mapObject and mapArray for the response:

provider.request(.UserProfile("octocat"),   
completion: { result in
	do {
	  if let user = try result.value?.mapObject(GHUser) {
	    print("User: \(user)")
	  } else {
	    print(result.error)
		}
	} catch {
	  print(error)
	}
})

2. RxSwift

If you use Moya RxSwift extensions, you can use RxMoyaUnbox extension.

 provider.request(.UserProfile("JaviLorbada"))
    .filterSuccessfulStatusCodes()
    .mapObject(GHUser)
    .observeOn(MainScheduler.instance)
    .subscribe(onNext: { user in
      print("User: \(user)")
    }, onError: { error in
	    print(error)
    })
    .addDisposableTo(disposeBag)

3. ReactiveCocoa

If you use Moya ReactiveCocoa extensions, you can use ReactiveMoyaUnbox extension.

 provider.request(.UserProfile("JaviLorbada"))
    .filterSuccessfulStatusCodes()
    .mapObject(GHUser)
    .observeOn(UIScheduler())
    .startWithResult { result in
    if let user = result.value {
      print("User: \(user)")
    } else { 
	    print(result.error)
    }
  }

Tests

The project contains some tests as an example. If you wanna check it out,

  1. Clone the repo git clone https://github.com/JaviLorbada/MoyaUnbox
  2. Run the setup script on $ bin/setup-iOS

Contributing

Issues / Pull Requests / Feedback welcome

Thanks

This project took inspiration on all the other Moya Mappings. If you use any other JSON serialisation library you should check them out.

License

MoyaUnbox is available under the MIT license. See the LICENSE file for more info.

Contact:

moyaunbox's People

Contributors

javilorbada avatar pabloelizalde avatar

Stargazers

 avatar

Watchers

 avatar  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.