require 'rubygems' require 'net/http' require 'timeout' require 'uri' require 'json' require 'timer' class Eotb @@api_key = nil METHODS = [:to_actor, :to_subject, :to_json, :to_hash, :inspect] def self.configure(*params) @@host = "beta.beholder.ragnarson.com" @@port = '80' @@api_key = params[0] if params.size > 0 @@reset = 100 if params.size == 1 @@uri = URI.parse('http://' + @@host + ':' + @@port + '/events/' + @@api_key) @@post = Net::HTTP::Post.new(@@uri.path) elsif params.size == 4 # DEPRECATED - remove on Eotb 0.5.19 @@host = params[2] @@port = params[3] params[1].to_i >= 1 ? @@reset = params[1].to_i : @@reset = 1 @@uri = URI.parse('http://' + @@host + ':' + @@port + '/events/' + @@api_key) @@post = Net::HTTP::Post.new(@@uri.path) puts 'DEPRECATION WARNING' puts 'Eotb.configure(api_key, events, host, port) -- method will be removed in eotb 0.5.19' elsif params.size == 3 # DEPRECATED - remove on Eotb 0.5.19 @@uri = URI.parse('http://' + @@host + ':' + @@port + '/events/' + @@api_key) @@post = Net::HTTP::Post.new(@@uri.path) @@login = params[1] @@pass = params[2] @@post.basic_auth params[1], params[2] puts 'DEPRECATION WARNING' puts 'Eotb.configure(api_key) -- method will be removed in eotb 0.5.19' else raise ArgumentError, 'Wrong number of arguments. Check Eotb.configure parameters!' end @@events = {} @@counter = 1 Thread.new do Timer.every(600.0) { |elapsed| unless @@events.empty? @@counter = @@reset send end } end unless ENV['RAILS_ENV'] == 'test' end def self.api_key @@api_key end def self.host_port [@@host, @@port] end def self.login_password [@@login, @@pass] end def self.events @@events end def self.register_event(*args) case args.size when 1 self._json_register_event(*args) when 2,3 self._base_register_event(*args) else raise ArgumentError, "wrong number of arguments (#{args.size} for 1,2 or 3)" end end def self._json_register_event(json) json = JSON.parse json args = [json["actor"],json["action"]] args << json["subject"] if json["subject"] self._base_register_event(*args) end def self._base_register_event(actor, action, subject = {}) unless ENV['RAILS_ENV'] == 'test' action = { "event#{@@counter}[action]" => action.to_s } event = (hash_format(actor, :actor)).merge(action).merge(hash_format(subject, :subject)) @@events.merge! event if (@@counter == @@reset) send else @@counter += 1 end end end def self.send @@post.set_form_data(@@events) @@counter = 1 @@events = {} begin Timeout::timeout(2) { Net::HTTP.new(@@uri.host, @@uri.port).start.request(@@post) } rescue => e logger = Logger.new("log/#{ENV['RAILS_ENV']}.log") logger.error "ERROR eotb gem: " + e.to_s rescue Timeout::Error => e logger = Logger.new("log/#{ENV['RAILS_ENV']}.log") logger.error "ERROR eotb gem: " + e.to_s end end def self.value_format(value) value.send METHODS.find { |m| m if value.respond_to? m } end def self.hash_flatten(hash) hash.inject({}) do |h, (k, v)| if v.is_a?(Hash) hash_flatten(v).each do |sk, sv| h[[k] + sk] = sv end else k = k ? [k] : [] h[k] = v end h end end def self.hash_format(hash, type) if hash.is_a?(Hash) h = hash_flatten(hash).map do |k, v| key = k.map{ |e| "[#{e}]" }.join "\"event#{@@counter}[#{type}]#{key}\" => #{value_format(v)}" end.join(', ') else h = "\"event#{@@counter}[#{type}]\" => #{value_format(hash)}" end eval "{#{h.to_s}}" end end