Class: M1API

Inherits:
Object
  • Object
show all
Defined in:
lib/m1_api.rb,
lib/m1_api/version.rb

Constant Summary collapse

VERSION =
Version = '0.0.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, api_config_file = nil) ⇒ M1API

Returns a new instance of M1API.



11
12
13
14
15
16
17
# File 'lib/m1_api.rb', line 11

def initialize(username, password, api_config_file = nil)
  @api_config_file = api_config_file || "#{__dir__}/m1_api/api_configs.yml"
  credentials = { username: username, password: password }
  res = YamlHelpers.call_api_from_yml(@api_config_file, 'authenticate', credentials)
  raise "failed to authenticate:\n\t#{res}" unless res[:code] == 200 && res[:body]['data']['authenticate']['result']['didSucceed']
  @token = res[:body]['data']['authenticate']['accessToken'] 
end

Instance Attribute Details

#api_config_fileObject

Returns the value of attribute api_config_file.



7
8
9
# File 'lib/m1_api.rb', line 7

def api_config_file
  @api_config_file
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/m1_api.rb', line 7

def token
  @token
end

Class Method Details

.read_credentials(credentials_file = nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/m1_api.rb', line 19

def self.read_credentials(credentials_file=nil)
  if credentials_file
    credentials = YamlHelpers.load_yaml(credentials_file)
    { username: credentials[:M1_USERNAME], password: credentials[:M1_PASSWORD] }
  else
    {username: ENV['M1_USERNAME'], password: ENV['M1_PASSWORD']}
  end
end

Instance Method Details

#check_statusObject



28
29
30
31
# File 'lib/m1_api.rb', line 28

def check_status
  res = call_api_from_yml(@@api_config_file, 'check_status', token)
  puts res.inspect
end

#query_accountsObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/m1_api.rb', line 33

def query_accounts
  accounts = {}
  data = { token: @token }
  id_res = YamlHelpers.call_api_from_yml(@api_config_file, 'list_account_ids', data)
  ids = id_res[:body]['data']['viewer']['_accounts1NFCow']['edges'].map { || ['node']['id'] }
  ids.each do |id|
    data[:account_id] = id
     = YamlHelpers.call_api_from_yml(@api_config_file, 'query_account', data)
    accounts[id] = [:body]['data']['node']
  end
  accounts
end