class Ethikdo::BaseModel

Constants

BASE_TEST_URL
BASE_URL

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/ethikdo/base_model.rb, line 6
def initialize(attributes = {})
  attributes.each do |key, value|
    m = "#{key}=".to_sym
    send(m, value) if respond_to?(m)
  end
end

Private Class Methods

api_key() click to toggle source
# File lib/ethikdo/base_model.rb, line 18
def self.api_key
  Ethikdo::configuration.api_key
end
base_options() click to toggle source
# File lib/ethikdo/base_model.rb, line 55
def self.base_options
  {
    format: :json,
    headers: {
      'Accept' => 'application/json',
      'Authorization' => api_key
    }
  }
end
base_url() click to toggle source
# File lib/ethikdo/base_model.rb, line 51
def self.base_url
  Ethikdo::configuration.environment == :production ? BASE_URL : BASE_TEST_URL
end
execute(method, path, options = {}) click to toggle source
# File lib/ethikdo/base_model.rb, line 22
def self.execute(method, path, options = {})
  begin
    response = request(method, path, options)
  rescue *Error::NET_HTTP_ERRORS => err
    raise ConnectionError.new, err.message
  end

  case response.code
  when 200..299
    response
  when 400
    raise BadRequestError.new(response)
  when 401
    raise AuthenticationError.new(response)
  when 404
    raise NotFoundError.new(response)
  when 400..499
    raise ResponseError.new(response)
  when 500
    raise InternalServerError.new(response)
  when 500..599
    raise ServerError.new(response)
  end
end
request(method, path, options = {}) click to toggle source
# File lib/ethikdo/base_model.rb, line 47
def self.request(method, path, options = {})
  HTTParty.send(method, base_url + path, base_options.merge(options))
end