Repository: Moya/Moya
File: Sources/ReactiveMoya/SignalProducer+Response.swift (Swift)

Code before:
import Foundation
import ReactiveSwift
#if !COCOAPODS
import Moya
#endif

/// Extension for processing raw NSData generated by network access.
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> {
        return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1)
    }

    /// Filters out responses that have the specified `statusCode`.
    public func filter(statusCode: Int) -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filter(statusCode: statusCode) }
        }
    }

    /// Filters out responses where `statusCode` falls within the range 200 - 299.
    public func filterSuccessfulStatusCodes() -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filterSuccessfulStatusCodes() }
        }
    }

    /// Filters out responses where `statusCode` falls within the range 200 - 399
    public func filterSuccessfulStatusAndRedirectCodes() -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filterSuccessfulStatusAndRedirectCodes() }
        }
    }

    /// Maps data received from the signal into an Image. If the conversion fails, the signal errors.
    public func mapImage() -> SignalProducer<Image, MoyaError> {

Review comment (suggestion):
this shouldn't have to use logic again, it is defined on `Response` and this way we can have inconsistent condense whenever we forgot to change the same line in 3 places after a fix/change... let's change it to:
```swift
     public func filter(statusCodes: Range<Int>) -> SignalProducer<Value, MoyaError> {
         return producer.flatMap(.latest) { response -> SignalProducer<Value, Error> in
             return unwrapThrowable { try response.filter(statusCodes: statusCodes) }
          }
      }
```

Code after:
import Foundation
import ReactiveSwift
#if !COCOAPODS
import Moya
#endif

/// Extension for processing raw NSData generated by network access.
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> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, Error> in
            return unwrapThrowable { try response.filter(statusCodes: statusCodes) }
        }
    }

    /// Filters out responses that have the specified `statusCode`.
    public func filter(statusCode: Int) -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filter(statusCode: statusCode) }
        }
    }

    /// Filters out responses where `statusCode` falls within the range 200 - 299.
    public func filterSuccessfulStatusCodes() -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filterSuccessfulStatusCodes() }
        }
    }

    /// Filters out responses where `statusCode` falls within the range 200 - 399
    public func filterSuccessfulStatusAndRedirectCodes() -> SignalProducer<Value, MoyaError> {
        return producer.flatMap(.latest) { response -> SignalProducer<Value, MoyaError> in
            return unwrapThrowable { try response.filterSuccessfulStatusAndRedirectCodes() }
        }
    }