Class: MercuryBanking::API
- Inherits:
-
Object
- Object
- MercuryBanking::API
- Defined in:
- lib/mercury_banking/api.rb
Overview
API client for Mercury Banking Handles all API requests to the Mercury Banking API
Instance Method Summary collapse
-
#accounts ⇒ Object
Userland API.
- #add_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:) ⇒ Object
- #balance(account_id) ⇒ Object
- #check_if_the_path_is_valid(path) ⇒ Object
-
#find_account_by_number(account_number) ⇒ Object
Find account by account number.
- #find_recipient(name:) ⇒ Object
- #get(path) ⇒ Object
- #get_account(account_id) ⇒ Object
-
#get_all_transactions(start_date = nil) ⇒ Object
Get transactions from all accounts.
- #get_recipient(recipient_id) ⇒ Object
-
#get_transactions(account_id, start_date = nil) ⇒ Object
/account/:id/transactions.
-
#initialize(secret) ⇒ API
constructor
A new instance of API.
- #parse_json(response) ⇒ Object
- #post(object, path) ⇒ Object
- #recipients ⇒ Object
- #send_get_request_to_api(path) ⇒ Object
- #send_post_request_to_api(object, path) ⇒ Object
-
#transaction(account_id, transaction_id) ⇒ Object
/account/:id/transactions/:id.
- #transfer(recipient_id:, amount:, account_id:, note: nil, external: nil) ⇒ Object
- #update_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:, recipient_id:) ⇒ Object
- #validate_body(body, path) ⇒ Object
Constructor Details
#initialize(secret) ⇒ API
Returns a new instance of API.
7 8 9 |
# File 'lib/mercury_banking/api.rb', line 7 def initialize(secret) @api_key = secret end |
Instance Method Details
#accounts ⇒ Object
Userland API
78 79 80 |
# File 'lib/mercury_banking/api.rb', line 78 def accounts get("accounts")["accounts"] end |
#add_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mercury_banking/api.rb', line 147 def add_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:) rec = MercuryBanking::Recipient.new( name: name, address: address, email: email, city: city, region: region, postal_code: postal_code, country: country, account_number: account_number, routing_number: routing_number ) post(rec.json, 'recipients') new_recipient = find_recipient(name: name) raise "Couldn't add account #{name}" unless new_recipient puts "Successfully added #{new_recipient['name']}" if new_recipient end |
#balance(account_id) ⇒ Object
95 96 97 98 |
# File 'lib/mercury_banking/api.rb', line 95 def balance(account_id) account = get_account(account_id) account['currentBalance'] end |
#check_if_the_path_is_valid(path) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/mercury_banking/api.rb', line 19 def check_if_the_path_is_valid(path) if path.nil? || path.empty? raise 'Path cannot be empty' elsif path[0] == '/' raise "Path should not start with a /." else path end end |
#find_account_by_number(account_number) ⇒ Object
Find account by account number
88 89 90 91 92 93 |
# File 'lib/mercury_banking/api.rb', line 88 def find_account_by_number(account_number) account = accounts.find { |a| a["accountNumber"] == account_number.to_s } raise "Account with number #{account_number} not found" unless account account end |
#find_recipient(name:) ⇒ Object
138 139 140 |
# File 'lib/mercury_banking/api.rb', line 138 def find_recipient(name:) recipients.find { |r| r["name"] == name && r['status'] != 'deleted' } end |
#get(path) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/mercury_banking/api.rb', line 11 def get(path) check_if_the_path_is_valid(path) response = send_get_request_to_api(path) body = parse_json(response) validate_body(body, path) body end |
#get_account(account_id) ⇒ Object
82 83 84 85 |
# File 'lib/mercury_banking/api.rb', line 82 def get_account(account_id) path = "account/#{account_id}" get(path) end |
#get_all_transactions(start_date = nil) ⇒ Object
Get transactions from all accounts
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mercury_banking/api.rb', line 108 def get_all_transactions(start_date = nil) all_transactions = [] accounts.each do |account| account_transactions = get_transactions(account["id"], start_date) # Add account information to each transaction account_transactions.each do |transaction| transaction["accountName"] = account["name"] transaction["accountId"] = account["id"] transaction["accountNumber"] = account["accountNumber"] end all_transactions.concat(account_transactions) rescue StandardError => e puts "Warning: Could not fetch transactions for account #{account['name']}: #{e.}" unless ENV['MERCURY_SILENT'] end # Sort all transactions by date (newest first) all_transactions.sort_by { |t| t["createdAt"] || "" }.reverse end |
#get_recipient(recipient_id) ⇒ Object
142 143 144 145 |
# File 'lib/mercury_banking/api.rb', line 142 def get_recipient(recipient_id) path = "recipient/#{recipient_id}" get(path) end |
#get_transactions(account_id, start_date = nil) ⇒ Object
/account/:id/transactions
101 102 103 104 105 |
# File 'lib/mercury_banking/api.rb', line 101 def get_transactions(account_id, start_date = nil) path = "account/#{account_id}/transactions" path += "?start=#{start_date}" if start_date get(path)["transactions"] end |
#parse_json(response) ⇒ Object
42 43 44 |
# File 'lib/mercury_banking/api.rb', line 42 def parse_json(response) JSON.parse(response.read_body) end |
#post(object, path) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/mercury_banking/api.rb', line 52 def post(object, path) check_if_the_path_is_valid(path) response = send_post_request_to_api(object, path) body = parse_json(response) validate_body(body, path) body end |
#recipients ⇒ Object
134 135 136 |
# File 'lib/mercury_banking/api.rb', line 134 def recipients get("recipients")["recipients"] end |
#send_get_request_to_api(path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mercury_banking/api.rb', line 29 def send_get_request_to_api(path) base_url = "https://backend.mercury.com/api/v1/" url = URI.join(base_url, path) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Get.new(url) request.basic_auth @api_key, '' http.request(request) end |
#send_post_request_to_api(object, path) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mercury_banking/api.rb', line 60 def send_post_request_to_api(object, path) base_url = "https://backend.mercury.com/api/v1/" url = URI.join(base_url, path) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["accept"] = 'application/json' request["content-type"] = 'application/json' request.body = object.to_json request.basic_auth @api_key, '' http.request(request) end |
#transaction(account_id, transaction_id) ⇒ Object
/account/:id/transactions/:id
129 130 131 132 |
# File 'lib/mercury_banking/api.rb', line 129 def transaction(account_id, transaction_id) path = "account/#{account_id}/transaction/#{transaction_id}" get(path) end |
#transfer(recipient_id:, amount:, account_id:, note: nil, external: nil) ⇒ Object
182 183 184 185 186 |
# File 'lib/mercury_banking/api.rb', line 182 def transfer(recipient_id:, amount:, account_id:, note: nil, external: nil) payload = { recipientId: recipient_id, amount: amount, paymentMethod: 'ach', note: note, externalMemo: external, idempotencyKey: "#{recipient_id}-#{amount}-#{note}-#{external}" } post(payload, "account/#{account_id}/transactions") end |
#update_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:, recipient_id:) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mercury_banking/api.rb', line 166 def update_recipient(name:, address:, email:, city:, region:, postal_code:, country:, account_number:, routing_number:, recipient_id:) rec = MercuryBanking::Recipient.new( name: name, address: address, email: email, city: city, region: region, postal_code: postal_code, country: country, account_number: account_number, routing_number: routing_number ) post(rec.json, "recipient/#{recipient_id}") end |
#validate_body(body, path) ⇒ Object
46 47 48 49 50 |
# File 'lib/mercury_banking/api.rb', line 46 def validate_body(body, path) raise "#{@api_key} access to #{path} errored with #{body['errors']['message']}" if body["errors"] body end |