Created
September 2, 2016 19:38
-
-
Save DreamingInBinary/d58434f1ba1e7e7fe4de6ca2a7ce8088 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// IGNNetworking.swift | |
// Unofficial Metacritic | |
// | |
// Created by Jordan Morgan on 8/22/16. | |
// Copyright © 2016 Dreaming In Binary, LLC. All rights reserved. | |
// | |
import Foundation | |
class IGNNetworking : Networking | |
{ | |
private static let apiKey = "redacted" | |
private static let apiURLTop = "https://newsapi.org/v1/articles?source=ign&sortBy=top&apiKey=" + IGNNetworking.apiKey | |
private static let apiURLLatest = "https://newsapi.org/v1/articles?source=ign&sortBy=latest&apiKey=" + IGNNetworking.apiKey | |
private static let topArticlesURL = NSURL(string: IGNNetworking.apiURLTop)! | |
private static let latestArticlesURL = NSURL(string: IGNNetworking.apiURLLatest)! | |
//MARK: Networking Calls | |
func getTopArticles(completion:([IGNArticleStub]?, NetworkError?) -> ()) | |
{ | |
GET(with: IGNNetworking.topArticlesURL) { info, error in | |
self.handleArticleResponse(info, error: error, completion: completion) | |
} | |
} | |
func getLatestArticles(completion:([IGNArticleStub]?, NetworkError?) -> ()) | |
{ | |
GET(with: IGNNetworking.latestArticlesURL) { info, error in | |
self.handleArticleResponse(info, error: error, completion: completion) | |
} | |
} | |
//MARK: Helpers | |
private func handleArticleResponse(info:NSDictionary?, error:NetworkError?, completion:([IGNArticleStub]?, NetworkError?) -> ()) | |
{ | |
guard error == nil else | |
{ | |
return completion(nil, error) | |
} | |
guard let data = info, articleData = data["articles"] as? NSArray else | |
{ | |
return completion(nil, error) | |
} | |
let articles = articleData.flatMap { IGNArticleStub(dictionary: $0 as! JSONDictionary)} | |
completion(articles, nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment