Class: Dashboarder::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/dashboarder/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



3
4
5
6
7
8
# File 'lib/dashboarder/api.rb', line 3

def initialize(options={})
  @options = options
  @options[:api_url] = Config.librato_api_url || 'https://metrics-api.librato.com'
  @options[:api_user] = Config.librato_email
  @options[:api_pass] = Config.librato_key
end

Instance Method Details

#api_passObject



35
36
37
# File 'lib/dashboarder/api.rb', line 35

def api_pass
  @options[:api_pass]
end

#api_urlObject



27
28
29
# File 'lib/dashboarder/api.rb', line 27

def api_url
  @options[:api_url]
end

#api_userObject



31
32
33
# File 'lib/dashboarder/api.rb', line 31

def api_user
  @options[:api_user]
end

#connectObject



22
23
24
25
# File 'lib/dashboarder/api.rb', line 22

def connect
  uri = URI.parse(@options[:api_url])
  Excon.new(api_url, :headers => { 'Authorization' => 'Basic ' + [api_user.sub('%40', '@') + ':' + api_pass].pack('m').delete(Excon::CR_NL) })
end

#connectionObject



10
11
12
# File 'lib/dashboarder/api.rb', line 10

def connection
  @connection ||= connect
end

#get(path, options = {}) ⇒ Object



14
15
16
# File 'lib/dashboarder/api.rb', line 14

def get(path, options = {})
  JSON.load(connection.get(options.merge(:path => path, :idempotent => true)).body)
end

#post(path, body, options = {}) ⇒ Object



18
19
20
# File 'lib/dashboarder/api.rb', line 18

def post(path, body, options = {})
  JSON.load(connection.post(options.merge(:path => path, :body => body.to_json, :headers => { 'Content-Type' => 'application/json' })).body)
end