<commit_before>//
//  Contributions.swift
//  GithubPulse
//
//  Created by <NAME> on 12/30/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

class Contributions : NSObject, NSXMLParserDelegate {
  var year = [Int]()
  var commits = [Int]()
  var today = 0
  var streak = 0
  var succeeded = false
  
  class func fetch(username: String, completionBlock: ((Bool, [Int], Int, Int) -> Void)?) {
    Contributions().fetch(username, completionBlock)
  }
  
  func fetch(username: String, completionBlock: ((Bool, [Int], Int, Int) -> Void)!) {
    self.year = [];
    
    let url = NSURL(string: "https://github.com/users/\(username)/contributions")
    let request = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 30)
    
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
      if error != nil {
        self.succeeded = false
      } else {
        self.succeeded = true
        
        let parser = NSXMLParser(data: data)
        parser.delegate = self
        parser.parse()
        
        self.calculate()
      }
      
      completionBlock(self.succeeded, self.commits, self.streak, self.today)
    }
  }
  
  func calculate() {
    if !self.year.isEmpty {
      var length = self.year.count - 1
      self.today = self.year[length]
      self.streak = self.today > 0 ? 1 : 0
      self.commits = Array(self.year[length-29 ... length])
      
      for var i = length - 1; i >= 0 && self.year[i] > 0; i-- {
        self.streak++;
      }
      <commit_msg>Ignore all cache on contributions request, cache is dealth with manually<commit_after>//
//  Contributions.swift
//  GithubPulse
//
//  Created by <NAME> on 12/30/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

class Contributions : NSObject, NSXMLParserDelegate {
  var year = [Int]()
  var commits = [Int]()
  var today = 0
  var streak = 0
  var succeeded = false
  
  class func fetch(username: String, completionBlock: ((Bool, [Int], Int, Int) -> Void)?) {
    Contributions().fetch(username, completionBlock)
  }
  
  func fetch(username: String, completionBlock: ((Bool, [Int], Int, Int) -> Void)!) {
    self.year = [];
    
    let url = NSURL(string: "https://github.com/users/\(username)/contributions")
    let request = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
    
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
      if error != nil {
        self.succeeded = false
      } else {
        self.succeeded = true
        
        let parser = NSXMLParser(data: data)
        parser.delegate = self
        parser.parse()
        
        self.calculate()
      }
      
      completionBlock(self.succeeded, self.commits, self.streak, self.today)
    }
  }
  
  func calculate() {
    if !self.year.isEmpty {
      var length = self.year.count - 1
      self.today = self.year[length]
      self.streak = self.today > 0 ? 1 : 0
      self.commits = Array(self.year[length-29 ... length])
      
      for var i = length - 1; i >= 0 && self.year[i] > 0; i-- {
        self.streak++;
      }
      
{'fraction_non_alphanumeric': 0.13186481593240795, 'fraction_numerical': 0.01388050694025347, 'mean_word_length': 2.507936507936508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '1878871', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1020, 'n_words': 262}