Skip to content

Commit

Permalink
Adopt some new Swift naming schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
cargath committed Oct 29, 2016
1 parent 8e7209a commit 8fa6594
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 199 deletions.
40 changes: 20 additions & 20 deletions MarvelKit/MarvelKit/Extensions/NSURLSession+Tasks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import Foundation

public enum MarvelKitError: Swift.Error {

case jsonObjectConvertibleError(msg: String)
case nsjsonSerializationError(msg: String)
case nsurlSessionError(msg: String)
case nsurlError(msg: String)
case JSONObjectConvertibleError(msg: String)
case JSONSerializationError(msg: String)
case URLSessionError(msg: String)
case URLError(msg: String)

}

public extension MarvelKitError {

var description: String {
switch self {
case let .jsonObjectConvertibleError(msg):
case let .JSONObjectConvertibleError(msg):
return msg
case let .nsjsonSerializationError(msg):
case let .JSONSerializationError(msg):
return msg
case let .nsurlSessionError(msg):
case let .URLSessionError(msg):
return msg
case let .nsurlError(msg):
case let .URLError(msg):
return msg
}
}
Expand All @@ -41,26 +41,26 @@ public extension URLSession {
public func dataTaskWithURL(_ url: URL, successHandler: @escaping (URLResponse?, Data) -> Void, errorHandler: @escaping ErrorHandler) -> URLSessionDataTask {
return dataTask(with: url, completionHandler: { data, response, error in
if let error = error {
return errorHandler(response, .nsurlSessionError(msg: error.localizedDescription))
return errorHandler(response, .URLSessionError(msg: error.localizedDescription))
} else if let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
if let data = data {
return successHandler(response, data)
} else {
return errorHandler(response, .nsurlSessionError(msg: "Failed to receive data"))
return errorHandler(response, .URLSessionError(msg: "Failed to receive data"))
}
} else {
if let
data = data,
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? JSONObject,
let error = Error(jsonObject: jsonObject) {
return errorHandler(response, .nsurlSessionError(msg: "Failed with status code '\(response.statusCode)'; Error: '\(error)'"))
let JSONObject = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? JSONObject,
let error = Error(JSONObject: JSONObject) {
return errorHandler(response, .URLSessionError(msg: "Failed with status code '\(response.statusCode)'; Error: '\(error)'"))
} else {
return errorHandler(response, .nsurlSessionError(msg: "Failed with status code '\(response.statusCode)'"))
return errorHandler(response, .URLSessionError(msg: "Failed with status code '\(response.statusCode)'"))
}
}
} else {
return errorHandler(nil, .nsurlSessionError(msg: "Failed to receive response"))
return errorHandler(nil, .URLSessionError(msg: "Failed to receive response"))
}
})
}
Expand All @@ -71,20 +71,20 @@ public extension URLSession {
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? JSONObject {
return successHandler(response, jsonObject)
} else {
return errorHandler(response, .nsjsonSerializationError(msg: "Failed to deserialize JSON"))
return errorHandler(response, .JSONSerializationError(msg: "Failed to deserialize JSON"))
}
} catch {
return errorHandler(response, .nsjsonSerializationError(msg: "\(error)"))
return errorHandler(response, .JSONSerializationError(msg: "\(error)"))
}
}, errorHandler: errorHandler)
}

public func resourceTaskWithURL<Resource: ResourceProtocol & DataProtocol>(_ url: URL, successHandler: @escaping (URLResponse?, DataWrapper<DataContainer<Resource>>) -> Void, errorHandler: @escaping ErrorHandler) -> URLSessionTask {
return JSONTaskWithURL(url, successHandler: { response, jsonObject in
if let resource = DataWrapper<DataContainer<Resource>>(jsonObject: jsonObject) {
return JSONTaskWithURL(url, successHandler: { response, JSONObject in
if let resource = DataWrapper<DataContainer<Resource>>(JSONObject: JSONObject) {
return successHandler(response, resource)
} else {
return errorHandler(response, .jsonObjectConvertibleError(msg: "Failed to initialize resource with JSON object"))
return errorHandler(response, .JSONObjectConvertibleError(msg: "Failed to initialize resource with JSON object"))
}
}, errorHandler: errorHandler)
}
Expand Down
2 changes: 1 addition & 1 deletion MarvelKit/MarvelKit/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class Request<Resource: DataResourceProtocol> {
open func exec(success successHandler: @escaping (DataWrapper<DataContainer<Resource>>) -> Void, error errorHandler: @escaping (MarvelKitError) -> Void) {

guard let url = self.url else {
return errorHandler(.nsurlError(msg: "Failed to create URL for resource"))
return errorHandler(.URLError(msg: "Failed to create URL for resource"))
}

defaultSession.resourceTaskWithURL(url, successHandler: { response, resource in
Expand Down
2 changes: 1 addition & 1 deletion MarvelKit/MarvelKit/Request/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol ResourceProtocol {

static var name: String { get }

init?(jsonObject: JSONObject)
init?(JSONObject: JSONObject)

}

Expand Down
24 changes: 12 additions & 12 deletions MarvelKit/MarvelKit/Response/Characters/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ public struct Character: DataProtocol {

extension Character {

public init?(jsonObject: JSONObject) {
self.id = jsonObject["id"] as? Int
self.name = jsonObject["name"] as? String
self.description = jsonObject["description"] as? String
self.modified = jsonObject["modified"] as? String
self.resourceURI = jsonObject["resourceURI"] as? String
self.urls = Url.fromJSONArray(jsonObject["urls"] as? JSONArray)
self.thumbnail = Image(jsonObject: jsonObject["thumbnail"] as? JSONObject)
self.comics = ComicList(jsonObject: jsonObject["comics"] as? JSONObject)
self.stories = StoryList(jsonObject: jsonObject["stories"] as? JSONObject)
self.events = EventList(jsonObject: jsonObject["events"] as? JSONObject)
self.series = SeriesList(jsonObject: jsonObject["series"] as? JSONObject)
public init?(JSONObject: JSONObject) {
self.id = JSONObject["id"] as? Int
self.name = JSONObject["name"] as? String
self.description = JSONObject["description"] as? String
self.modified = JSONObject["modified"] as? String
self.resourceURI = JSONObject["resourceURI"] as? String
self.urls = Url.from(JSONArray: JSONObject["urls"] as? JSONArray)
self.thumbnail = Image(JSONObject: JSONObject["thumbnail"] as? JSONObject)
self.comics = ComicList(JSONObject: JSONObject["comics"] as? JSONObject)
self.stories = StoryList(JSONObject: JSONObject["stories"] as? JSONObject)
self.events = EventList(JSONObject: JSONObject["events"] as? JSONObject)
self.series = SeriesList(JSONObject: JSONObject["series"] as? JSONObject)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public struct CharacterSummary: SummaryProtocol {

extension CharacterSummary {

public init?(jsonObject: JSONObject) {
self.resourceURI = jsonObject["resourceURI"] as? String
self.name = jsonObject["name"] as? String
self.role = jsonObject["role"] as? String
public init?(JSONObject: JSONObject) {
self.resourceURI = JSONObject["resourceURI"] as? String
self.name = JSONObject["name"] as? String
self.role = JSONObject["role"] as? String
}

}
Expand Down
60 changes: 30 additions & 30 deletions MarvelKit/MarvelKit/Response/Comics/Comic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,36 @@ public struct Comic: DataProtocol {

extension Comic {

public init?(jsonObject: JSONObject) {
self.id = jsonObject["id"] as? Int
self.digitalId = jsonObject["digitalId"] as? Int
self.title = jsonObject["title"] as? String
self.issueNumber = jsonObject["issueNumber"] as? Double
self.variantDescription = jsonObject["variantDescription"] as? String
self.description = jsonObject["description"] as? String
self.modified = jsonObject["modified"] as? String
self.isbn = jsonObject["isbn"] as? String
self.upc = jsonObject["upc"] as? String
self.diamondCode = jsonObject["diamondCode"] as? String
self.ean = jsonObject["ean"] as? String
self.issn = jsonObject["issn"] as? String
self.format = jsonObject["format"] as? String
self.pageCount = jsonObject["pageCount"] as? Int
self.textObjects = TextObject.fromJSONArray(jsonObject["textObjects"] as? JSONArray)
self.resourceURI = jsonObject["resourceURI"] as? String
self.urls = Url.fromJSONArray(jsonObject["urls"] as? JSONArray)
self.series = SeriesSummary(jsonObject: jsonObject["series"] as? JSONObject)
self.variants = ComicSummary.fromJSONArray(jsonObject["variants"] as? JSONArray)
self.collections = ComicSummary.fromJSONArray(jsonObject["collections"] as? JSONArray)
self.collectedIssues = ComicSummary.fromJSONArray(jsonObject["collectedIssues"] as? JSONArray)
self.dates = ComicDate.fromJSONArray(jsonObject["dates"] as? JSONArray)
self.prices = ComicPrice.fromJSONArray(jsonObject["prices"] as? JSONArray)
self.thumbnail = Image(jsonObject: jsonObject["thumbnail"] as? JSONObject)
self.images = Image.fromJSONArray(jsonObject["images"] as? JSONArray)
self.creators = CreatorList(jsonObject: jsonObject["creators"] as? JSONObject)
self.characters = CharacterList(jsonObject: jsonObject["characters"] as? JSONObject)
self.stories = StoryList(jsonObject: jsonObject["stories"] as? JSONObject)
self.events = EventList(jsonObject: jsonObject["events"] as? JSONObject)
public init?(JSONObject: JSONObject) {
self.id = JSONObject["id"] as? Int
self.digitalId = JSONObject["digitalId"] as? Int
self.title = JSONObject["title"] as? String
self.issueNumber = JSONObject["issueNumber"] as? Double
self.variantDescription = JSONObject["variantDescription"] as? String
self.description = JSONObject["description"] as? String
self.modified = JSONObject["modified"] as? String
self.isbn = JSONObject["isbn"] as? String
self.upc = JSONObject["upc"] as? String
self.diamondCode = JSONObject["diamondCode"] as? String
self.ean = JSONObject["ean"] as? String
self.issn = JSONObject["issn"] as? String
self.format = JSONObject["format"] as? String
self.pageCount = JSONObject["pageCount"] as? Int
self.textObjects = TextObject.from(JSONArray: JSONObject["textObjects"] as? JSONArray)
self.resourceURI = JSONObject["resourceURI"] as? String
self.urls = Url.from(JSONArray: JSONObject["urls"] as? JSONArray)
self.series = SeriesSummary(JSONObject: JSONObject["series"] as? JSONObject)
self.variants = ComicSummary.from(JSONArray: JSONObject["variants"] as? JSONArray)
self.collections = ComicSummary.from(JSONArray: JSONObject["collections"] as? JSONArray)
self.collectedIssues = ComicSummary.from(JSONArray: JSONObject["collectedIssues"] as? JSONArray)
self.dates = ComicDate.from(JSONArray: JSONObject["dates"] as? JSONArray)
self.prices = ComicPrice.from(JSONArray: JSONObject["prices"] as? JSONArray)
self.thumbnail = Image(JSONObject: JSONObject["thumbnail"] as? JSONObject)
self.images = Image.from(JSONArray: JSONObject["images"] as? JSONArray)
self.creators = CreatorList(JSONObject: JSONObject["creators"] as? JSONObject)
self.characters = CharacterList(JSONObject: JSONObject["characters"] as? JSONObject)
self.stories = StoryList(JSONObject: JSONObject["stories"] as? JSONObject)
self.events = EventList(JSONObject: JSONObject["events"] as? JSONObject)
}

}
Expand Down
6 changes: 3 additions & 3 deletions MarvelKit/MarvelKit/Response/Comics/ComicDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public struct ComicDate {

extension ComicDate: JSONObjectConvertible {

public init?(jsonObject: JSONObject) {
self.type = jsonObject["type"] as? String
self.date = jsonObject["date"] as? String
public init?(JSONObject: JSONObject) {
self.type = JSONObject["type"] as? String
self.date = JSONObject["date"] as? String
}

}
6 changes: 3 additions & 3 deletions MarvelKit/MarvelKit/Response/Comics/ComicPrice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public struct ComicPrice {

extension ComicPrice: JSONObjectConvertible {

public init?(jsonObject: JSONObject) {
self.type = jsonObject["type"] as? String
self.price = jsonObject["price"] as? Float
public init?(JSONObject: JSONObject) {
self.type = JSONObject["type"] as? String
self.price = JSONObject["price"] as? Float
}

}
6 changes: 3 additions & 3 deletions MarvelKit/MarvelKit/Response/Comics/ComicSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public struct ComicSummary: SummaryProtocol {

extension ComicSummary: JSONObjectConvertible {

public init?(jsonObject: JSONObject) {
self.resourceURI = jsonObject["resourceURI"] as? String
self.name = jsonObject["name"] as? String
public init?(JSONObject: JSONObject) {
self.resourceURI = JSONObject["resourceURI"] as? String
self.name = JSONObject["name"] as? String
}

}
Expand Down
30 changes: 15 additions & 15 deletions MarvelKit/MarvelKit/Response/Creators/Creator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,21 @@ public struct Creator: DataProtocol {

extension Creator {

public init?(jsonObject: JSONObject) {
self.id = jsonObject["id"] as? Int
self.firstName = jsonObject["firstName"] as? String
self.middleName = jsonObject["middleName"] as? String
self.lastName = jsonObject["lastName"] as? String
self.suffix = jsonObject["suffix"] as? String
self.fullName = jsonObject["fullName"] as? String
self.modified = jsonObject["modified"] as? String
self.resourceURI = jsonObject["resourceURI"] as? String
self.urls = Url.fromJSONArray(jsonObject["urls"] as? JSONArray)
self.thumbnail = Image(jsonObject: jsonObject["thumbnail"] as? JSONObject)
self.series = SeriesList(jsonObject: jsonObject["series"] as? JSONObject)
self.stories = StoryList(jsonObject: jsonObject["stories"] as? JSONObject)
self.comics = ComicList(jsonObject: jsonObject["comics"] as? JSONObject)
self.events = EventList(jsonObject: jsonObject["events"] as? JSONObject)
public init?(JSONObject: JSONObject) {
self.id = JSONObject["id"] as? Int
self.firstName = JSONObject["firstName"] as? String
self.middleName = JSONObject["middleName"] as? String
self.lastName = JSONObject["lastName"] as? String
self.suffix = JSONObject["suffix"] as? String
self.fullName = JSONObject["fullName"] as? String
self.modified = JSONObject["modified"] as? String
self.resourceURI = JSONObject["resourceURI"] as? String
self.urls = Url.from(JSONArray: JSONObject["urls"] as? JSONArray)
self.thumbnail = Image(JSONObject: JSONObject["thumbnail"] as? JSONObject)
self.series = SeriesList(JSONObject: JSONObject["series"] as? JSONObject)
self.stories = StoryList(JSONObject: JSONObject["stories"] as? JSONObject)
self.comics = ComicList(JSONObject: JSONObject["comics"] as? JSONObject)
self.events = EventList(JSONObject: JSONObject["events"] as? JSONObject)
}

}
Expand Down
8 changes: 4 additions & 4 deletions MarvelKit/MarvelKit/Response/Creators/CreatorSummary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public struct CreatorSummary: SummaryProtocol {

extension CreatorSummary {

public init?(jsonObject: JSONObject) {
self.resourceURI = jsonObject["resourceURI"] as? String
self.name = jsonObject["name"] as? String
self.role = jsonObject["role"] as? String
public init?(JSONObject: JSONObject) {
self.resourceURI = JSONObject["resourceURI"] as? String
self.name = JSONObject["name"] as? String
self.role = JSONObject["role"] as? String
}

}
Expand Down
12 changes: 6 additions & 6 deletions MarvelKit/MarvelKit/Response/DataContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public struct DataContainer<DataType: DataProtocol>: DataContainerProtocol {

extension DataContainer {

public init?(jsonObject: JSONObject) {
self.offset = jsonObject["offset"] as? Int
self.limit = jsonObject["limit"] as? Int
self.total = jsonObject["total"] as? Int
self.count = jsonObject["count"] as? Int
self.results = DataType.fromJSONArray(jsonObject["results"] as? JSONArray)
public init?(JSONObject: JSONObject) {
self.offset = JSONObject["offset"] as? Int
self.limit = JSONObject["limit"] as? Int
self.total = JSONObject["total"] as? Int
self.count = JSONObject["count"] as? Int
self.results = DataType.from(JSONArray: JSONObject["results"] as? JSONArray)
}

}
16 changes: 8 additions & 8 deletions MarvelKit/MarvelKit/Response/DataWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public struct DataWrapper<ContainerType: DataContainerProtocol>: DataWrapperProt

extension DataWrapper: JSONObjectConvertible {

public init?(jsonObject: JSONObject) {
self.code = jsonObject["code"] as? Int
self.status = jsonObject["status"] as? String
self.copyright = jsonObject["copyright"] as? String
self.attributionText = jsonObject["attributionText"] as? String
self.attributionHTML = jsonObject["attributionHTML"] as? String
self.data = ContainerType(jsonObject: jsonObject["data"] as? JSONObject)
self.etag = jsonObject["etag"] as? String
public init?(JSONObject: JSONObject) {
self.code = JSONObject["code"] as? Int
self.status = JSONObject["status"] as? String
self.copyright = JSONObject["copyright"] as? String
self.attributionText = JSONObject["attributionText"] as? String
self.attributionHTML = JSONObject["attributionHTML"] as? String
self.data = ContainerType(JSONObject: JSONObject["data"] as? JSONObject)
self.etag = JSONObject["etag"] as? String
}

}
6 changes: 3 additions & 3 deletions MarvelKit/MarvelKit/Response/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public struct Error {

extension Error: JSONObjectConvertible {

public init?(jsonObject: JSONObject) {
public init?(JSONObject: JSONObject) {

guard let
message = jsonObject["message"] as? String,
let code = jsonObject["code"] as? String else {
message = JSONObject["message"] as? String,
let code = JSONObject["code"] as? String else {

return nil
}
Expand Down
Loading

0 comments on commit 8fa6594

Please sign in to comment.