Class: Mondo::Client

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

Constant Summary collapse

DEFAULT_API_URL =
'https://api.monzo.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.

Raises:



18
19
20
21
22
23
24
25
# File 'lib/api/mondo/client.rb', line 18

def initialize(args = {})
  args.symbolize_keys!
  self.access_token = args.fetch(:token)
  self. = args.fetch(:account_id, nil)
  self.api_url = args.fetch(:api_url, DEFAULT_API_URL)
  raise ClientError.new("You must provide a token") unless self.access_token
   unless 
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



16
17
18
# File 'lib/api/mondo/client.rb', line 16

def access_token
  @access_token
end

#account_idObject

Returns the value of attribute account_id.



16
17
18
# File 'lib/api/mondo/client.rb', line 16

def 
  @account_id
end

#api_urlObject

Returns the value of attribute api_url.



16
17
18
# File 'lib/api/mondo/client.rb', line 16

def api_url
  @api_url
end

Instance Method Details

#accountsAccounts

Returns all accounts for this user.

Returns:

  • (Accounts)

    all accounts for this user



101
102
103
104
105
# File 'lib/api/mondo/client.rb', line 101

def accounts(opts = {})
  resp = api_get("/accounts", opts)
  return resp if resp.error.present?
  resp.parsed["accounts"].map { |acc| Account.new(acc, self) }
end

#api_delete(path, data = {}) ⇒ Hash

Note:

this method is for internal use

Issue a DELETE request to the API server

Parameters:

  • path (String)

    the path that will be added to the API prefix

  • data (Hash) (defaults to: {})

    a hash of data that will be sent as the request body

Returns:

  • (Hash)

    hash the parsed response data



85
86
87
# File 'lib/api/mondo/client.rb', line 85

def api_delete(path, data = {})
  api_request(:delete, path, :data => data)
end

#api_get(path, params = {}) ⇒ Hash

Note:

this method is for internal use

Issue an GET request to the API server

Parameters:

  • path (String)

    the path that will be added to the API prefix

  • params (Hash) (defaults to: {})

    query string parameters

Returns:

  • (Hash)

    hash the parsed response data



45
46
47
# File 'lib/api/mondo/client.rb', line 45

def api_get(path, params = {})
  api_request(:get, path, :params => params)
end

#api_patch(path, data = {}) ⇒ Hash

Note:

this method is for internal use

Issue a PATCH request to the API server

Parameters:

  • path (String)

    the path that will be added to the API prefix

  • data (Hash) (defaults to: {})

    a hash of data that will be sent as the request body

Returns:

  • (Hash)

    hash the parsed response data



75
76
77
# File 'lib/api/mondo/client.rb', line 75

def api_patch(path, data = {})
  api_request(:patch, path, :data => data)
end

#api_post(path, data = {}) ⇒ Hash

Note:

this method is for internal use

Issue a POST request to the API server

Parameters:

  • path (String)

    the path that will be added to the API prefix

  • data (Hash) (defaults to: {})

    a hash of data that will be sent as the request body

Returns:

  • (Hash)

    hash the parsed response data



55
56
57
# File 'lib/api/mondo/client.rb', line 55

def api_post(path, data = {})
  api_request(:post, path, :data => data)
end

#api_put(path, data = {}) ⇒ Hash

Note:

this method is for internal use

Issue a PUT request to the API server

Parameters:

  • path (String)

    the path that will be added to the API prefix

  • data (Hash) (defaults to: {})

    a hash of data that will be sent as the request body

Returns:

  • (Hash)

    hash the parsed response data



65
66
67
# File 'lib/api/mondo/client.rb', line 65

def api_put(path, data = {})
  api_request(:put, path, :data => data)
end

#api_request(method, path, opts = {}) ⇒ Object

Note:

this method is for internal use

Issue a request to the API server, returning the full response

Parameters:

  • method (Symbol)

    the HTTP method to use (e.g. :get, :post)

  • path (String)

    the path that will be added to the API prefix

  • [Hash] (Hash)

    a customizable set of options



95
96
97
# File 'lib/api/mondo/client.rb', line 95

def api_request(method, path, opts = {})
  request(method, path, opts)
end

#balanceBalance

Returns of the balance information.

Returns:

  • (Balance)

    of the balance information

Raises:



138
139
140
141
142
143
144
# File 'lib/api/mondo/client.rb', line 138

def balance( = nil)
   ||= self.
  raise ClientError.new("You must provide an account id to see your balance") unless 
  resp = api_get("balance", account_id: )
  return resp if resp.error.present?
  Balance.new(resp.parsed, self)
end

#cardsCards

Returns all cards for this user.

Returns:

  • (Cards)

    all cards for this user

Raises:



109
110
111
112
113
114
115
# File 'lib/api/mondo/client.rb', line 109

def cards(opts = {})
  raise ClientError.new("You must provide an account id to query transactions") unless self.
  opts.merge!(account_id: self.)
  resp = api_get("/card/list", opts)
  return resp if resp.error.present?
  resp.parsed["cards"].map { |tx| Card.new(tx, self) }
end

#connectionObject

The Faraday connection object



232
233
234
# File 'lib/api/mondo/client.rb', line 232

def connection
  @connection ||= Faraday.new(self.api_url, { ssl: { verify: false } })
end

#create_feed_item(params) ⇒ Object



146
147
148
# File 'lib/api/mondo/client.rb', line 146

def create_feed_item(params)
  FeedItem.new(params, self).save
end

#pingObject

Replies “pong”



35
36
37
# File 'lib/api/mondo/client.rb', line 35

def ping
  api_request(:get, "/ping").parsed["ping"]
end

#register_web_hook(url) ⇒ Object

Raises:



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/api/mondo/client.rb', line 150

def register_web_hook(url)
  raise ClientError.new("You must provide an account id to register webhooks") unless self.
  hook = WebHook.new(
    {
      account_id: self.,
      url: url
    },
    self
  )
  hook.save
  web_hooks << hook
end

#request(method, path, opts = {}) ⇒ Object

Send a request to the Mondo API servers

Parameters:

  • method (Symbol)

    the HTTP method to use (e.g. :get, :post)

  • path (String)

    the path fragment of the URL

  • [Hash] (Hash)

    a customizable set of options

Raises:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/api/mondo/client.rb', line 191

def request(method, path, opts = {})
  raise ClientError, 'Access token missing' unless @access_token

  opts[:headers] = {} if opts[:headers].nil?
  opts[:headers]['Accept'] = 'application/json'
  opts[:headers]['Content-Type'] = 'application/json' unless method == :get
  opts[:headers]['User-Agent'] = user_agent
  opts[:headers]['Authorization'] = "Bearer #{@access_token}"

  if !opts[:data].nil?
    opts[:body] = opts[:data].to_param

    puts "SETTING BODY #{opts[:body]}"

    opts[:headers]['Content-Type'] = 'application/x-www-form-urlencoded' # sob sob
  end

  path = URI.encode(path)

  resp = connection.run_request(method, path, opts[:body], opts[:headers]) do |req|
    req.params = opts[:params] if !opts[:params].nil?
  end

  response = Response.new(resp)

  case response.status
  when 301, 302, 303, 307
    # TODO
  when 200..299, 300..399
    # on non-redirecting 3xx statuses, just return the response
    response
  when 400..599
    error = ApiError.new(response)
    raise(error, "Status code #{response.status}")
  else
    error = ApiError.new(response)
    raise(error, "Unhandled status code value of #{response.status}")
  end
end

#set_accountObject

Hacky



28
29
30
31
32
# File 'lib/api/mondo/client.rb', line 28

def 
  acc = accounts.first
  return unless acc
  self. = acc.id
end

#transactionTransaction

Returns of the transaction information.

Returns:

Raises:



129
130
131
132
133
134
# File 'lib/api/mondo/client.rb', line 129

def transaction(transaction_id, opts = {})
  raise ClientError.new("You must provide an transaction id to query transactions") unless transaction_id
  resp = api_get("/transactions/#{transaction_id}", opts)
  return resp if resp.error.present?
  Transaction.new(resp.parsed['transaction'], self)
end

#transactionsTransactions

Returns all transactions for this user.

Returns:

  • (Transactions)

    all transactions for this user

Raises:



119
120
121
122
123
124
125
# File 'lib/api/mondo/client.rb', line 119

def transactions(opts = {})
  raise ClientError.new("You must provide an account id to query transactions") unless self.
  opts.merge!(account_id: self.)
  resp = api_get("/transactions", opts)
  return resp if resp.error.present?
  resp.parsed["transactions"].map { |tx| Transaction.new(tx, self) }
end

#user_agentObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/api/mondo/client.rb', line 174

def user_agent
  @user_agent ||= begin
    gem_info = "mondo-ruby/v#{Mondo::VERSION}"
    ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
    ruby_version = RUBY_VERSION
    ruby_version += " p#{RUBY_PATCHLEVEL}" if defined?(RUBY_PATCHLEVEL)
    comment = ["#{ruby_engine} #{ruby_version}"]
    comment << RUBY_PLATFORM if defined?(RUBY_PLATFORM)
    "#{gem_info} (#{comment.join("; ")})"
  end
end

#web_hooksObject

Raises:



163
164
165
166
167
168
169
170
171
172
# File 'lib/api/mondo/client.rb', line 163

def web_hooks
  raise ClientError.new("You must provide an account id to list webhooks") unless self.
  @web_hooks ||= begin
    resp = api_get("webhooks", account_id: self.)

    puts resp.inspect

    resp.parsed['webhooks'].map { |hook| WebHook.new(hook, self) }
  end
end