<commit_before>//
//  Trends.swift
//  GoSquared
//
//  Created by <NAME> on 27/11/2015.
//  Copyright © 2015 Go Squared Ltd. All rights reserved.
//

import Foundation

public class Trends {

    private let key: String
    private let token: String
    private let client: GoSquaredAPI
    private let baseURL: String

    public init(client: GoSquaredAPI) {
        self.key = client.key
        self.token = client.token
        self.client = client
        self.baseURL = "\(GoSquaredAPI.baseURL)/trends/v2"
    }

    lazy var dateFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter
    }()

    public func aggregate(from: NSDate, to: NSDate, completionHandler: GoSquaredAPI.Handler) -> NSURLSessionDataTask? {
        let safeFrom = dateFormatter.stringFromDate(from).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
        let safeTo = dateFormatter.stringFromDate(to).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
        let url = NSURL(string: "\(baseURL)/aggregate/?api_key=\(key)&site_token=\(token)&from=\(safeFrom)&to=\(safeTo)&interval=hour")!

        return client.get(url, handler: completionHandler)
    }

    public func aggregate(from: NSDate, to: NSDate) -> GoSquaredAPI.CombiningFunction {
        return GoSquaredAPI.CombiningFunction(endpoint: "aggregate", params: [
            "from": dateFormatter.stringFromDate(from),
            "to": dateFormatter.stringFromDate(to),
            "dateFormat": "YYYY-MM-DD HH:mm:ss",
            "interval": "hour"
        ])
    }

    public func executeCombiningFunction(functions: [GoSquaredAPI.CombiningFunction], completionHandler: GoSquaredAPI.Handler) -> NSURLSessionDataTask? {
        let funcs: [(name: String, params: String)] = functions.enumerate().map { idx, fn in
            let name = "\(fn.endpoint):\(idx)"
            let params = fn.params.map({ key, val -> String in<commit_msg>add page combining function<commit_after>//
//  Trends.swift
//  GoSquared
//
//  Created by <NAME> on 27/11/2015.
//  Copyright © 2015 Go Squared Ltd. All rights reserved.
//

import Foundation

public class Trends {

    private let key: String
    private let token: String
    private let client: GoSquaredAPI
    private let baseURL: String

    public init(client: GoSquaredAPI) {
        self.key = client.key
        self.token = client.token
        self.client = client
        self.baseURL = "\(GoSquaredAPI.baseURL)/trends/v2"
    }

    lazy var dateFormatter: NSDateFormatter = {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter
    }()

    public func aggregate(from: NSDate, to: NSDate, completionHandler: GoSquaredAPI.Handler) -> NSURLSessionDataTask? {
        let safeFrom = dateFormatter.stringFromDate(from).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
        let safeTo = dateFormatter.stringFromDate(to).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
        let url = NSURL(string: "\(baseURL)/aggregate/?api_key=\(key)&site_token=\(token)&from=\(safeFrom)&to=\(safeTo)&interval=hour")!

        return client.get(url, handler: completionHandler)
    }

    public func aggregate(from: NSDate, to: NSDate) -> GoSquaredAPI.CombiningFunction {
        return GoSquaredAPI.CombiningFunction(endpoint: "aggregate", params: [
            "from": dateFormatter.stringFromDate(from),
            "to": dateFormatter.stringFromDate(to),
            "dateFormat": "YYYY-MM-DD HH:mm:ss",
            "interval": "hour"
        ])
    }

    public func page(from: NSDate, to: NSDate) -> GoSquaredAPI.CombiningFunction {
        return GoSquaredAPI.CombiningFunction(endpoint: "page", params: [
            "from": dateFormatter.stringFromDate(from),
            "to": dateFormatter.stringFromDate(to),
            "dateFormat": "YYYY-MM-DD HH:mm:ss",
            "limit": "0,3"
        ])
    }

    public func executeCombiningFunction(functions: [GoSquaredAPI.CombiningFunction], completionHandler: GoSquaredAPI.Handler) -> NSURLSessionDataTask? {
        let funcs: [(name: String, params: String)] = functions.enumerate().map { idx, fn in
            let name = "\(fn.endpoint):\(idx)"
            let params = fn.params.map({ key, val -> String in
{'fraction_non_alphanumeric': 0.11936280008974647, 'fraction_numerical': 0.006282252636302446, 'mean_word_length': 3.1547064305684995, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 14, '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': '5919357', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1304, 'n_words': 284}