Class: Hover::Client::Static

Inherits:
Hover show all
Defined in:
lib/hover/client/static.rb

Instance Attribute Summary collapse

Attributes inherited from HMAC

#client_id, #client_secret

Attributes inherited from HTTP

#prefix, #site

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hover

#json_delete, #json_get, #json_patch, #json_post, #json_put

Methods inherited from HMAC

#authenticate

Methods inherited from HTTP

#authenticate, #delete, #get, #get_redirect_location, #make_form_request, #make_request, #make_uri, #parse_response, #patch, #post, #put, #raise_errors, #raise_errors_for_cloudfront_response

Constructor Details

#initialize(application, environment, client_id, client_secret, site = 'http://localhost:3000', prefix = 'api/v1') ⇒ Static

Returns a new instance of Static.



9
10
11
12
13
14
# File 'lib/hover/client/static.rb', line 9

def initialize(application, environment, client_id, client_secret, site = 'http://localhost:3000', prefix = 'api/v1')
  super(client_id, client_secret, site, prefix)

  self.application = application
  self.environment = environment
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



7
8
9
# File 'lib/hover/client/static.rb', line 7

def application
  @application
end

#environmentObject

Returns the value of attribute environment.



7
8
9
# File 'lib/hover/client/static.rb', line 7

def environment
  @environment
end

Class Method Details

.param_name(*args) ⇒ Object



82
83
84
85
# File 'lib/hover/client/static.rb', line 82

def self.param_name(*args)
  first = args.shift
  "#{first}" + args.map{ |arg| "[#{arg}]" }.join
end

.tags_to_params(tags, parent_keys = ['metric', 'tags']) ⇒ Object

Class Methods



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hover/client/static.rb', line 66

def self.tags_to_params(tags, parent_keys = ['metric', 'tags'])
  params = {}

  tags.each do |key, value|
    if value.is_a?(Hash)
      params.merge!(tags_to_params(value, (parent_keys + [key])))
    elsif value.is_a?(Array)
      params["#{param_name(*parent_keys, key)}[]"] = value
    else
      params[param_name(*parent_keys, key)] = value
    end
  end

  params
end

Instance Method Details

#add_unique_identifier(parameters) ⇒ Object



55
56
57
58
59
60
# File 'lib/hover/client/static.rb', line 55

def add_unique_identifier(parameters)
  sorted_array = parameters.to_a.sort_by(&:first)
  metric_identifier = Digest::SHA256.hexdigest(sorted_array.to_json)

  parameters['metric[identifier]'] = metric_identifier
end

#alooma_urlObject



32
33
34
35
36
37
38
# File 'lib/hover/client/static.rb', line 32

def alooma_url
  @alooma_url ||= if Rails.env.staging?
                    "https://inputs.alooma.com/rest/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnROYW1lIjoiaG92ZXItc2YtMSIsImlucHV0TGFiZWwiOiJzdGF0aWNfc3RhZ2luZyIsImlucHV0VHlwZSI6IlJFU1RBUEkifQ.Iba3VM9GaOjzJEmUFPuHDA6oaQT7TBe9A0iMxfYW0x0"
                  elsif Rails.env.production?
                    "https://inputs.alooma.com/rest/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnROYW1lIjoiaG92ZXItc2YtMSIsImlucHV0TGFiZWwiOiJzdGF0aWNfcHJvZHVjdGlvbiIsImlucHV0VHlwZSI6IlJFU1RBUEkifQ.jiuWVLDPDvZSv4bmiy54GVEpOre19bDqr9ZC7Y-HKwo"
                  end
end

#create_metric(name:, value:, happened_at: Time.now.utc, tags: {}, remote_record_id: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hover/client/static.rb', line 16

def create_metric(name:, value:, happened_at: Time.now.utc, tags: {}, remote_record_id: nil)
  return unless alooma_url

  parameters = {
    "metric[name]" => name,
    'metric[value]' => value,
    'metric[happened_at]' => happened_at.to_s,
    'metric[environment]' => environment,
    'metric[application]' => application
  }.merge(self.class.tags_to_params(tags))
  parameters['metric[remote_record_id]'] = remote_record_id if remote_record_id
  add_unique_identifier(parameters)

  post_to_alooma(parameters, alooma_url)
end

#post_to_alooma(parameters, url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hover/client/static.rb', line 40

def post_to_alooma(parameters, url)
  uri = URI.parse(url)
  request = Net::HTTP::Post.new(uri)
  request.set_form_data(parameters)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if 'https' == uri.scheme
  response = http.request(request)

  unless (200 .. 299).include?(response.code.to_i)
    raise AloomaReportingError.new("Error reporting to alooma. #{response.inspect}")
  end

  response
end