Class: Wavefront::Base
- Inherits:
-
Object
- Object
- Wavefront::Base
- Includes:
- Mixins, Validators
- Defined in:
- lib/wavefront-sdk/base.rb
Overview
Abstract class from which all API classes inherit. When you make any call to the Wavefront API from this SDK, you are returned an OpenStruct object.
Direct Known Subclasses
Alert, BaseWrite, CloudIntegration, Dashboard, DerivedMetric, Event, ExternalLink, Integration, MaintenanceWindow, Message, Metric, Notificant, Proxy, Query, SavedSearch, Search, Source, User, Webhook
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#net ⇒ Object
readonly
Returns the value of attribute net.
-
#noop ⇒ Object
readonly
Returns the value of attribute noop.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#update_keys ⇒ Object
readonly
Returns the value of attribute update_keys.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#api_base ⇒ String
Derive the first part of the API path from the class name.
-
#api_delete(path) ⇒ Hash
Make a DELETE call to the Wavefront API and return the result as a Ruby hash.
-
#api_get(path, query = {}) ⇒ Hash
Make a GET call to the Wavefront API and return the result as a Ruby hash.
- #api_path ⇒ Object
-
#api_post(path, body = nil, ctype = 'text/plain') ⇒ Hash
Make a POST call to the Wavefront API and return the result as a Ruby hash.
-
#api_put(path, body = nil, ctype = 'application/json') ⇒ Hash
Make a PUT call to the Wavefront API and return the result as a Ruby hash.
-
#everything ⇒ Object
Return all objects using a lazy enumerator.
-
#hash_for_update(old, new) ⇒ Hash
doing a PUT to update an object requires only a certain subset of the keys returned by #describe().
-
#initialize(creds = {}, opts = {}) ⇒ Nil
constructor
Create a new API object.
- #log(message, severity = :info) ⇒ Object
-
#mk_conn(path, headers = {}) ⇒ URI::HTTPS
Create a Faraday connection object.
-
#respond(resp) ⇒ Object
If we need to massage a raw response to fit what the Wavefront::Response class expects (I’m looking at you, ‘User’), a class can provide a #response_shim method.
-
#time_to_ms(time) ⇒ Ingeter
Convert an epoch timestamp into epoch milliseconds.
Methods included from Mixins
#parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?
Methods included from Validators
#wf_alert_id?, #wf_alert_severity?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_source_id?, #wf_string?, #wf_tag?, #wf_ts?, #wf_user_id?, #wf_value?, #wf_version?, #wf_webhook_id?
Constructor Details
#initialize(creds = {}, opts = {}) ⇒ Nil
Create a new API object. This will always be called from a class which inherits this one. If the inheriting class defines #post_initialize, that method will be called afterwards, with the same arguments.
46 47 48 49 50 51 52 53 54 |
# File 'lib/wavefront-sdk/base.rb', line 46 def initialize(creds = {}, opts = {}) @opts = opts @debug = opts[:debug] || false @noop = opts[:noop] || false @verbose = opts[:verbose] || false @logger = Wavefront::Logger.new(opts) setup_endpoint(creds) post_initialize(creds, opts) if respond_to?(:post_initialize) end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def conn @conn end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def debug @debug end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def logger @logger end |
#net ⇒ Object (readonly)
Returns the value of attribute net.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def net @net end |
#noop ⇒ Object (readonly)
Returns the value of attribute noop.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def noop @noop end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def opts @opts end |
#update_keys ⇒ Object (readonly)
Returns the value of attribute update_keys.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def update_keys @update_keys end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
25 26 27 |
# File 'lib/wavefront-sdk/base.rb', line 25 def verbose @verbose end |
Instance Method Details
#api_base ⇒ String
Derive the first part of the API path from the class name. You can override this in your class if you wish
78 79 80 |
# File 'lib/wavefront-sdk/base.rb', line 78 def api_base self.class.name.split('::').last.downcase end |
#api_delete(path) ⇒ Hash
Make a DELETE call to the Wavefront API and return the result as a Ruby hash. Can optionally perform a verbose noop, if the the information used to build the URI.
156 157 158 |
# File 'lib/wavefront-sdk/base.rb', line 156 def api_delete(path) make_call(mk_conn(path), :delete) end |
#api_get(path, query = {}) ⇒ Hash
Make a GET call to the Wavefront API and return the result as a Ruby hash. Can optionally perform a verbose noop, if the the information used to build the URI.
108 109 110 |
# File 'lib/wavefront-sdk/base.rb', line 108 def api_get(path, query = {}) make_call(mk_conn(path), :get, nil, query) end |
#api_path ⇒ Object
209 210 211 |
# File 'lib/wavefront-sdk/base.rb', line 209 def api_path ['', 'api', 'v2', api_base].uri_concat end |
#api_post(path, body = nil, ctype = 'text/plain') ⇒ Hash
Make a POST call to the Wavefront API and return the result as a Ruby hash. Can optionally perform a verbose noop, if the the information used to build the URI.
123 124 125 126 127 128 |
# File 'lib/wavefront-sdk/base.rb', line 123 def api_post(path, body = nil, ctype = 'text/plain') body = body.to_json unless body.is_a?(String) make_call(mk_conn(path, 'Content-Type': ctype, 'Accept': 'application/json'), :post, nil, body) end |
#api_put(path, body = nil, ctype = 'application/json') ⇒ Hash
Make a PUT call to the Wavefront API and return the result as a Ruby hash. Can optionally perform a verbose noop, if the the information used to build the URI.
141 142 143 144 145 |
# File 'lib/wavefront-sdk/base.rb', line 141 def api_put(path, body = nil, ctype = 'application/json') make_call(mk_conn(path, 'Content-Type': ctype, 'Accept': 'application/json'), :put, nil, body.to_json) end |
#everything ⇒ Object
Return all objects using a lazy enumerator
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/wavefront-sdk/base.rb', line 195 def everything Enumerator.new do |y| offset = 0 limit = 100 loop do resp = api_get('', offset: offset, limit: limit).response resp.items.map { |i| y.<< i } offset += limit raise StopIteration unless resp.moreItems == true end end.lazy end |
#hash_for_update(old, new) ⇒ Hash
doing a PUT to update an object requires only a certain subset of the keys returned by #describe(). This method takes the existing description of an object and turns it into a new has which can be PUT.
170 171 172 173 174 175 176 |
# File 'lib/wavefront-sdk/base.rb', line 170 def hash_for_update(old, new) raise ArgumentError unless old.is_a?(Hash) && new.is_a?(Hash) Hash[old.merge(new).map { |k, v| [k.to_sym, v] }].select do |k, _v| update_keys.include?(k) end end |
#log(message, severity = :info) ⇒ Object
56 57 58 |
# File 'lib/wavefront-sdk/base.rb', line 56 def log(, severity = :info) logger.log(, severity) end |
#mk_conn(path, headers = {}) ⇒ URI::HTTPS
Create a Faraday connection object. The server comes from the endpoint passed to the initializer in the ‘creds’ hash; the root of the URI is dynamically derived by the #setup_endpoint method.
90 91 92 93 94 95 |
# File 'lib/wavefront-sdk/base.rb', line 90 def mk_conn(path, headers = {}) url = format('https://%s%s', net[:endpoint], [net[:api_base], path].uri_concat) Faraday.new(url: Addressable::URI.encode(url), headers: net[:headers].merge(headers)) end |
#respond(resp) ⇒ Object
If we need to massage a raw response to fit what the Wavefront::Response class expects (I’m looking at you, ‘User’), a class can provide a #response_shim method.
182 183 184 185 186 187 188 189 190 |
# File 'lib/wavefront-sdk/base.rb', line 182 def respond(resp) body = if respond_to?(:response_shim) response_shim(resp.body, resp.status) else resp.body end Wavefront::Response.new(body, resp.status, opts) end |
#time_to_ms(time) ⇒ Ingeter
Convert an epoch timestamp into epoch milliseconds. If the timestamp looks like it’s already epoch milliseconds, return it as-is.
67 68 69 70 71 |
# File 'lib/wavefront-sdk/base.rb', line 67 def time_to_ms(time) return false unless time.is_a?(Integer) return time if time.to_s.size == 13 (time.to_f * 1000).round end |