<commit_before>//
//  WhisperRequestManager.swift
//  NUSWhispers
//
//  Created by jin on 28/4/15.
//  Copyright (c) 2015 crypt. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
import KILabel
import SVProgressHUD

class WhisperRequestManager {

    var requestManager: Alamofire.Manager?
    var delegate: WhisperRequestManagerDelegate?
    let whisperCountPerRequest: Int = 25

    class var sharedInstance : WhisperRequestManager {
        struct Singleton {
            static let instance = WhisperRequestManager()
        }
        return Singleton.instance
    }

    private init() {
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        config.timeoutIntervalForRequest = 30
        self.requestManager = Alamofire.Manager(configuration: config)
    }

    func requestForWhisper(tag: Int) {
        let url = "http://nuswhispers.com/api/confessions/\(tag)"
        makeGetRequest(url)
    }

    func requestForWhispers(section: Section, offset: Int = 0) {
        let url = "http://nuswhispers.com/api/confessions/\(section.apiEndpoint)?count=\(whisperCountPerRequest)&offset=\(offset)"
        makeGetRequest(url)
    }

    func hashtagLinkTapHandler(delegate: WhisperRequestManagerDelegate) -> ((KILabel, String, NSRange) -> ()) {
        let handler: ((KILabel, String, NSRange) -> ()) = { (label: KILabel, string: String, range: NSRange) in
            let tag = string.substringWithRange(Range<String.Index>(start: advance(string.startIndex, 1), end: string.endIndex)).toInt()
            if let tag = tag {
                WhisperRequestManager.sharedInstance.delegate = delegate
                WhisperRequestManager.sharedInstance.requestForWhisper(tag)
                SVProgressHUD.show()
            }
        }
        return handler
    }

    func urlLinkTapHandler(delegate: WhisperRequestManagerDelegate) -> ((KILabel, String, NSRange) -> ()) {
        let handler: ((KILabel, String, NSRange) -> ()) = { (label: KILabel, string: String, range: NSRange) in
            UIApplication.sharedApplication().openURL(NSURL(string: string)!)
        }
        return handler
    }

    private func makeGetRequest(urlString: String) {
        let req = NSMutableURLRequest(URL: NSURL(string: urlString)!)
        req.HTTPBody = nil
        req.HTTPMethod = "GET"
        req.addValue("0", forHTTPHeaderField: "Content-Length")
        requestManager!.request(req).responseJSON { (req, resp, json, error) in
            if let e = error {
                println(e)
            } else {
                self.updateWhisperDataSource(JSON(json!))
            }
        }<commit_msg>makeGetRequest in RequestManager now takes in a completion block. Added method to pull NUSWhispers Leaderboard data.<commit_after>//
//  WhisperRequestManager.swift
//  NUSWhispers
//
//  Created by jin on 28/4/15.
//  Copyright (c) 2015 crypt. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON
import KILabel
import SVProgressHUD

class WhisperRequestManager {

    var requestManager: Alamofire.Manager?
    var delegate: WhisperRequestManagerDelegate?
    let whisperCountPerRequest: Int = 25

    class var sharedInstance : WhisperRequestManager {
        struct Singleton {
            static let instance = WhisperRequestManager()
        }
        return Singleton.instance
    }

    private init() {
        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        config.timeoutIntervalForRequest = 30
        self.requestManager = Alamofire.Manager(configuration: config)
    }

    func requestForWhisper(tag: Int) {
        let url = "http://nuswhispers.com/api/confessions/\(tag)"
        makeGetRequest(url) { (json: JSON) in
            self.updateWhisperDataSource(json)
        }
    }

    func requestForWhispers(section: Section, offset: Int = 0) {
        let url = "http://nuswhispers.com/api/confessions/\(section.apiEndpoint)?count=\(whisperCountPerRequest)&offset=\(offset)"
        makeGetRequest(url) { (json: JSON) in
            self.updateWhisperDataSource(json)
        }
    }

    func hashtagLinkTapHandler(delegate: WhisperRequestManagerDelegate) -> ((KILabel, String, NSRange) -> ()) {
        let handler: ((KILabel, String, NSRange) -> ()) = { (label: KILabel, string: String, range: NSRange) in
            let tag = string.substringWithRange(Range<String.Index>(start: advance(string.startIndex, 1), end: string.endIndex)).toInt()
            if let tag = tag {
                WhisperRequestManager.sharedInstance.delegate = delegate
                WhisperRequestManager.sharedInstance.requestForWhisper(tag)
                SVProgressHUD.show()
            }
        }
        return handler
    }

    func urlLinkTapHandler(delegate: WhisperRequestManagerDelegate) -> ((KILabel, String, NSRange) -> ()) {
        let handler: ((KILabel, String, NSRange) -> ()) = { (label: KILabel, string: String, range: NSRange) in
            UIApplication.sharedApplication().openURL(NSURL(string: string)!)
        }
        return handler
    }

    func requestForLeaderboard() {
        let url = "http://yangshun.im/nuswhispers-leaderboard/leaderboard/may.json"
        makeGetRequest(url) { (json: JSON) in
            println(json)
        }
    }

    private func makeGetRequest(urlString: String, completion: (json: JSON) -> ()?) {
        let req = NSMutableURLRequest(URL: NSURL(string: urlString)!)
        req.HTTPBody = nil
        req.HTTPMethod = "GET"
        req.addValue("0", forHTTPHeaderField: "Content-Length")
        requestManager!.request(req).responseJSON { (req, resp, json, error) in
            if let e = error {
                println(e)
            } else {
                completion(json: JSON(json!))
            }
        }
{'fraction_non_alphanumeric': 0.10699517574086836, 'fraction_numerical': 0.005513439007580979, 'mean_word_length': 2.6555415617128464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3817638', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1653, 'n_words': 361}