Class: Sinatra::KontagentObject

Inherits:
Object
  • Object
show all
Defined in:
lib/sinmetrics/kontagent.rb

Defined Under Namespace

Classes: APIProxy

Constant Summary collapse

@@version =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options.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_keyObject

Returns the value of attribute api_key.



29
30
31
# File 'lib/sinmetrics/kontagent.rb', line 29

def api_key
  @api_key
end

#appObject (readonly)

Returns the value of attribute app.



28
29
30
# File 'lib/sinmetrics/kontagent.rb', line 28

def app
  @app
end

#envObject

Returns the value of attribute env.



29
30
31
# File 'lib/sinmetrics/kontagent.rb', line 29

def env
  @env
end

#secretObject

Returns the value of attribute secret.



29
30
31
# File 'lib/sinmetrics/kontagent.rb', line 29

def secret
  @secret
end

Instance Method Details

#base_urlObject



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.options.kontagent_request *args
  else
    @request.call *args
  end
end