Class: Circonus
- Inherits:
-
Object
- Object
- Circonus
- Defined in:
- lib/circonus.rb,
lib/circonus/values.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :timeout => 20, :open_timeout => 20 }
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
Instance Method Summary collapse
- #_rest(type, url, headers, data = nil) ⇒ Object
- #add(method, data) ⇒ Object
- #delete(method, id) ⇒ Object
- #get(method, id) ⇒ Object
-
#get_data(cid, metric, params = {}) ⇒ Object
extraction of time ranged data (this one is a bit different from the other v2 ones).
-
#initialize(apitoken, appname, agent, options = {}) ⇒ Circonus
constructor
A new instance of Circonus.
- #list(method, filter = nil) ⇒ Object
- #update(method, id, data) ⇒ Object
- #v1_add(method, data, params) ⇒ Object
- #v1_delete(method, id) ⇒ Object
- #v1_get(method, id) ⇒ Object
- #v1_list(method) ⇒ Object
Constructor Details
#initialize(apitoken, appname, agent, options = {}) ⇒ Circonus
Returns a new instance of Circonus.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/circonus.rb', line 26 def initialize(apitoken,appname,agent, ={}) @apitoken = apitoken @debug = true @raise_errors = false @appname = appname @agent = agent @headers = { "X-Circonus-Auth-Token" => @apitoken, "X-Circonus-App-Name" => @appname, "Accept" => 'application/json' } @url_v1_prefix = "https://circonus.com/api/json/" @url_prefix = "https://api.circonus.com/v2/" @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
14 15 16 |
# File 'lib/circonus.rb', line 14 def agent @agent end |
#debug ⇒ Object
Returns the value of attribute debug.
16 17 18 |
# File 'lib/circonus.rb', line 16 def debug @debug end |
#raise_errors ⇒ Object
Returns the value of attribute raise_errors.
15 16 17 |
# File 'lib/circonus.rb', line 15 def raise_errors @raise_errors end |
Instance Method Details
#_rest(type, url, headers, data = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/circonus.rb', line 42 def _rest(type,url,headers,data=nil) begin resource = RestClient::Resource.new url, :timeout => @options[:timeout], :open_timeout => @options[:open_timeout] case type when 'delete' r = resource.delete headers when 'post' r = resource.post Yajl::Encoder.encode(data), headers when 'put' r = resource.put Yajl::Encoder.encode(data), headers else 'get' r = resource.get headers end rescue RestClient::Forbidden,RestClient::BadRequest,RestClient::InternalServerError,RestClient::MethodNotAllowed => e err = Yajl::Parser.parse(e.response) print "Error (#{e.http_code}): ",err['error']," [#{e.http_body}]\n" if @debug raise if @raise_errors return nil,err rescue RestClient::RequestTimeout raise Circonus::Timeout end return r end |
#add(method, data) ⇒ Object
83 84 85 86 87 |
# File 'lib/circonus.rb', line 83 def add(method,data) r, err = _rest('post',@url_prefix + method, @headers, data) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#delete(method, id) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/circonus.rb', line 75 def delete(method,id) cid = id.to_s.split('/').last url = @url_prefix + method + '/' + cid r,err = _rest('delete',url, @headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#get(method, id) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/circonus.rb', line 66 def get(method,id) cid = id.to_s.split('/').last url = @url_prefix + method + '/' + cid #print "url=#{url}\n" r,err = _rest('get',url, @headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#get_data(cid, metric, params = {}) ⇒ Object
extraction of time ranged data (this one is a bit different from the other v2 ones)
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/circonus.rb', line 165 def get_data(cid,metric,params = {}) params['start'] = (Time.now - 3600).to_i unless params.has_key? 'start' params['end'] = Time.now.to_i unless params.has_key? 'end' params['period'] = 300 unless params.has_key? 'period' params['type'] = 'numeric' unless params.has_key? 'type' url = @url_prefix + 'data' + '/' + cid.split('/').last + '_' + CGI::escape(metric) #puts "url=#{url}" if @debug headers = @headers.merge({:params => params}) r,err = _rest('get',url, headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#list(method, filter = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/circonus.rb', line 96 def list(method,filter=nil) url = @url_prefix + method if (not filter.nil?) and filter.any? query_string = filter.map { |k,v| "f_#{CGI::escape(k)}=#{CGI::escape(v)}" }.join('&') url += '?' + query_string end r, err = _rest('get',url,@headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#update(method, id, data) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/circonus.rb', line 89 def update(method,id,data) cid = id.to_s.split('/').last r, err = _rest('put',@url_prefix + method + '/' + cid, @headers, data) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#v1_add(method, data, params) ⇒ Object
134 135 136 137 138 139 |
# File 'lib/circonus.rb', line 134 def v1_add(method, data, params) headers = @headers.merge({:params => params}) r,err = _rest 'get',@url_v1_prefix + 'add_' + method, headers, data return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#v1_delete(method, id) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/circonus.rb', line 125 def v1_delete(method,id) cid = id.to_s.split('/').last headers = @headers.merge({:params => {"#{method}_id".to_sym => cid}}) # attempt to guess at the id name expected url = @url_v1_prefix + 'delete_' + method r, err = _rest('delete',url, headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#v1_get(method, id) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/circonus.rb', line 114 def v1_get(method,id) cid = id.to_s.split('/').last headers = @headers.merge({:params => {"#{method}_id".to_sym => cid}}) # attempt to guess at the id name expected url = @url_v1_prefix + 'get_' + method #print "url=#{url}\n" #pp headers r, err = _rest('get',url, headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |
#v1_list(method) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/circonus.rb', line 107 def v1_list(method) url = @url_v1_prefix + 'list_' + method r, err = _rest('get',url,@headers) return nil,err if r.nil? return Yajl::Parser.parse(r) end |