class Freemle::Client

Constants

VERSION

Attributes

api_key[RW]
app_name[RW]
base_url[RW]

Public Class Methods

create_customer(customer) click to toggle source

Creates a customer in Freemle with the given values. Returns the unique ID of the created invoice.

Raises Freemle::BadRequest when validation errors occur. Raises Freemle::UnauthorizedError when app_name and api_key are not properly set. Raises Freemle::ServerError if something goes wrong on the server for any other reason.

# File lib/freemle/client.rb, line 38
def self.create_customer(customer)
  post "/customers", customer: customer do |body_as_hash|
    body_as_hash["customer_id"]
  end
end
create_invoice(invoice) click to toggle source

Creates an invoice in Freemle with the given values. Returns the unique ID of the created invoice.

Raises Freemle::BadRequest when validation errors occur. Raises Freemle::UnauthorizedError when app_name and api_key are not properly set. Raises Freemle::ServerError if something goes wrong on the server for any other reason.

# File lib/freemle/client.rb, line 27
def self.create_invoice(invoice)
  post "/invoices", invoice: invoice do |body_as_hash|
    body_as_hash["invoice_id"]
  end
end
find_customers(company_name) click to toggle source

Finds customers in Freemle and returns them as a list of hashes. Each hash represents a customer.

Raises Freemle::BadRequest when validation errors occur. Raises Freemle::UnauthorizedError when app_name and api_key are not properly set. Raises Freemle::ServerError if something goes wrong on the server for any other reason.

# File lib/freemle/client.rb, line 16
def self.find_customers(company_name)
  client = get_client
  response = client.get "#{base_url}/customers", {company_name: company_name}, {Authorization: "Basic #{get_auth_header}"}
  handle_response(response, {200 => lambda { |response| from_json(response.body)["customers"] }})
end

Private Class Methods

from_json(body) click to toggle source
# File lib/freemle/client.rb, line 78
def self.from_json(body)
  ActiveSupport::JSON.decode(body)
end
get_auth_header() click to toggle source
# File lib/freemle/client.rb, line 57
def self.get_auth_header
  Base64.encode64("#{app_name}:#{api_key}").delete("\r\n")
end
get_client() click to toggle source
# File lib/freemle/client.rb, line 53
def self.get_client
  HTTPClient.new
end
handle_response(response, status_handlers = {}) click to toggle source
# File lib/freemle/client.rb, line 61
def self.handle_response(response, status_handlers = {})
  case response.status
    when 400 then
      raise BadRequest.new(from_json(response.body)["errors"])
    when 401 then
      raise UnauthorizedError
    when 500 then
      raise ServerError
    else
      if status_handlers[response.status].present?
        status_handlers[response.status].call(response)
      else
        raise UnsupportedStatusCode.new(response.status)
      end
  end
end
post(url, body) { |body_as_hash| ... } click to toggle source
# File lib/freemle/client.rb, line 46
def self.post(url, body, &block)
  client = get_client
  response = client.post "#{base_url}#{url}", body.to_json, {Authorization: "Basic #{get_auth_header}", 'Content-Type' => 'application/json'}
  body_as_hash = handle_response(response, {201 => lambda { |response| from_json(response.body) }})
  yield body_as_hash
end