Last active
January 4, 2017 03:05
-
-
Save kaiobrito/3628a303d33f5902153c3bc6672c8da3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension SearchableWebService { | |
func search(term: String, handler: SearchResponseHandler? = nil) { | |
let url = self.searchURL(withTerm: term) | |
let task = URLSession.shared.dataTask(with: url) { data, _ , _ in | |
if let data = data { | |
let json = JSON(data: data) | |
handler?(json["items"].arrayValue.map(T.init)) | |
} else { | |
handler?(nil) | |
} | |
} | |
task.resume() | |
} | |
} | |
extension SingleObjectWebService { | |
func getBy(key: String, response: SingleObjectResponse? = nil) { | |
let url = getURL(withKey: key) | |
let task = URLSession.shared.dataTask(with: url) { data, _ , _ in | |
if let data = data { | |
let json = JSON(data: data) | |
response?(T.init(json: json)) | |
} else { | |
response?(nil) | |
} | |
} | |
task.resume() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol SearchableWebService { | |
typealias SearchResponseHandler = (([T]?) -> Void) | |
associatedtype T: JSONMappable | |
func searchURL(withTerm term: String) -> URL | |
func search(term: String, handler: SearchResponseHandler?) | |
} | |
protocol SingleObjectWebService { | |
associatedtype T: JSONMappable | |
typealias SingleObjectResponse = (T?) -> Void | |
func getURL(withKey key: String) -> URL | |
func getBy(key: String, response: SingleObjectResponse?) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GHRepositoryWebService: SearchableWebService { | |
typealias T = GHRepository | |
func searchURL(withTerm term: String) -> URL { | |
return URL(string:"https://api.github.com/search/repositories?q=\(term)")! | |
} | |
} | |
class GHOrganizationWebService: SingleObjectWebService { | |
typealias T = GHOrganization | |
func getURL(withKey key: String) -> URL { | |
return URL(string: "https://api.github.com/orgs/\(key)")! | |
} | |
} | |
class GHUserWebService: SearchableWebService, SingleObjectWebService { | |
typealias T = GHUser | |
func searchURL(withTerm term: String) -> URL { | |
return URL(string: "https://api.github.com/search/users?q=\(term)")! | |
} | |
func getURL(withKey key: String) -> URL { | |
return URL(string: "https://api.github.com/users/\(key)")! | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol JSONMappable { | |
init(json: JSON) | |
} | |
struct GHUser: JSONMappable { | |
var login: String | |
var id: String | |
init(json: JSON) { | |
self.login = json["login"].stringValue | |
self.id = json["id"].stringValue | |
} | |
} | |
struct GHRepository: JSONMappable { | |
var id: String | |
var name: String | |
init(json: JSON) { | |
self.name = json["name"].stringValue | |
self.id = json["id"].stringValue | |
} | |
} | |
struct GHOrganization: JSONMappable { | |
var id: String | |
var login: String | |
init(json: JSON) { | |
self.id = json["id"].stringValue | |
self.login = json["login"].stringValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment