<commit_before>require 'rubygems'
require 'ruby-growl'
require 'open-uri'
require 'json'
require 'yaml'

module RcrNotify
  class Notifier
    attr_reader :settings, :last_projects
    
    def initialize
      @settings = {:interval => 30}
      @growl = Growl.new "localhost", "run>code>run notifier", %w(success failure)
    end
    
    def poll
      print '.'
      data = JSON::parse open(url).read
      
      projects = data["user"]["projects"].inject({}) do |hash, project|
        hash.merge project.delete("name") => project
      end

      if last_projects
        projects.each do |name, this_time|
          notify_for name, last_projects[name], this_time
        end
      end
      
      @last_projects = projects
    end
    
    private
    
    def notify_for(name, last_time, this_time)
      return if this_time['commit'] == last_time['commit']

      commit_message = "#{this_time['commit_message']}\n" +
                       "\t\t—#{this_time['author_name']}"
        
      title = if success?(this_time)
        success?(last_time) ? 'success' : 'fixed'
      else
        success?(last_time) ? 'broken' : 'still broken'
      end
      
      notify "#{name} build #{title}", commit_message, success?(this_time)
    end
    
    def success?(hash)
      hash && hash["status"] == "success"
    end
        
    def url
      "http://runcoderun.com/api/v1/json/#{settings[:username]}"
    end
  
    def notify(title, body, success)<commit_msg>handle spotty internet<commit_after>require 'rubygems'
require 'ruby-growl'
require 'open-uri'
require 'json'
require 'yaml'

module RcrNotify
  class Notifier
    attr_reader :settings, :last_projects
    
    def initialize
      @settings = {:interval => 30}
      @growl = Growl.new "localhost", "run>code>run notifier", %w(success failure)
    end
    
    def poll
      data = JSON::parse open(url).read
      print '.'

      
      projects = data["user"]["projects"].inject({}) do |hash, project|
        hash.merge project.delete("name") => project
      end

      if last_projects
        projects.each do |name, this_time|
          notify_for name, last_projects[name], this_time
        end
      end
      
      @last_projects = projects
    rescue SocketError
      print 'o'
    end
    
    private
    
    def notify_for(name, last_time, this_time)
      return if this_time['commit'] == last_time['commit']

      commit_message = "#{this_time['commit_message']}\n" +
                       "\t\t—#{this_time['author_name']}"
        
      title = if success?(this_time)
        success?(last_time) ? 'success' : 'fixed'
      else
        success?(last_time) ? 'broken' : 'still broken'
      end
      
      notify "#{name} build #{title}", commit_message, success?(this_time)
    end
    
    def success?(hash)
      hash && hash["status"] == "success"
    end
        
    def url
      "http://runcoderun.com/api/v1/json/#{settings[:username]}"
    end
  
    def notify(title, body, success)
{'fraction_non_alphanumeric': 0.12670890296765588, 'fraction_numerical': 0.0020006668889629878, 'mean_word_length': 2.34075723830735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5785814', 'n_tokens_mistral': 935, 'n_tokens_neox': 931, 'n_words': 215}