Skip to content

Commit 4fb6e34

Browse files
authored
Merge pull request #6 from rhodgkins/swift-3.0
Swift 3.0 support
2 parents 5d0b066 + 295db53 commit 4fb6e34

15 files changed

Lines changed: 537 additions & 431 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode7.3
2+
osx_image: xcode8
33
script:
44
# Workaround for Simulator getting stuck
55
# See: https://github.com/travis-ci/travis-ci/issues/3040#issuecomment-146700203

HTTPStatusCodes.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'HTTPStatusCodes'
3-
s.version = '3.0.0'
3+
s.version = '3.1.0'
44
s.license = 'MIT'
55
s.summary = 'Swift enum wrapper for easier handling of HTTP status codes'
66
s.homepage = 'https://github.com/rhodgkins/SwiftHTTPStatusCodes'

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ If this library is out of date compared to this page please open an issue and I
1616

1717
## Usage
1818

19+
Swift 3.0 support is added in version 3.1 of this framework. For use with older versions of Swift use version 3.0.
20+
1921
### Carthage
2022

2123
`Cartfile`:
2224
```ogdl
23-
github "rhodgkins/SwiftHTTPStatusCodes" ~> 3.0
25+
github "rhodgkins/SwiftHTTPStatusCodes" ~> 3.1
2426
```
2527
Source code:
2628
```swift
@@ -30,7 +32,7 @@ import HTTPStatusCodes
3032
### CocoaPods
3133
`Podfile`:
3234
```ruby
33-
pod 'HTTPStatusCodes', '~> 3.0.0'
35+
pod 'HTTPStatusCodes', '~> 3.1.0'
3436
```
3537
Source code:
3638

Sources/HTTPStatusCodes+Extensions.swift

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ import Foundation
1111
public extension HTTPStatusCode {
1212
/// Informational - Request received, continuing process.
1313
public var isInformational: Bool {
14-
return inRange(100...199)
14+
return isIn(range: 100...199)
1515
}
1616
/// Success - The action was successfully received, understood, and accepted.
1717
public var isSuccess: Bool {
18-
return inRange(200...299)
18+
return isIn(range: 200...299)
1919
}
2020
/// Redirection - Further action must be taken in order to complete the request.
2121
public var isRedirection: Bool {
22-
return inRange(300...399)
22+
return isIn(range: 300...399)
2323
}
2424
/// Client Error - The request contains bad syntax or cannot be fulfilled.
2525
public var isClientError: Bool {
26-
return inRange(400...499)
26+
return isIn(range: 400...499)
2727
}
2828
/// Server Error - The server failed to fulfill an apparently valid request.
2929
public var isServerError: Bool {
30-
return inRange(500...599)
30+
return isIn(range: 500...599)
3131
}
3232

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

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

@@ -59,15 +59,23 @@ extension HTTPStatusCode: CustomDebugStringConvertible, CustomStringConvertible
5959
public extension HTTPStatusCode {
6060

6161
/// Obtains a possible status code from an optional HTTP URL response.
62-
public init?(HTTPResponse: NSHTTPURLResponse?) {
62+
public init?(HTTPResponse: HTTPURLResponse?) {
6363
guard let statusCodeValue = HTTPResponse?.statusCode else {
6464
return nil
6565
}
6666
self.init(statusCodeValue)
6767
}
68+
69+
/// This is declared as it's not automatically picked up by the complier for the above init
70+
private init?(_ rawValue: Int) {
71+
guard let value = HTTPStatusCode(rawValue: rawValue) else {
72+
return nil
73+
}
74+
self = value
75+
}
6876
}
6977

70-
public extension NSHTTPURLResponse {
78+
public extension HTTPURLResponse {
7179

7280
/**
7381
* Marked internal to expose (as `statusCodeValue`) for Objective-C interoperability only.
@@ -93,52 +101,62 @@ public extension NSHTTPURLResponse {
93101
*
94102
* - returns: the instance of the object, or `nil` if an error occurred during initialization.
95103
*/
96-
@available(iOS, introduced=7.0)
104+
@available(iOS, introduced: 7.0)
97105
@objc(initWithURL:statusCodeValue:HTTPVersion:headerFields:)
98-
public convenience init?(URL url: NSURL, statusCode: HTTPStatusCode, HTTPVersion: String?, headerFields: [String : String]?) {
99-
self.init(URL: url, statusCode: statusCode.rawValue, HTTPVersion: HTTPVersion, headerFields: headerFields)
106+
public convenience init?(url: URL, statusCode: HTTPStatusCode, HTTPVersion: String?, headerFields: [String : String]?) {
107+
self.init(url: url, statusCode: statusCode.rawValue, httpVersion: HTTPVersion, headerFields: headerFields)
100108
}
101109
}
102110

103111
// MARK: - Deprecated cases
104112

105113
public extension HTTPStatusCode {
106114

107-
@available(*, deprecated, renamed="PayloadTooLarge")
108-
static let RequestEntityTooLarge = PayloadTooLarge
115+
/// - deprecated: Renamed to `payloadTooLarge`
116+
@available(*, deprecated, renamed: "payloadTooLarge")
117+
static let requestEntityTooLarge = payloadTooLarge
109118

110-
@available(*, deprecated, renamed="URITooLong")
111-
static let RequestURITooLong = URITooLong
119+
/// - deprecated: Renamed to `uriTooLong`
120+
@available(*, deprecated, renamed: "uriTooLong")
121+
static let requestURITooLong = uriTooLong
112122

113-
@available(*, deprecated, renamed="RangeNotSatisfiable")
114-
static let RequestedRangeNotSatisfiable = RangeNotSatisfiable
123+
/// - deprecated: Renamed to `rangeNotSatisfiable`
124+
@available(*, deprecated, renamed: "rangeNotSatisfiable")
125+
static let requestedRangeNotSatisfiable = rangeNotSatisfiable
115126

116-
@available(*, deprecated, renamed="IISLoginTimeout")
117-
static let LoginTimeout = IISLoginTimeout
127+
/// - deprecated: Renamed to `iisLoginTimeout`
128+
@available(*, deprecated, renamed: "iisLoginTimeout")
129+
static let loginTimeout = iisLoginTimeout
118130

119-
@available(*, deprecated, renamed="IISRetryWith")
120-
static let RetryWith = IISRetryWith
131+
/// - deprecated: Renamed to `iisRetryWith`
132+
@available(*, deprecated, renamed: "iisRetryWith")
133+
static let retryWith = iisRetryWith
121134

122-
@available(*, deprecated, renamed="NginxNoResponse")
123-
static let NoResponse = NginxNoResponse
135+
/// - deprecated: Renamed to `nginxNoResponse`
136+
@available(*, deprecated, renamed: "nginxNoResponse")
137+
static let noResponse = nginxNoResponse
124138

125-
@available(*, deprecated, renamed="NginxSSLCertificateError")
126-
static let CertError = NginxSSLCertificateError
139+
/// - deprecated: Renamed to `nginxSSLCertificateError`
140+
@available(*, deprecated, renamed: "nginxSSLCertificateError")
141+
static let certError = nginxSSLCertificateError
127142

128-
@available(*, deprecated, renamed="NginxSSLCertificateRequired")
129-
static let NoCert = NginxSSLCertificateRequired
143+
/// - deprecated: Renamed to `nginxSSLCertificateRequired`
144+
@available(*, deprecated, renamed: "nginxSSLCertificateRequired")
145+
static let noCert = nginxSSLCertificateRequired
130146

131-
@available(*, deprecated, renamed="NginxHTTPToHTTPS")
132-
static let HTTPToHTTPS = NginxHTTPToHTTPS
147+
/// - deprecated: Renamed to `nginxHTTPToHTTPS`
148+
@available(*, deprecated, renamed: "nginxHTTPToHTTPS")
149+
static let httpToHTTPS = nginxHTTPToHTTPS
133150

134-
@available(*, deprecated, renamed="NginxClientClosedRequest")
135-
static let ClientClosedRequest = NginxClientClosedRequest
151+
/// - deprecated: Renamed to `nginxClientClosedRequest`
152+
@available(*, deprecated, renamed: "nginxClientClosedRequest")
153+
static let clientClosedRequest = nginxClientClosedRequest
136154

137-
/// 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.
155+
/// 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.
138156
///
139157
/// - seealso: [Twitter Error Codes & Responses](https://dev.twitter.com/docs/error-codes-responses)
140-
@available(*, deprecated, renamed="TooManyRequests")
141-
static let TwitterEnhanceYourCalm = TooManyRequests
158+
@available(*, deprecated, renamed: "tooManyRequests")
159+
static let twitterEnhanceYourCalm = tooManyRequests
142160
}
143161

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

159177
/// Authentication Timeout: 419
160178
///
161179
/// Removed from Wikipedia page.
162-
@available(*, unavailable, message="No longer available")
163-
static let AuthenticationTimeout = __Unavailable
180+
@available(*, unavailable, message: "No longer available")
181+
static let authenticationTimeout = __Unavailable
164182

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

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

179197
/// Network Timeout Error: 599
180198
///
181199
/// Removed from Wikipedia page.
182-
@available(*, unavailable, message="No longer available")
183-
static let NetworkTimeoutError = __Unavailable
200+
@available(*, unavailable, message: "No longer available")
201+
static let networkTimeoutError = __Unavailable
184202
}

0 commit comments

Comments
 (0)