Class: Eotb
- Inherits:
-
Object
- Object
- Eotb
- Defined in:
- lib/eotb.rb
Defined Under Namespace
Classes: Report
Constant Summary collapse
- METHODS =
[:to_actor, :to_subject, :to_json, :to_hash, :inspect]
- @@api_key =
nil
Class Method Summary collapse
- ._base_register_event(actor, action, subject = {}) ⇒ Object
- ._json_register_event(json) ⇒ Object
- .api_key ⇒ Object
- .configure(*params) ⇒ Object
- .events ⇒ Object
- .hash_flatten(hash) ⇒ Object
- .hash_format(hash, type) ⇒ Object
- .host_port ⇒ Object
- .login_password ⇒ Object
- .register_event(*args) ⇒ Object
- .send ⇒ Object
- .value_format(value) ⇒ Object
Class Method Details
._base_register_event(actor, action, subject = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/eotb.rb', line 91 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 |
._json_register_event(json) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/eotb.rb', line 84 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 |
.api_key ⇒ Object
60 61 62 |
# File 'lib/eotb.rb', line 60 def self.api_key @@api_key end |
.configure(*params) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/eotb.rb', line 13 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 |
.events ⇒ Object
69 70 71 |
# File 'lib/eotb.rb', line 69 def self.events @@events end |
.hash_flatten(hash) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/eotb.rb', line 126 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 |
.hash_format(hash, type) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/eotb.rb', line 140 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 |
.host_port ⇒ Object
63 64 65 |
# File 'lib/eotb.rb', line 63 def self.host_port [@@host, @@port] end |
.login_password ⇒ Object
66 67 68 |
# File 'lib/eotb.rb', line 66 def self.login_password [@@login, @@pass] end |
.register_event(*args) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/eotb.rb', line 73 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 |
.send ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/eotb.rb', line 105 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 |
.value_format(value) ⇒ Object
122 123 124 |
# File 'lib/eotb.rb', line 122 def self.value_format(value) value.send METHODS.find { |m| m if value.respond_to? m } end |