Latest 1.0.0
Homepage https://github.com/gperdomor/MoyaUnbox
License MIT
Platforms ios 8.0, osx 10.12, watchos 3.1, tvos 9.0, requires ARC
Authors

Build Status
codecov
Carthage compatible
CocoaPods compatible
Swift Package Manager compatible

Unbox bindings for
Moya for easier JSON serialization. Includes
RxSwift and ReactiveSwift bindings as well.

Installation

CocoaPods

Use the following entry in your Podfile

pod 'MoyaUnbox', '1.0.0'

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

pod 'MoyaUnbox/RxSwift', '1.0.0'

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

pod 'MoyaUnbox/ReactiveSwift', '1.0.0'

Usage

Create a model struct or class. It needs to implement protocol Unboxable. More details about model creation here

import Foundation
import Unbox

struct Repository: Unboxable {
    let identifier: Int
    let name: String
    let fullName: String
    let language: String? // Optional property

    init(unboxer: Unboxer) throws {
        identifier = try unboxer.unbox(key: "id")
        name = try unboxer.unbox(key: "name")
        fullName = try unboxer.unbox(key: "full_name")
        language = unboxer.unbox(key: "language")
    }
}

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

map(to: type) // map object
map(to: type, fromKey: key) // map object
map(to: [type]) // map array of objects
map(to: [type], fromKey: key) // map array of objects

While using map(to: type) tries to map whole response data to object,
with map(to: type, fromKey: key) you can specify nested object in a response to
fetch. For example map(to: type, fromKey: "data.response.user") will go through
dictionary of data, through dictionary of response to dictionary of user, which it
will parse.

For RxSwift and ReactiveSwift additionally methods for optional mapping are provided.
These methods are:

mapOptional(to: type)
mapOptional(to: type, fromKey: key)
mapOptional(to: [type])
mapOptional(to: [type], fromKey: key)

See examples below, or in a Demo project.

1. Normal usage (without RxSwift or ReactiveSwift)

provider = MoyaProvider<GitHub>()
provider.request(GitHub.repos(username: "gperdomor")) { (result) in
    if case .success(let response) = result {
        do {
            let repos = try response.map(to: [Repository.self])
            print(repos)
        } catch Error.jsonMapping(let error) {
            print(try? error.mapString())
        } catch {
            print(":(")
        }
    }
}

2. RxSwift

provider = RxMoyaProvider<GitHub>()
provider.request(GitHub.repo(fullName: "gperdomor/sygnaler"))
        .map(to: Repository.self)
        .subscribe { event in
            switch event {
            case .next(let repo):
                print(repo)
            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<GitHub>()
provider
    .request(GitHub.repos(username: "gperdomor"))
    .mapOptional(to: [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. ReactiveSwift

provider = ReactiveSwiftMoyaProvider<GitHub>()
provider
    .request(GitHub.repos(username: "gperdomor"))
    .map(to: [Repository.self])
    .start { event in
        switch event {
        case .value(let repos):
            print(repos)
        case .failed(let error):
            print(error)
        default: break
        }
    }

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

provider = ReactiveSwiftMoyaProvider<GitHub>()
provider
    .request(GitHub.repos(username: "gperdomor"))
    .mapOptional(to: [Repository.self])
    .start { event in
        switch event {
        case .value(let repos):
            // Here we can have either nil or [Repository] object.
            print(repos)
        case .failed(let error):
            print(error)
        default: break
        }
    }

Sample Project

There’s a sample project in the Demo directory. To use it, run pod install to download the required libraries. Have fun!

Other Mappers

Contributing

Hey! Like MoyaUnbox? Awesome! We could actually really use your help!

Open source isn’t just writing code. MoyaUnbox could use your help with any of the
following:

  • Finding (and reporting!) bugs.
  • New feature suggestions.
  • Answering questions on issues.
  • Documentation improvements.
  • Reviewing pull requests.
  • Helping to manage issue priorities.
  • Fixing bugs/new features.

If any of that sounds cool to you, send a pull request! After a few
contributions, we’ll add you as an admin to the repo so you can merge pull
requests and help steer the ship :ship:

License

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

Latest podspec

{
    "name": "MoyaUnbox",
    "version": "1.0.0",
    "summary": "Unbox bindings for Moya. Includes RxSwift and ReactiveSwift bindings as well.",
    "description": "[Unbox](https://github.com/JohnSundell/Unbox) bindings forn[Moya](https://github.com/Moya/Moya) for easier JSON serialization.nIncludes [RxSwift](https://github.com/ReactiveX/RxSwift) and [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift) bindings as well.nInstructions on how to use it are inn[the README](https://github.com/gperdomor/MoyaUnbox).",
    "homepage": "https://github.com/gperdomor/MoyaUnbox",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Gustavo Perdomo": "[email protected]"
    },
    "social_media_url": "",
    "platforms": {
        "ios": "8.0",
        "osx": "10.12",
        "watchos": "3.1",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/gperdomor/MoyaUnbox.git",
        "tag": "1.0.0"
    },
    "requires_arc": true,
    "default_subspecs": "Core",
    "pushed_with_swift_version": "3.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Sources/MoyaUnbox/*.swift",
            "dependencies": {
                "Moya": [
                    "~> 8.0"
                ],
                "Unbox": [
                    "~> 2.4"
                ]
            },
            "frameworks": "Foundation"
        },
        {
            "name": "RxSwift",
            "source_files": "Sources/RxMoyaUnbox/*.swift",
            "dependencies": {
                "MoyaUnbox/Core": [],
                "Moya/RxSwift": [],
                "RxSwift": [
                    "~> 3.2"
                ]
            }
        },
        {
            "name": "ReactiveSwift",
            "source_files": "Sources/ReactiveMoyaUnbox/*.swift",
            "dependencies": {
                "MoyaUnbox/Core": [],
                "Moya/ReactiveCocoa": [],
                "ReactiveSwift": [
                    "~> 1.0"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This