Skip to content

Commit

Permalink
Merge pull request #1624 from LucianoPAlmeida/development
Browse files Browse the repository at this point in the history
Making range filter generic to any range type.
  • Loading branch information
sunshinejr committed Apr 6, 2018
2 parents 3e50289 + 605b31f commit af55d6d
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github "Alamofire/Alamofire" "4.7.0"
github "Alamofire/Alamofire" "4.7.1"
github "AliSoftware/OHHTTPStubs" "f90c2bb0fb882e43761ab963ca8869d349d2c6e3"
github "Quick/Nimble" "v7.0.3"
github "Quick/Quick" "v1.2.0"
Expand Down
4 changes: 2 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Next

### Added
### Changed

- Convenience filter by `Range<Int>` on reponses. [#1605](https://github.com/Moya/Moya/pull/1605) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida).
- **Breaking Change** Changed `Response`s filter method parameter to use a generic `RangeExpression` that accepts any range type. [#1624](https://github.com/Moya/Moya/pull/1624) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida).

# [11.0.1] - 2018-02-26

Expand Down
15 changes: 2 additions & 13 deletions Sources/Moya/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,19 @@ public final class Response: CustomDebugStringConvertible, Equatable {
public extension Response {

/**
Returns the `Response` if the `statusCode` falls within the specified closed range.
Returns the `Response` if the `statusCode` falls within the specified range.
- parameters:
- statusCodes: The range of acceptable status codes.
- throws: `MoyaError.statusCode` when others are encountered.
*/
public func filter(statusCodes: ClosedRange<Int>) throws -> Response {
public func filter<R: RangeExpression>(statusCodes: R) throws -> Response where R.Bound == Int {
guard statusCodes.contains(statusCode) else {
throw MoyaError.statusCode(self)
}
return self
}

/**
Returns the `Response` if the `statusCode` falls within the specified range.
- parameters:
- statusCodes: The range of acceptable status codes.
- throws: `MoyaError.statusCode` when others are encountered.
*/
public func filter(statusCodes: Range<Int>) throws -> Response {
return try filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1)
}

/**
Returns the `Response` if it has the specified `statusCode`.
Expand Down
9 changes: 1 addition & 8 deletions Sources/ReactiveMoya/SignalProducer+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import Moya
extension SignalProducerProtocol where Value == Response, Error == MoyaError {

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: ClosedRange<Int>) -> SignalProducer<Value, MoyaError> {
return producer.flatMap(.latest) { response -> SignalProducer<Value, Error> in
return unwrapThrowable { try response.filter(statusCodes: statusCodes) }
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> SignalProducer<Value, MoyaError> {
public func filter<R: RangeExpression>(statusCodes: R) -> SignalProducer<Value, MoyaError> where R.Bound == Int {
return producer.flatMap(.latest) { response -> SignalProducer<Value, Error> in
return unwrapThrowable { try response.filter(statusCodes: statusCodes) }
}
Expand Down
9 changes: 1 addition & 8 deletions Sources/RxMoya/Observable+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import Moya
extension ObservableType where E == Response {

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: ClosedRange<Int>) -> Observable<E> {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filter(statusCodes: statusCodes))
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> Observable<E> {
public func filter<R: RangeExpression>(statusCodes: R) -> Observable<E> where R.Bound == Int {
return flatMap { response -> Observable<E> in
return Observable.just(try response.filter(statusCodes: statusCodes))
}
Expand Down
9 changes: 1 addition & 8 deletions Sources/RxMoya/Single+Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import RxSwift
extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {

/// Filters out responses that don't fall within the given closed range, generating errors when others are encountered.
public func filter(statusCodes: ClosedRange<Int>) -> Single<ElementType> {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filter(statusCodes: statusCodes))
}
}

/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
public func filter(statusCodes: Range<Int>) -> Single<ElementType> {
public func filter<R: RangeExpression>(statusCodes: R) -> Single<ElementType> where R.Bound == Int {
return flatMap { response -> Single<ElementType> in
return Single.just(try response.filter(statusCodes: statusCodes))
}
Expand Down

0 comments on commit af55d6d

Please sign in to comment.