Class: M1API
- Inherits:
-
Object
- Object
- M1API
- Defined in:
- lib/m1_api.rb,
lib/m1_api/version.rb
Overview
open browser open network logs save entire thing as har parse should match the logs against when the actions were performed
Constant Summary collapse
- VERSION =
Version = '0.1.0'
Instance Attribute Summary collapse
-
#accounts ⇒ Object
Returns the value of attribute accounts.
-
#accounts_detail ⇒ Object
Returns the value of attribute accounts_detail.
-
#api_config_file ⇒ Object
Returns the value of attribute api_config_file.
-
#api_configs ⇒ Object
Returns the value of attribute api_configs.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #deposit(action, account_id, bank_id, transaction) ⇒ Object
-
#initialize(username, password, api_config_file = "#{__dir__}/m1_api/api_configs.yml") ⇒ M1API
constructor
A new instance of M1API.
- #load_config_file(api_config_file) ⇒ Object
- #output(string) ⇒ Object
- #query_account_detail(account_id) ⇒ Object
- #query_accounts ⇒ Object
- #withdraw(action, account_id, bank_id, transaction) ⇒ Object
Constructor Details
#initialize(username, password, api_config_file = "#{__dir__}/m1_api/api_configs.yml") ⇒ M1API
Returns a new instance of M1API.
20 21 22 23 24 25 26 27 28 |
# File 'lib/m1_api.rb', line 20 def initialize(username, password, api_config_file = "#{__dir__}/m1_api/api_configs.yml") @accounts = {} @accounts_detail = {} load_config_file(api_config_file) credentials = { username: username, password: password } res = YamlHelpers.call_api_from_config(@api_configs, :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
#accounts ⇒ Object
Returns the value of attribute accounts.
13 14 15 |
# File 'lib/m1_api.rb', line 13 def accounts @accounts end |
#accounts_detail ⇒ Object
Returns the value of attribute accounts_detail.
13 14 15 |
# File 'lib/m1_api.rb', line 13 def accounts_detail @accounts_detail end |
#api_config_file ⇒ Object
Returns the value of attribute api_config_file.
13 14 15 |
# File 'lib/m1_api.rb', line 13 def api_config_file @api_config_file end |
#api_configs ⇒ Object
Returns the value of attribute api_configs.
13 14 15 |
# File 'lib/m1_api.rb', line 13 def api_configs @api_configs end |
#token ⇒ Object
Returns the value of attribute token.
13 14 15 |
# File 'lib/m1_api.rb', line 13 def token @token end |
Class Method Details
.read_credentials(credentials_file = nil) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/m1_api.rb', line 36 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
#deposit(action, account_id, bank_id, transaction) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/m1_api.rb', line 66 def deposit(action, account_id, bank_id, transaction) case action when 'deposit' data = { token: @token, account_id: account_id, bank_id: bank_id, amount: transaction } YamlHelpers.call_api_from_config(@api_configs, :deposit, data) when 'cancel' data = { token: @token, account_id: account_id, bank_id: bank_id, transfer_id: transaction } YamlHelpers.call_api_from_config(@api_configs, :cancel_deposit, data) else output "Invalid deposit action: '#{action}'" end end |
#load_config_file(api_config_file) ⇒ Object
30 31 32 33 34 |
# File 'lib/m1_api.rb', line 30 def load_config_file(api_config_file) # change it to reject both if either fails @api_config_file = api_config_file @api_configs = YamlHelpers.load_yaml(@api_config_file) end |
#output(string) ⇒ Object
16 17 18 |
# File 'lib/m1_api.rb', line 16 def output(string) puts string end |
#query_account_detail(account_id) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/m1_api.rb', line 55 def query_account_detail(account_id) data = { token: @token, account_id: account_id } detail = YamlHelpers.call_api_from_config(@api_configs, :query_account_detail, data)[:body]['data']['node'] @accounts_detail[account_id] = { status: detail['status'], bank: detail['lastAchRelationship'], transfers: detail['_achTransfers']['edges'] } @accounts_detail end |
#query_accounts ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/m1_api.rb', line 45 def query_accounts accounts = {} data = { token: @token } id_res = YamlHelpers.call_api_from_config(@api_configs, :list_account_ids, data) ids = id_res[:body]['data']['viewer']['accounts']['edges'].each do |account| accounts[account['node']['nickname']] = account['node']['id'] end @accounts = accounts end |
#withdraw(action, account_id, bank_id, transaction) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/m1_api.rb', line 79 def withdraw(action, account_id, bank_id, transaction) case action when 'withdraw' data = { token: @token, account_id: account_id, bank_id: bank_id, amount: transaction } YamlHelpers.call_api_from_config(@api_configs, :withdraw, data) when 'cancel' data = { token: @token, account_id: account_id, bank_id: bank_id, transfer_id: transaction } YamlHelpers.call_api_from_config(@api_configs, :cancel_withdraw, data) else output "Invalid withdraw action: '#{action}'" end end |