Class: Mondo::Client
- Inherits:
-
Object
- Object
- Mondo::Client
- Defined in:
- lib/api/mondo/client.rb
Constant Summary collapse
- DEFAULT_API_URL =
'https://api.monzo.com'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#api_url ⇒ Object
Returns the value of attribute api_url.
Instance Method Summary collapse
-
#accounts ⇒ Accounts
All accounts for this user.
-
#api_delete(path, data = {}) ⇒ Hash
Issue a DELETE request to the API server.
-
#api_get(path, params = {}) ⇒ Hash
Issue an GET request to the API server.
-
#api_patch(path, data = {}) ⇒ Hash
Issue a PATCH request to the API server.
-
#api_post(path, data = {}) ⇒ Hash
Issue a POST request to the API server.
-
#api_put(path, data = {}) ⇒ Hash
Issue a PUT request to the API server.
-
#api_request(method, path, opts = {}) ⇒ Object
Issue a request to the API server, returning the full response.
-
#balance ⇒ Balance
Of the balance information.
-
#cards ⇒ Cards
All cards for this user.
-
#connection ⇒ Object
The Faraday connection object.
- #create_feed_item(params) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
-
#ping ⇒ Object
Replies “pong”.
- #register_web_hook(url) ⇒ Object
-
#request(method, path, opts = {}) ⇒ Object
Send a request to the Mondo API servers.
-
#set_account ⇒ Object
Hacky.
-
#transaction ⇒ Transaction
Of the transaction information.
-
#transactions ⇒ Transactions
All transactions for this user.
- #user_agent ⇒ Object
- #web_hooks ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
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.account_id = 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 set_account unless account_id end |
Instance Attribute Details
#access_token ⇒ Object
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_id ⇒ Object
Returns the value of attribute account_id.
16 17 18 |
# File 'lib/api/mondo/client.rb', line 16 def account_id @account_id end |
#api_url ⇒ Object
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
#accounts ⇒ Accounts
Returns 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
this method is for internal use
Issue a DELETE request to the API server
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
this method is for internal use
Issue an GET request to the API server
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
this method is for internal use
Issue a PATCH request to the API server
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
this method is for internal use
Issue a POST request to the API server
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
this method is for internal use
Issue a PUT request to the API server
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
this method is for internal use
Issue a request to the API server, returning the full response
95 96 97 |
# File 'lib/api/mondo/client.rb', line 95 def api_request(method, path, opts = {}) request(method, path, opts) end |
#balance ⇒ Balance
Returns of the balance information.
138 139 140 141 142 143 144 |
# File 'lib/api/mondo/client.rb', line 138 def balance(account_id = nil) account_id ||= self.account_id raise ClientError.new("You must provide an account id to see your balance") unless account_id resp = api_get("balance", account_id: account_id) return resp if resp.error.present? Balance.new(resp.parsed, self) end |
#cards ⇒ Cards
Returns all cards for this user.
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.account_id opts.merge!(account_id: self.account_id) resp = api_get("/card/list", opts) return resp if resp.error.present? resp.parsed["cards"].map { |tx| Card.new(tx, self) } end |
#connection ⇒ Object
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 |
#ping ⇒ Object
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
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.account_id hook = WebHook.new( { account_id: self.account_id, url: url }, self ) hook.save web_hooks << hook end |
#request(method, path, opts = {}) ⇒ Object
Send a request to the Mondo API servers
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_account ⇒ Object
Hacky
28 29 30 31 32 |
# File 'lib/api/mondo/client.rb', line 28 def set_account acc = accounts.first return unless acc self.account_id = acc.id end |
#transaction ⇒ Transaction
Returns of the transaction information.
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 |
#transactions ⇒ Transactions
Returns all transactions for this user.
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.account_id opts.merge!(account_id: self.account_id) resp = api_get("/transactions", opts) return resp if resp.error.present? resp.parsed["transactions"].map { |tx| Transaction.new(tx, self) } end |
#user_agent ⇒ Object
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_hooks ⇒ Object
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.account_id @web_hooks ||= begin resp = api_get("webhooks", account_id: self.account_id) puts resp.inspect resp.parsed['webhooks'].map { |hook| WebHook.new(hook, self) } end end |