class Monzo::Client

Internal: A client to perform HTTPS requests to the Monzo API.

Attributes

access_token[R]

Public: Returns the access token used to authenticate with.

Public Class Methods

new(access_token) click to toggle source

Public: Initialize a Monzo::Client to make HTTP requests

access_token - The access_token to authenticate with.

# File lib/monzo/client.rb, line 15
def initialize(access_token)
  @access_token = access_token
end

Public Instance Methods

delete(path) click to toggle source

Internal: Perform a DELETE request to the Monzo API.

path - The URI path to request.

Returns a HTTP response.

# File lib/monzo/client.rb, line 90
def delete(path)
  uri = build_uri(path)

  request = Net::HTTP::Delete.new(uri.request_uri)
  set_authorisation_header(request)

  response = https_client(uri).request(request)
end
get(path, options = {}) click to toggle source

Internal: Perform a GET request to the Monzo API.

path - The URI path to request. options - A Hash of query options to include in the URI.

Returns a HTTP response.

# File lib/monzo/client.rb, line 25
def get(path, options = {})
  uri = build_uri(path, options)

  request = Net::HTTP::Get.new(uri.request_uri)
  set_authorisation_header(request)

  response = https_client(uri).request(request)
end
patch(path, data, options = {}) click to toggle source

Internal: Perform a PATCH request to the Monzo API.

path - The URI path to request. data - The form data to send with the request. options - A Hash of query options to include in the URI.

Returns a HTTP response.

# File lib/monzo/client.rb, line 58
def patch(path, data, options = {})
  uri = build_uri(path, options)

  request = Net::HTTP::Patch.new(uri.request_uri)
  set_authorisation_header(request)
  request.set_form_data(data)

  response = https_client(uri).request(request)
end
post(path, data, options = {}) click to toggle source

Internal: Perform a POST request to the Monzo API.

path - The URI path to request. data - The form data to send with the request. options - A Hash of query options to include in the URI.

Returns a HTTP response.

# File lib/monzo/client.rb, line 41
def post(path, data, options = {})
  uri = build_uri(path, options)

  request = Net::HTTP::Post.new(uri.request_uri)
  set_authorisation_header(request)
  request.set_form_data(data)

  response = https_client(uri).request(request)
end
put(path, data, options = {}) click to toggle source

Internal: Perform a PUT request to the Monzo API.

path - The URI path to request. data - The form data to send with the request. options - A Hash of query options to include in the URI.

Returns a HTTP response.

# File lib/monzo/client.rb, line 75
def put(path, data, options = {})
  uri = build_uri(path, options)

  request = Net::HTTP::Put.new(uri.request_uri)
  set_authorisation_header(request)
  request.set_form_data(data)

  response = https_client(uri).request(request)
end

Private Instance Methods

build_uri(path, options = {}) click to toggle source

Internal: Build a URI for the given path and query options

path - The URI path for the URI. options - The query options for the URI.

Returns an instance of URI for the Monzo API.

# File lib/monzo/client.rb, line 107
def build_uri(path, options = {})
  uri = URI.join(host, path)
  uri.query = URI.encode_www_form(options)
  uri
end
host() click to toggle source

Internal: The host name for the Monzo API

Returns a host name as a String.

# File lib/monzo/client.rb, line 136
def host
  "https://api.monzo.com/"
end
https_client(uri) click to toggle source

Internal: Build a HTTPS client for the given URI for requests to be issued.

uri - The URI for the client to request.

Returns an instance of Net::HTTP.

# File lib/monzo/client.rb, line 118
def https_client(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http
end
set_authorisation_header(request) click to toggle source

Internal: Set an authorisation header with the access token for the request

request - The HTTP request to add the request header

Returns nothing.

# File lib/monzo/client.rb, line 129
def set_authorisation_header(request)
  request["Authorization"] = "Bearer #{access_token}"
end