Skip to content

Commit

Permalink
Merge pull request #6 from rhodgkins/swift-3.0
Browse files Browse the repository at this point in the history
Swift 3.0 support
  • Loading branch information
rhodgkins committed Sep 9, 2016
2 parents 5d0b066 + 295db53 commit 4fb6e34
Show file tree
Hide file tree
Showing 15 changed files with 537 additions and 431 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode7.3
osx_image: xcode8
script:
# Workaround for Simulator getting stuck
# See: https://github.com/travis-ci/travis-ci/issues/3040#issuecomment-146700203
Expand Down
2 changes: 1 addition & 1 deletion HTTPStatusCodes.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'HTTPStatusCodes'
s.version = '3.0.0'
s.version = '3.1.0'
s.license = 'MIT'
s.summary = 'Swift enum wrapper for easier handling of HTTP status codes'
s.homepage = 'https://github.com/rhodgkins/SwiftHTTPStatusCodes'
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -16,11 +16,13 @@ If this library is out of date compared to this page please open an issue and I

## Usage

Swift 3.0 support is added in version 3.1 of this framework. For use with older versions of Swift use version 3.0.

### Carthage

`Cartfile`:
```ogdl
github "rhodgkins/SwiftHTTPStatusCodes" ~> 3.0
github "rhodgkins/SwiftHTTPStatusCodes" ~> 3.1
```
Source code:
```swift
Expand All @@ -30,7 +32,7 @@ import HTTPStatusCodes
### CocoaPods
`Podfile`:
```ruby
pod 'HTTPStatusCodes', '~> 3.0.0'
pod 'HTTPStatusCodes', '~> 3.1.0'
```
Source code:

Expand Down
108 changes: 63 additions & 45 deletions Sources/HTTPStatusCodes+Extensions.swift
Expand Up @@ -11,35 +11,35 @@ import Foundation
public extension HTTPStatusCode {
/// Informational - Request received, continuing process.
public var isInformational: Bool {
return inRange(100...199)
return isIn(range: 100...199)
}
/// Success - The action was successfully received, understood, and accepted.
public var isSuccess: Bool {
return inRange(200...299)
return isIn(range: 200...299)
}
/// Redirection - Further action must be taken in order to complete the request.
public var isRedirection: Bool {
return inRange(300...399)
return isIn(range: 300...399)
}
/// Client Error - The request contains bad syntax or cannot be fulfilled.
public var isClientError: Bool {
return inRange(400...499)
return isIn(range: 400...499)
}
/// Server Error - The server failed to fulfill an apparently valid request.
public var isServerError: Bool {
return inRange(500...599)
return isIn(range: 500...599)
}

/// - returns: `true` if the status code is in the provided range, false otherwise.
private func inRange(range: Range<HTTPStatusCode.RawValue>) -> Bool {
private func isIn(range: ClosedRange<HTTPStatusCode.RawValue>) -> Bool {
return range.contains(rawValue)
}
}

public extension HTTPStatusCode {
/// - returns: a localized string suitable for displaying to users that describes the specified status code.
public var localizedReasonPhrase: String {
return NSHTTPURLResponse.localizedStringForStatusCode(rawValue)
return HTTPURLResponse.localizedString(forStatusCode: rawValue)
}
}

Expand All @@ -59,15 +59,23 @@ extension HTTPStatusCode: CustomDebugStringConvertible, CustomStringConvertible
public extension HTTPStatusCode {

/// Obtains a possible status code from an optional HTTP URL response.
public init?(HTTPResponse: NSHTTPURLResponse?) {
public init?(HTTPResponse: HTTPURLResponse?) {
guard let statusCodeValue = HTTPResponse?.statusCode else {
return nil
}
self.init(statusCodeValue)
}

/// This is declared as it's not automatically picked up by the complier for the above init
private init?(_ rawValue: Int) {
guard let value = HTTPStatusCode(rawValue: rawValue) else {
return nil
}
self = value
}
}

public extension NSHTTPURLResponse {
public extension HTTPURLResponse {

/**
* Marked internal to expose (as `statusCodeValue`) for Objective-C interoperability only.
Expand All @@ -93,52 +101,62 @@ public extension NSHTTPURLResponse {
*
* - returns: the instance of the object, or `nil` if an error occurred during initialization.
*/
@available(iOS, introduced=7.0)
@available(iOS, introduced: 7.0)
@objc(initWithURL:statusCodeValue:HTTPVersion:headerFields:)
public convenience init?(URL url: NSURL, statusCode: HTTPStatusCode, HTTPVersion: String?, headerFields: [String : String]?) {
self.init(URL: url, statusCode: statusCode.rawValue, HTTPVersion: HTTPVersion, headerFields: headerFields)
public convenience init?(url: URL, statusCode: HTTPStatusCode, HTTPVersion: String?, headerFields: [String : String]?) {
self.init(url: url, statusCode: statusCode.rawValue, httpVersion: HTTPVersion, headerFields: headerFields)
}
}

// MARK: - Deprecated cases

public extension HTTPStatusCode {

@available(*, deprecated, renamed="PayloadTooLarge")
static let RequestEntityTooLarge = PayloadTooLarge
/// - deprecated: Renamed to `payloadTooLarge`
@available(*, deprecated, renamed: "payloadTooLarge")
static let requestEntityTooLarge = payloadTooLarge

@available(*, deprecated, renamed="URITooLong")
static let RequestURITooLong = URITooLong
/// - deprecated: Renamed to `uriTooLong`
@available(*, deprecated, renamed: "uriTooLong")
static let requestURITooLong = uriTooLong

@available(*, deprecated, renamed="RangeNotSatisfiable")
static let RequestedRangeNotSatisfiable = RangeNotSatisfiable
/// - deprecated: Renamed to `rangeNotSatisfiable`
@available(*, deprecated, renamed: "rangeNotSatisfiable")
static let requestedRangeNotSatisfiable = rangeNotSatisfiable

@available(*, deprecated, renamed="IISLoginTimeout")
static let LoginTimeout = IISLoginTimeout
/// - deprecated: Renamed to `iisLoginTimeout`
@available(*, deprecated, renamed: "iisLoginTimeout")
static let loginTimeout = iisLoginTimeout

@available(*, deprecated, renamed="IISRetryWith")
static let RetryWith = IISRetryWith
/// - deprecated: Renamed to `iisRetryWith`
@available(*, deprecated, renamed: "iisRetryWith")
static let retryWith = iisRetryWith

@available(*, deprecated, renamed="NginxNoResponse")
static let NoResponse = NginxNoResponse
/// - deprecated: Renamed to `nginxNoResponse`
@available(*, deprecated, renamed: "nginxNoResponse")
static let noResponse = nginxNoResponse

@available(*, deprecated, renamed="NginxSSLCertificateError")
static let CertError = NginxSSLCertificateError
/// - deprecated: Renamed to `nginxSSLCertificateError`
@available(*, deprecated, renamed: "nginxSSLCertificateError")
static let certError = nginxSSLCertificateError

@available(*, deprecated, renamed="NginxSSLCertificateRequired")
static let NoCert = NginxSSLCertificateRequired
/// - deprecated: Renamed to `nginxSSLCertificateRequired`
@available(*, deprecated, renamed: "nginxSSLCertificateRequired")
static let noCert = nginxSSLCertificateRequired

@available(*, deprecated, renamed="NginxHTTPToHTTPS")
static let HTTPToHTTPS = NginxHTTPToHTTPS
/// - deprecated: Renamed to `nginxHTTPToHTTPS`
@available(*, deprecated, renamed: "nginxHTTPToHTTPS")
static let httpToHTTPS = nginxHTTPToHTTPS

@available(*, deprecated, renamed="NginxClientClosedRequest")
static let ClientClosedRequest = NginxClientClosedRequest
/// - deprecated: Renamed to `nginxClientClosedRequest`
@available(*, deprecated, renamed: "nginxClientClosedRequest")
static let clientClosedRequest = nginxClientClosedRequest

/// Returned by version 1 of the Twitter Search and Trends API when the client is being rate limited; versions 1.1 and later use the 429 Too Many Requests response code instead.
/// Returned by version 1 of the Twitter Search and Trends API when the client is being rate limited; versions 1.1 and later use the 429 Too Many Requests (`tooManyRequests`) response code instead.
///
/// - seealso: [Twitter Error Codes & Responses](https://dev.twitter.com/docs/error-codes-responses)
@available(*, deprecated, renamed="TooManyRequests")
static let TwitterEnhanceYourCalm = TooManyRequests
@available(*, deprecated, renamed: "tooManyRequests")
static let twitterEnhanceYourCalm = tooManyRequests
}

// MARK: - Remove cases
Expand All @@ -153,32 +171,32 @@ public extension HTTPStatusCode {
/// No longer used. Originally meant "Subsequent requests should use the specified proxy."
///
/// - seealso: [Original draft](https://tools.ietf.org/html/draft-cohen-http-305-306-responses-00)
@available(*, unavailable, message="No longer used")
static let SwitchProxy = __Unavailable
@available(*, unavailable, message: "No longer used")
static let switchProxy = __Unavailable

/// Authentication Timeout: 419
///
/// Removed from Wikipedia page.
@available(*, unavailable, message="No longer available")
static let AuthenticationTimeout = __Unavailable
@available(*, unavailable, message: "No longer available")
static let authenticationTimeout = __Unavailable

/// Method Failure: 419
///
/// A deprecated response used by the Spring Framework when a method has failed.
///
/// - seealso: [Spring Framework: HttpStatus enum documentation - `METHOD_FAILURE`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/HttpStatus.html#METHOD_FAILURE)
@available(*, unavailable, message="Deprecated")
static let SpringFrameworkMethodFailure = __Unavailable
@available(*, unavailable, message: "Deprecated")
static let springFrameworkMethodFailure = __Unavailable

/// Request Header Too Large: 494
///
/// Removed and replaced with `RequestHeaderFieldsTooLarge` - 431
@available(*, unavailable, renamed="RequestHeaderFieldsTooLarge", message="Changed to a 431 status code")
static let RequestHeaderTooLarge = __Unavailable
@available(*, unavailable, renamed: "requestHeaderFieldsTooLarge", message: "Changed to a 431 status code")
static let requestHeaderTooLarge = __Unavailable

/// Network Timeout Error: 599
///
/// Removed from Wikipedia page.
@available(*, unavailable, message="No longer available")
static let NetworkTimeoutError = __Unavailable
@available(*, unavailable, message: "No longer available")
static let networkTimeoutError = __Unavailable
}

0 comments on commit 4fb6e34

Please sign in to comment.