Latest 3.0.0
Homepage https://github.com/spxrogers/Moya-Gloss
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0, requires ARC
Authors

CocoaPods
Carthagecompatible

Gloss bindings for Moya for fabulous JSON serialization.
Supports RxSwift and ReactiveSwift bindings as well.

Installation

CocoaPods

Add to your Podfile:

pod 'Moya-Gloss'

The subspec(s) if you want to use the bindings over RxSwift or ReactiveSwift (or ReactiveCocoa).

pod 'Moya-Gloss/RxSwift'
pod 'Moya-Gloss/ReactiveSwift'

Carthage

github "spxrogers/Moya-Gloss"

Carthage lists Moya and Gloss as explicit dependencies, so it’s only
necessary to list the above in your Cartfile and it will pull down all
three libraries. Carthage will generate three frameworks, MoyaGloss,
RxMoyaGloss, and ReactiveMoyaGloss (following the naming convention of the
three generated frameworks from Moya).

Note: is only necessary to import one of these variations, otherwise Xcode
will give you an "Ambiguous use…" error.

Usage

Define your Model

Create a Class or Struct which implements the Decodable (or Glossy) protocol.

import Foundation
import Gloss

struct Person: JSONDecodable {

  let name: String
  let age: Int?

  init?(json: JSON) {
    guard let name: String = "name" <~~ json
      else { return nil }

    self.name = name
    self.age = "age" <~~ json
  }
}

API

mapObject()
mapObject(forKeyPath:)
mapArray()
mapArray(forKeyPath:)

Examples

1. Example with no reactive extension

provider.request(ExampleAPI.GetObject) { (result) in
  switch result {
  case .success(let response):
    do {
      let person = try response.mapObject(Person.self)
      // *OR* the line below for a keyPath
      // let person = try response.mapObject(Person.self, forKeyPath: "person")
      print("Found person: (person)")
    } catch {
      print(error)
    }
  case .failure(let error):
    print(error)
  }
}

2. Example with RxSwift

provider.request(ExampleAPI.GetObject)
  .mapObject(type: Person.self)
  // *OR* the line below for a keyPath
  // .mapObject(Person.self, forKeyPath: "multi.nested.person")
  .subscribe { (event) in
    switch event {
    case .success(let person):
      print("Found person: (person)")
      break
    case .error(let error):
      print(error)
      break
    }
  }
  .disposedBy(by: your_preferred_dispose_bag)

3. Example with ReactiveCocoa

provider.request(ExampleAPI.GetObject)
  .mapObject(type: Person.self)
  // *OR* the line below for a keyPath
  // .mapObject(Person.self, forKeyPath: "person")
  .start { (event) in
    switch event {
    case .value(let person):
      print("Found person: (person)")
    case .failed(let error):
      print(error)
    default:
      break
    }
  }

Contributing

Issues and pull requests are welcome!

Author

Steven Rogers @spxrogers

Thanks …

… to Harlan Kellaway for creating Gloss, my preferred JSON library :)

… to the Moya team for a wonderful network library.

… to the authors of the existing Moya community extensions for inspiring me to make one for Gloss.

… and finally, to everyone in the community that has contributed back :heart:

License

Moya-Gloss is released under an MIT license. See LICENSE for more information.

Chuck Norris likes a permissivelicense.

Latest podspec

{
    "name": "Moya-Gloss",
    "version": "3.0.0",
    "summary": "Convenience Gloss bindings for Moya.",
    "description": "[Gloss](https://github.com/hkellaway/Gloss) bindings forn[Moya](https://github.com/Moya/Moya) for fabulous JSON serialization.n[RxSwift](https://github.com/ReactiveX/RxSwift/) and [ReactiveSwift](https://github.com/ReactiveCocoa/ReactiveSwift/) bindings also included.nInstructions on how to use it are inn[the README](https://github.com/spxrogers/Moya-Gloss).",
    "homepage": "https://github.com/spxrogers/Moya-Gloss",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "steven rogers": "[email protected]"
    },
    "source": {
        "git": "https://github.com/spxrogers/Moya-Gloss.git",
        "tag": "3.0.0"
    },
    "social_media_url": "https://twitter.com/spxrogers",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "default_subspecs": "Core",
    "pushed_with_swift_version": "4.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Source/*.swift",
            "dependencies": {
                "Moya": [
                    "~> 10.0"
                ],
                "Gloss": [
                    "~> 2.0.0-beta.2"
                ]
            },
            "frameworks": "Foundation"
        },
        {
            "name": "RxSwift",
            "source_files": "Source/RxSwift/*.swift",
            "dependencies": {
                "Moya-Gloss/Core": [],
                "Moya/RxSwift": [],
                "RxSwift": [
                    "~> 4.0"
                ]
            }
        },
        {
            "name": "ReactiveSwift",
            "source_files": "Source/ReactiveSwift/*.swift",
            "dependencies": {
                "Moya-Gloss/Core": [],
                "Moya/ReactiveSwift": [],
                "ReactiveSwift": [
                    "~> 2.0"
                ]
            }
        },
        {
            "name": "ReactiveCocoa",
            "dependencies": {
                "Moya-Gloss/ReactiveSwift": []
            }
        }
    ]
}

Pin It on Pinterest

Share This