Class: Vault::Usage::Client
- Inherits:
-
Object
- Object
- Vault::Usage::Client
- Defined in:
- lib/vault-usage-client.rb,
lib/vault-usage-client/client.rb,
lib/vault-usage-client/version.rb
Overview
Client for the Vault::Usage
HTTP API.
Defined Under Namespace
Classes: InvalidTimeError
Constant Summary collapse
- VERSION =
The
Vault::Usage::Client
gem version. '1.0.0'.freeze
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#close_app_ownership_event(event_id, user_hid, app_hid, stop_time) ⇒ Object
Report that ownership of an app by a user stopped at a particular time.
-
#close_usage_event(event_id, product_name, consumer_hid, stop_time) ⇒ Object
Report that usage of a product, by a user or app, stopped at a particular time.
-
#initialize(url = nil) ⇒ Client
constructor
Instantiate a client.
-
#open_app_ownership_event(event_id, user_hid, app_hid, start_time) ⇒ Object
Report that ownership of an app by a user started at a particular time.
-
#open_close_usage_event(event_id, product_name, consumer_hid, start_time, stop_time, detail = nil) ⇒ Object
Report that usage of a product, by a user or app, started at a particular time.
-
#open_dynos_for_app(app_hid) ⇒ Array
Get the open dyno usage events for the specified app.
-
#open_usage_event(event_id, product_name, consumer_hid, start_time, detail = nil) ⇒ Object
Report that usage of a product, by a user or app, started at a particular time.
-
#usage_for_event(event_id) ⇒ Array
Request a single usage event.
-
#usage_for_user(user_hid, start_time, stop_time, exclude = nil, callback_url = nil, snapshot = false) ⇒ Array
Get the usage events for the apps owned by the specified user during the specified period.
-
#usage_for_user_by_product(user_hid, product_name) ⇒ Array
Get the usage events for a product specific to a user.
Constructor Details
#initialize(url = nil) ⇒ Client
Instantiate a client.
14 15 16 |
# File 'lib/vault-usage-client/client.rb', line 14 def initialize(url = nil) @url = url || ENV['VAULT_USAGE_URL'] end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/vault-usage-client/client.rb', line 4 def url @url end |
Instance Method Details
#close_app_ownership_event(event_id, user_hid, app_hid, stop_time) ⇒ Object
Report that ownership of an app by a user stopped at a particular time.
309 310 311 312 313 314 315 316 317 |
# File 'lib/vault-usage-client/client.rb', line 309 def close_app_ownership_event(event_id, user_hid, app_hid, stop_time) unless stop_time.zone.eql?('UTC') raise InvalidTimeError.new('Stop time must be in UTC.') end path = "/users/#{user_hid}/apps/#{app_hid}/close/#{event_id}" + "/#{iso_format(stop_time)}" connection = Excon.new(@url) connection.put(path: path, expects: [201]) end |
#close_usage_event(event_id, product_name, consumer_hid, stop_time) ⇒ Object
Report that usage of a product, by a user or app, stopped at a particular time.
67 68 69 70 71 72 73 74 75 |
# File 'lib/vault-usage-client/client.rb', line 67 def close_usage_event(event_id, product_name, consumer_hid, stop_time) unless stop_time.zone.eql?('UTC') raise InvalidTimeError.new('Stop time must be in UTC.') end path = "/products/#{product_name}/usage/#{consumer_hid}" + "/events/#{event_id}/close/#{iso_format(stop_time)}" connection = Excon.new(@url) connection.put(path: path, expects: [201]) end |
#open_app_ownership_event(event_id, user_hid, app_hid, start_time) ⇒ Object
Report that ownership of an app by a user started at a particular time.
287 288 289 290 291 292 293 294 295 |
# File 'lib/vault-usage-client/client.rb', line 287 def open_app_ownership_event(event_id, user_hid, app_hid, start_time) unless start_time.zone.eql?('UTC') raise InvalidTimeError.new('Start time must be in UTC.') end path = "/users/#{user_hid}/apps/#{app_hid}/open/#{event_id}" + "/#{iso_format(start_time)}" connection = Excon.new(@url) connection.put(path: path, expects: [201]) end |
#open_close_usage_event(event_id, product_name, consumer_hid, start_time, stop_time, detail = nil) ⇒ Object
Report that usage of a product, by a user or app, started at a particular time.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vault-usage-client/client.rb', line 97 def open_close_usage_event(event_id, product_name, consumer_hid, start_time, stop_time, detail=nil) unless start_time.zone.eql?('UTC') raise InvalidTimeError.new('Start time must be in UTC.') end unless stop_time.zone.eql?('UTC') raise InvalidTimeError.new('Stop time must be in UTC.') end path = "/products/#{product_name}/usage/#{consumer_hid}" + "/events/#{event_id}/open/#{iso_format(start_time)}" + "/close/#{iso_format(stop_time)}" unless detail.nil? headers = {'Content-Type' => 'application/json'} body = MultiJson.dump(detail) end connection = Excon.new(@url) connection.put(path: path, headers: headers, body: body, expects: [201]) end |
#open_dynos_for_app(app_hid) ⇒ Array
Get the open dyno usage events for the specified app
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/vault-usage-client/client.rb', line 260 def open_dynos_for_app(app_hid) path = "/apps/#{app_hid}/ps/open" query = {} connection = Excon.new(@url) response = connection.get(path: path, expects: [200], query: query) payload = MultiJson.load(response.body, {symbolize_keys: true}) events = payload[:events] events.each do |event| event.each do |key, value| event[key] = parse_date(value) if date?(value) end end end |
#open_usage_event(event_id, product_name, consumer_hid, start_time, detail = nil) ⇒ Object
Report that usage of a product, by a user or app, started at a particular time.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vault-usage-client/client.rb', line 37 def open_usage_event(event_id, product_name, consumer_hid, start_time, detail=nil) unless start_time.zone.eql?('UTC') raise InvalidTimeError.new('Start time must be in UTC.') end path = "/products/#{product_name}/usage/#{consumer_hid}" + "/events/#{event_id}/open/#{iso_format(start_time)}" unless detail.nil? headers = {'Content-Type' => 'application/json'} body = MultiJson.dump(detail) end connection = Excon.new(@url) connection.put(path: path, headers: headers, body: body, expects: [201]) end |
#usage_for_event(event_id) ⇒ Array
Request a single usage event.
133 134 135 136 137 138 139 140 141 |
# File 'lib/vault-usage-client/client.rb', line 133 def usage_for_event(event_id) path = "/usage_events/#{event_id}" connection = Excon.new(@url) response = connection.get(path: path, expects: [200]) event = MultiJson.load(response.body, {symbolize_keys: true}) event.each do |key, value| event[key] = parse_date(value) if date?(value) end end |
#usage_for_user(user_hid, start_time, stop_time, exclude = nil, callback_url = nil, snapshot = false) ⇒ Array
Get the usage events for the apps owned by the specified user during the specified period.
usage events JSON at the start_time
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/vault-usage-client/client.rb', line 177 def usage_for_user(user_hid, start_time, stop_time, exclude=nil, callback_url=nil, snapshot=false) unless start_time.zone.eql?('UTC') raise InvalidTimeError.new('Start time must be in UTC.') end unless stop_time.zone.eql?('UTC') raise InvalidTimeError.new('Stop time must be in UTC.') end path = "/users/#{user_hid}/usage/#{iso_format(start_time)}/" + "#{iso_format(stop_time)}" query = {} query[:snapshot] = true if snapshot unless exclude.nil? || exclude.empty? query[:exclude] = exclude.join(',') end query[:callback_url] = callback_url if callback_url connection = Excon.new(@url) response = connection.get(path: path, expects: [200], query: query) payload = MultiJson.load(response.body, {symbolize_keys: true}) return payload[:job_id] if payload[:job_id] events = payload[:events] events.each do |event| event.each do |key, value| event[key] = parse_date(value) if date?(value) end end end |
#usage_for_user_by_product(user_hid, product_name) ⇒ Array
Get the usage events for a product specific to a user.
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/vault-usage-client/client.rb', line 226 def usage_for_user_by_product(user_hid, product_name) path = "/users/#{user_hid}/usage/product/#{product_name}" connection = Excon.new(@url) response = connection.get(path: path, expects: [200]) payload = MultiJson.load(response.body, {symbolize_keys: true}) return payload[:job_id] if payload[:job_id] events = payload[:events] events.each do |event| event.each do |key, value| event[key] = parse_date(value) if date?(value) end end end |