class RootInsurance::Client

Public Class Methods

new(app_id, app_secret, env=nil) click to toggle source

Initialize a new client

@param [String] app_id The app's id @param [String] app_secret The app's secret. Currently it's a blank string @param [Symbol] env The environment to use. Either :production or :sandbox. The default is :sandbox

# File lib/root_insurance/client.rb, line 25
def initialize(app_id, app_secret, env=nil)
  @app_id = app_id
  @app_secret = app_secret
  @env = env || :sandbox
end

Private Instance Methods

api_root() click to toggle source
# File lib/root_insurance/client.rb, line 86
def api_root
  @env == :production ? "https://api.root.co.za/v1/insurance" : "https://sandbox.root.co.za/v1/insurance"
end
auth() click to toggle source
# File lib/root_insurance/client.rb, line 82
def auth
  {username: @app_id, password: @app_secret}
end
error_message(response_body) click to toggle source
# File lib/root_insurance/client.rb, line 90
def error_message(response_body)
  response_body["error"] || response_body["message"]
end
get(entity, query=nil) click to toggle source
# File lib/root_insurance/client.rb, line 32
def get(entity, query=nil)
  response = HTTParty.get("#{api_root}/#{entity}",
    query: query || {},
    basic_auth: auth)

  parse_response(response)
end
parse_response(response) click to toggle source
# File lib/root_insurance/client.rb, line 67
def parse_response(response)
  parsed = JSON.parse(response.body)

  case response.code
  when 200
    parsed
  when 400
    raise RootInsurance::InputError.new(error_message(parsed))
  when 401, 403
    raise RootInsurance::AuthenticationError.new(error_message(parsed))
  else
    raise error_message(parsed)
  end
end
patch(entity, data) click to toggle source
# File lib/root_insurance/client.rb, line 58
def patch(entity, data)
  response = HTTParty.patch("#{api_root}/#{entity}",
    body:       data.to_json,
    basic_auth: auth,
    headers:    {'Content-Type' => 'application/json', 'Accept' => 'application/json'})

  parse_response(response)
end
post(entity, data) click to toggle source
# File lib/root_insurance/client.rb, line 40
def post(entity, data)
  response = HTTParty.post("#{api_root}/#{entity}",
    body:       data.to_json,
    basic_auth: auth,
    headers:    {'Content-Type' => 'application/json', 'Accept' => 'application/json'})

  parse_response(response)
end
put(entity, data) click to toggle source
# File lib/root_insurance/client.rb, line 49
def put(entity, data)
  response = HTTParty.put("#{api_root}/#{entity}",
    body:       data.to_json,
    basic_auth: auth,
    headers:    {'Content-Type' => 'application/json', 'Accept' => 'application/json'})

  parse_response(response)
end