Skip to content

Commit b78ed79

Browse files
authored
Merge pull request #68 from k8mil/master
Support for Moya 9.0.0. Add support for ReactiveSwift and RxSwift
2 parents 0d1a036 + 907d373 commit b78ed79

6 files changed

Lines changed: 132 additions & 104 deletions

File tree

Moya-ObjectMapper.podspec

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Pod::Spec.new do |s|
2222

2323
s.subspec "Core" do |ss|
2424
ss.source_files = "Source/*.swift"
25-
ss.dependency "Moya", '~> 8.0'
25+
ss.dependency "Moya", '~> 9.0'
2626
ss.dependency "ObjectMapper", '~> 2.2'
2727
ss.framework = "Foundation"
2828
end
@@ -34,10 +34,10 @@ Pod::Spec.new do |s|
3434
ss.dependency "RxSwift", '~> 3.1'
3535
end
3636

37-
# s.subspec "ReactiveCocoa" do |ss|
38-
# ss.source_files = "Source/ReactiveCocoa/*.swift"
39-
# ss.dependency "Moya/ReactiveCocoa"
40-
# ss.dependency "Moya-ObjectMapper/Core"
41-
# ss.dependency "ReactiveSwift"
42-
# end
37+
s.subspec "ReactiveSwift" do |ss|
38+
ss.source_files = "Source/ReactiveSwift/*.swift"
39+
ss.dependency "Moya/ReactiveSwift"
40+
ss.dependency "Moya-ObjectMapper/Core"
41+
ss.dependency "ReactiveSwift"
42+
end
4343
end

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ Due to the fact that most libraries haven't officially released a Swift 3.0 vers
1515

1616
```ruby
1717
pod 'Moya-ObjectMapper'
18-
pod 'Moya'
1918
```
2019

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

2322
```ruby
2423
pod 'Moya-ObjectMapper/RxSwift'
25-
pod 'Moya'
26-
pod 'RxSwift'
24+
```
25+
26+
The subspec if you want to use the bindings over ReactiveSwift.
2727

28+
```ruby
29+
pod 'Moya-ObjectMapper/ReactiveSwift'
2830
```
2931

3032
# Usage
@@ -54,7 +56,7 @@ struct Repository: Mappable {
5456
}
5557
```
5658

57-
## 1. Without RxSwift
59+
## 1. Without RxSwift and ReactiveSwift
5860

5961

6062
```swift
@@ -90,19 +92,35 @@ GitHubProvider.request(.userRepositories(username), completion: { result in
9092

9193
```swift
9294
GitHubProvider.request(.userRepositories(username))
93-
.mapArray(Repository)
95+
.mapArray(Repository.self)
9496
.subscribe { event -> Void in
9597
switch event {
9698
case .next(let repos):
9799
self.repos = repos
98100
case .error(let error):
99101
print(error)
100-
default:
101-
break
102+
default: break
102103
}
103104
}.addDisposableTo(disposeBag)
104105
```
105106

107+
108+
## 2. With ReactiveSwift
109+
110+
```swift
111+
GitHubProvider.request(.userRepositories(username))
112+
.mapArray(Repository.self)
113+
.start { event in
114+
switch event {
115+
case .value(let repos):
116+
self.repos = repos
117+
case .failed(let error):
118+
print(error)
119+
default: break
120+
}
121+
}
122+
```
123+
106124
# Contributing
107125

108126
Issues and pull requests are welcome!

Source/ReactiveCocoa/SignalProducer+ObjectMapper.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import ReactiveSwift
2+
import Moya
3+
import ObjectMapper
4+
5+
/// Extension for processing Responses into Mappable objects through ObjectMapper
6+
extension SignalProducerProtocol where Value == Moya.Response, Error == MoyaError {
7+
8+
/// Maps data received from the signal into an object which implements the Mappable protocol.
9+
/// If the conversion fails, the signal errors.
10+
public func mapObject<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> SignalProducer<T, MoyaError> {
11+
return producer.flatMap(.latest) {
12+
(response: Response) -> SignalProducer<T, Error> in
13+
return unwrapThrowable { try response.mapObject(type, context: context) }
14+
}
15+
}
16+
17+
/// Maps data received from the signal into an array of objects which implement the Mappable
18+
/// protocol.
19+
/// If the conversion fails, the signal errors.
20+
public func mapArray<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> SignalProducer<[T], MoyaError> {
21+
return producer.flatMap(.latest) { (response: Response) -> SignalProducer<[T], Error> in
22+
return unwrapThrowable { try response.mapArray(type, context: context) }
23+
}
24+
}
25+
}
26+
27+
/// Maps throwable to SignalProducer
28+
private func unwrapThrowable<T>(_ throwable: () throws -> T) -> SignalProducer<T, MoyaError> {
29+
do {
30+
return SignalProducer(value: try throwable())
31+
} catch {
32+
return SignalProducer(error: error as! MoyaError)
33+
}
34+
}

Source/RxSwift/Observable+ObjectMapper.swift

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Single+MoyaObjectMapper.swift
3+
//
4+
// Created by Ivan Bruel on 09/12/15.
5+
// Copyright © 2015 Ivan Bruel. All rights reserved.
6+
//
7+
8+
import Foundation
9+
import RxSwift
10+
import Moya
11+
import ObjectMapper
12+
13+
14+
/// Extension for processing Responses into Mappable objects through ObjectMapper
15+
public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {
16+
17+
/// Maps data received from the signal into an object
18+
/// which implements the Mappable protocol and returns the result back
19+
/// If the conversion fails, the signal errors.
20+
public func mapObject<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> {
21+
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
22+
.flatMap { response -> Single<T> in
23+
return Single.just(try response.mapObject(type, context: context))
24+
}
25+
.observeOn(MainScheduler.instance)
26+
}
27+
28+
/// Maps data received from the signal into an array of objects
29+
/// which implement the Mappable protocol and returns the result back
30+
/// If the conversion fails, the signal errors.
31+
public func mapArray<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> {
32+
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
33+
.flatMap { response -> Single<[T]> in
34+
return Single.just(try response.mapArray(type, context: context))
35+
}
36+
.observeOn(MainScheduler.instance)
37+
}
38+
}
39+
40+
41+
// MARK: - ImmutableMappable
42+
43+
public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {
44+
45+
/// Maps data received from the signal into an object
46+
/// which implements the ImmutableMappable protocol and returns the result back
47+
/// If the conversion fails, the signal errors.
48+
public func mapObject<T:ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> {
49+
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
50+
.flatMap { response -> Single<T> in
51+
return Single.just(try response.mapObject(type, context: context))
52+
}
53+
.observeOn(MainScheduler.instance)
54+
}
55+
56+
/// Maps data received from the signal into an array of objects
57+
/// which implement the ImmutableMappable protocol and returns the result back
58+
/// If the conversion fails, the signal errors.
59+
public func mapArray<T:ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> {
60+
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
61+
.flatMap { response -> Single<[T]> in
62+
return Single.just(try response.mapArray(type, context: context))
63+
}
64+
.observeOn(MainScheduler.instance)
65+
}
66+
}

0 commit comments

Comments
 (0)