<commit_before>require 'rest_client'
require 'json'

class Pusher

  class << self
    attr_accessor :host, :port
    attr_writer :key, :secret
  end

  self.host = 'api.pusherapp.com'
  self.port = 80

  def self.[](channel_id)
    @channels ||= {}
    @channels[channel_id.to_s] ||= Channel.new(@key, channel_id)
  end

  class Channel
    def initialize(key, id)
      @http = RestClient::Resource.new(
        "http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}"
      )
    end

    def trigger(event_name, data)
      begin
        @http.post(:event => JSON.generate({
          :event => event_name,
          :data => data
        }))
      rescue StandardError => e
        handle_error e
      end
    end

    private

      def handle_error(e)
        puts e.inspect
      end

  end

end<commit_msg>Changed API request to send JSON string as body<commit_after>require 'json'

class Pusher

  class << self
    attr_accessor :host, :port
    attr_writer :key, :secret
  end

  self.host = 'api.pusherapp.com'
  self.port = 80

  def self.[](channel_id)
    @channels ||= {}
    @channels[channel_id.to_s] ||= Channel.new(@key, channel_id)
  end

  class Channel
    def initialize(key, id)
      @uri = URI.parse("http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}")
      @http = Net::HTTP.new(@uri.host, @uri.port)
    end

    def trigger(event_name, data)
      begin
        @http.post( 
          @uri.path, 
          self.class.turn_into_json({
            :event => event_name,
            :data => data
            }),
          {'Content-Type'=> 'application/json'}
        )
      rescue StandardError => e
        handle_error e
      end
    end
    
    def self.turn_into_json(data)
      j = if Object.const_defined?('ActiveSupport')
        data.to_json
      else
        JSON.generate(data)
      end
      j
    end

    private

    def handle_error(e)
      puts e.inspect
    end

  end

end
{'fraction_non_alphanumeric': 0.12724474089276552, 'fraction_numerical': 0.002052334530528476, 'mean_word_length': 2.0854430379746836, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4460128', 'n_tokens_mistral': 663, 'n_tokens_neox': 656, 'n_words': 141}