Class: MercuryBanking::API

Inherits:
Object
  • Object
show all
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

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

#accountsObject

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: ,
    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()
   = ()
  ['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 ()
   = accounts.find { |a| a["accountNumber"] == .to_s }
  raise "Account with number #{} not found" unless 

  
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 ()
  path = "account/#{}"
  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 ||
     = get_transactions(["id"], start_date)
    # Add account information to each transaction
    .each do |transaction|
      transaction["accountName"] = ["name"]
      transaction["accountId"] = ["id"]
      transaction["accountNumber"] = ["accountNumber"]
    end
    all_transactions.concat()
  rescue StandardError => e
    puts "Warning: Could not fetch transactions for account #{['name']}: #{e.message}" 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(, start_date = nil)
  path = "account/#{}/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

#recipientsObject



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(, transaction_id)
  path = "account/#{}/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/#{}/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: ,
    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