class Start::BaseResource

Attributes

path[RW]

Public Class Methods

all() click to toggle source
# File lib/start/base_resource.rb, line 14
def all
  handle_response Start.get(path)
end
create(params = {}) click to toggle source
# File lib/start/base_resource.rb, line 6
def create(params = {})
  handle_response Start.post(path, body: params)
end
get(id) click to toggle source
# File lib/start/base_resource.rb, line 10
def get(id)
  handle_response Start.get("#{path}/#{id}")
end

Private Class Methods

handle_response(response) click to toggle source
# File lib/start/base_resource.rb, line 20
def handle_response(response)
  body = JSON.parse(response.body);

  if response.code.between?(200, 299) and !body.key?('error')
    # The request was successful
    return body
  end

  exception_class = if ['banking', 'authentication', 'processing', 'request'].include?(body['error']['type'])
                      Object.const_get "Start::#{body['error']['type'].capitalize}Error"
                    else
                      StartError
                    end

  raise exception_class.new(body['error']['message'], body['error']['code'], response.code, body['error']['extras'])
end