Class: Sinatra::KontagentObject
- Inherits:
-
Object
- Object
- Sinatra::KontagentObject
- Defined in:
- lib/sinmetrics/kontagent.rb
Defined Under Namespace
Classes: APIProxy
Constant Summary collapse
- @@version =
1
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #base_url ⇒ Object
-
#initialize(app) ⇒ KontagentObject
constructor
A new instance of KontagentObject.
- #make_url(method, args = {}) ⇒ Object
- #request(*args) ⇒ Object
Constructor Details
#initialize(app) ⇒ KontagentObject
Returns a new instance of KontagentObject.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sinmetrics/kontagent.rb', line 14 def initialize app if app.respond_to?(:options) @app = app [:api_key, :secret, :env].each do |var| instance_variable_set("@#{var}", app..send("kontagent_#{var}")) end else @request = Proc.new { |url| Net::HTTP.get( URI.parse(url) ) } [:api_key, :secret, :env, :request].each do |var| instance_variable_set("@#{var}", app[var]) if app.has_key?(var) end end end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
29 30 31 |
# File 'lib/sinmetrics/kontagent.rb', line 29 def api_key @api_key end |
#app ⇒ Object (readonly)
Returns the value of attribute app.
28 29 30 |
# File 'lib/sinmetrics/kontagent.rb', line 28 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
29 30 31 |
# File 'lib/sinmetrics/kontagent.rb', line 29 def env @env end |
#secret ⇒ Object
Returns the value of attribute secret.
29 30 31 |
# File 'lib/sinmetrics/kontagent.rb', line 29 def secret @secret end |
Instance Method Details
#base_url ⇒ Object
65 66 67 68 69 70 |
# File 'lib/sinmetrics/kontagent.rb', line 65 def base_url case env when :production ; 'http://api.geo.kontagent.net/api' else ; 'http://api.test.kontagent.com/api' end end |
#make_url(method, args = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sinmetrics/kontagent.rb', line 72 def make_url method, args = {} args = { :ts => Time.now.getgm.strftime("%Y-%m-%dT%H:%M:%S") }.merge(args) sorted = args.map{ |k,v| next nil unless v next nil if k == :url_only "#{k}=" + case v when Array v.join('%2C') else v.to_s end }.compact.sort sorted << "an_sig=" + Digest::MD5.hexdigest(sorted.join+self.secret) query = sorted.map{|v| v.gsub('&', '%26').gsub(' ', '+')}.join('&') "#{base_url}/v#{@@version}/#{api_key}/#{method}/?#{query}" end |
#request(*args) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/sinmetrics/kontagent.rb', line 30 def request *args if @app @app..kontagent_request *args else @request.call *args end end |