Sha256: 46e4ce41b41e7a6c67b1cfb53ed7e08b22827021897edab2a6e1e18c93c8cb85

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require "active_model"

class ApiClient::Base
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  extend ApiClient::Parser
  extend ApiClient::Dispatcher

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end

  def self.get(url = '', header = {})
    dispatch { _get(url, header) }
  end

  def self.post(url = '', args = {}, header = {})
    dispatch { _post(url, args, header) }
  end

  def self.put(url = '', args = {}, header = {})
    dispatch { _put(url, args, header) }
  end

  def self.patch(url = '', args = {}, header = {})
    dispatch { _patch(url, args, header) }
  end

  def self.delete(url = '', header = {})
    dispatch { _delete(url, header) }
  end

  protected

  def self.dispatch
    begin
      code, body = _response(yield)
    rescue Errno::ECONNREFUSED
      raise ApiClient::Exceptions::ConnectionRefused
    end
    raise_exception(code)
    new(body)
  end

  def self.raise_exception(code)
    case code
      when '401' then raise ApiClient::Exceptions::Unauthorized
      when '403' then raise ApiClient::Exceptions::Forbidden
      when '404' then raise ApiClient::Exceptions::NotFound
      when '500' then raise ApiClient::Exceptions::InternalServerError
      when '502' then raise ApiClient::Exceptions::BadGateway
      when '503' then raise ApiClient::Exceptions::ServiceUnavailable
      else return
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api-client-1.2.0 lib/api-client/base.rb