Sha256: 3e9e136fed65971ef2c6b75f697c2931a2229d198b28f91f736374a4b37a3bdc
Contents?: true
Size: 1.53 KB
Versions: 1
Compression:
Stored size: 1.53 KB
Contents
require 'rubygems' require 'net/http' require 'uri' require 'json' class Eotb def self.configure(api_key, host = '127.0.0.1', port = '3000') @@uri = URI.parse('http://' + host + ':' + port + '/apps/' + api_key + '/events') @@post = Net::HTTP::Post.new(@@uri.path) @@api_key = { "event[app_id]" => api_key } end def self.register_event(actor, action, subject = {}) action = { "event[action]" => action.to_s } event = @@api_key.merge(hash_format(actor, :actor)).merge(action).merge(hash_format(subject, :subject)) @@post.set_form_data(event) Net::HTTP.new(@@uri.host, @@uri.port).start.request(@@post) end def self.value_format(object) if object.respond_to? :to_actor object.to_actor elsif object.respond_to? :to_subject object.to_subject elsif object.respond_to? :to_json object.to_json elsif object.respond_to? :to_hash object.to_hash else object.inspect end 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) a = hash_flatten(hash).map do |k, v| key = k.map{ |e| "[#{e}]" }.join "\"event[#{type}]#{key}\" => #{value_format(v)}" end a.join(', ') else a = "\"event[#{type}]\" => #{value_format(hash)}" end eval '{' + a + '}' end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
eotb-0.4.2 | lib/eotb.rb |