Class: Adk2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/adk2/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network_name, opts = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/adk2/client.rb', line 10

def initialize(network_name, opts = {})
  @api_base_url = "https://#{network_name}.adk2.com"
  @username = opts[:username]
  @password = opts[:password]
  @api_token = opts[:api_token]
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/adk2/client.rb', line 8

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/adk2/client.rb', line 8

def username
  @username
end

Instance Method Details

#entities(type, params = {}) ⇒ Object



17
18
19
# File 'lib/adk2/client.rb', line 17

def entities(type, params = {})
  JSON.parse(http_get(type, { rows: 1000 }.merge(params)), symbolize_names: true)[:rows]
end

#entity(type, id) ⇒ Object



21
22
23
# File 'lib/adk2/client.rb', line 21

def entity(type, id)
  JSON.parse(http_get("#{type}/#{id}"), symbolize_names: true)
end

#get_api_tokenObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/adk2/client.rb', line 30

def get_api_token
  return @api_token = nil if username.nil? || password.nil?
  response = resource('auth').post(
    {
      'email' => username,
      'password' => password
    }.to_json
  )

  @api_token = resource('profile/apikey').post(
    '',
    cookies: response.cookies
  )
end

#report(level, name, start_date, end_date) ⇒ Object



25
26
27
28
# File 'lib/adk2/client.rb', line 25

def report(level, name, start_date, end_date)
  response = http_get("reports/#{level}/#{name}/#{start_date}-#{end_date}", fmt: 'csv')
  CSV.parse(response, headers: true).map(&:to_h)
end