Sha256: 7e7eb98184de5c9171c737e2fb435b8aec64f3059ab7a61cbd4f68e584e22b8f

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require "api-client/version"
require "net/http"

module ApiClient
  autoload :Exceptions, 'api-client/exceptions'

  def self.get(url = '')
    begin
      response = Net::HTTP.get_response(URI.parse(url))
    rescue Errno::ECONNREFUSED
      raise ApiClient::Exceptions::ConnectionRefused
    end
    raise_exception(response.code)
    response.body
  end

  def self.post(url = '', args = {})
    begin
      response = Net::HTTP.post_form(URI.parse(url), args)
    rescue Errno::ECONNREFUSED
      raise ApiClient::Exceptions::ConnectionRefused
    end
    raise_exception(response.code)
    response.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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
api-client-0.5.0 lib/api-client.rb