Class: Wavefront::CoreApi

Inherits:
Object
  • Object
show all
Includes:
Mixins, Validators
Defined in:
lib/wavefront-sdk/core/api.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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#log, #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_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #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.



38
39
40
41
42
43
44
45
# File 'lib/wavefront-sdk/core/api.rb', line 38

def initialize(creds = {}, opts = {})
  @creds   = creds
  @opts    = opts
  @api     = setup_api(creds, opts)
  @s_api   = setup_api(creds, opts)
  @logger  = Wavefront::Logger.new(opts)
  post_initialize(creds, opts) if respond_to?(:post_initialize)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



18
19
20
# File 'lib/wavefront-sdk/core/api.rb', line 18

def api
  @api
end

#credsObject (readonly)

Returns the value of attribute creds.



18
19
20
# File 'lib/wavefront-sdk/core/api.rb', line 18

def creds
  @creds
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/wavefront-sdk/core/api.rb', line 18

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



18
19
20
# File 'lib/wavefront-sdk/core/api.rb', line 18

def opts
  @opts
end

#update_keysObject (readonly)

Returns the value of attribute update_keys.



18
19
20
# File 'lib/wavefront-sdk/core/api.rb', line 18

def update_keys
  @update_keys
end

Instance Method Details

#api_baseString

Derive the first part of the API path from the class name. You can override this in your class if you wish. This method is called by the ApiCaller class.



57
58
59
# File 'lib/wavefront-sdk/core/api.rb', line 57

def api_base
  self.class.name.split('::').last.downcase
end

#api_pathObject

The API path is normally /api/v2/something, but not always. Override this method if not



64
65
66
# File 'lib/wavefront-sdk/core/api.rb', line 64

def api_path
  ['', 'api', 'v2', api_base].uri_concat
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.

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
# File 'lib/wavefront-sdk/core/api.rb', line 91

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

#setup_api(creds, opts) ⇒ Object



47
48
49
# File 'lib/wavefront-sdk/core/api.rb', line 47

def setup_api(creds, opts)
  Wavefront::ApiCaller.new(self, creds, 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.



75
76
77
78
79
# File 'lib/wavefront-sdk/core/api.rb', line 75

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