Class: Circonus::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/circonus-api.rb,
lib/circonus-api/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name = nil, api_token = nil) ⇒ Api



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/circonus-api.rb', line 8

def initialize(app_name=nil, api_token=nil)
  app_name  ||= ENV['CIRCONUS_APP_NAME']
  api_token ||= ENV['CIRCONUS_API_TOKEN']

  @resource = RestClient::Resource.new('https://api.circonus.com',
    :headers => {
      :x_circonus_app_name => app_name,
      :x_circonus_auth_token => api_token,
      :accept => 'application/json'
    }
  )
end

Class Method Details

.resource(plural_name, singular_name = plural_name.gsub(/s$/, '')) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/circonus-api.rb', line 21

def self.resource( plural_name, singular_name = plural_name.gsub(/s$/, '') )
  define_method("list_#{plural_name}")     {            get("/#{singular_name}")              }
  define_method("show_#{singular_name}")   { |id|       get("/#{singular_name}/#{id}")        }
  define_method("create_#{singular_name}") { |data|     post("/#{singular_name}", data)       }
  define_method("update_#{singular_name}") { |id, data| put("/#{singular_name}/#{id}", data)  }
  define_method("delete_#{singular_name}") { |id|       delete("/#{singular_name}/#{id}")     }
end

Instance Method Details

#delete(path, params = {}) ⇒ Object



52
53
54
# File 'lib/circonus-api.rb', line 52

def delete(path, params={})
  request(:delete, path, params)
end

#deserialize(json_string) ⇒ Object



67
68
69
# File 'lib/circonus-api.rb', line 67

def deserialize(json_string)
  JSON.parse(json_string)
end

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



40
41
42
# File 'lib/circonus-api.rb', line 40

def get(path, params={})
  request(:get, path, params)
end

#post(path, params = {}) ⇒ Object



48
49
50
# File 'lib/circonus-api.rb', line 48

def post(path, params={})
  request(:post, path, params.to_json)
end

#put(path, params = {}) ⇒ Object



44
45
46
# File 'lib/circonus-api.rb', line 44

def put(path, params={})
  request(:put, path, params.to_json)
end

#request(method, path, params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/circonus-api.rb', line 56

def request(method, path, params)
  begin
    response = @resource[URI.escape(path)].send(method, params)
    result = deserialize(response)
  rescue RestClient::Exception => e
    result = deserialize(e.http_body)
  end

  return result
end