Skip to content

Instantly share code, notes, and snippets.

@DreamingInBinary
Created September 2, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DreamingInBinary/d58434f1ba1e7e7fe4de6ca2a7ce8088 to your computer and use it in GitHub Desktop.
Save DreamingInBinary/d58434f1ba1e7e7fe4de6ca2a7ce8088 to your computer and use it in GitHub Desktop.
//
// 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