Sha256: 200a15e11f1ac99caeb74084405e5fe33328f9b697dc60d0b36fc7bf6247cd70

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module VzaarApi
  module Lib
    class Api

      def self.api_root_url
        "#{VzaarApi.protocol}://#{VzaarApi.hostname}/api/v2"
      end

      def self.resource_url(resource, path = nil)
        [api_root_url, resource, path].compact.join('/')
      end

      def delete(url)
        handle_response http_client.delete(url, {}, headers)
      end

      def get(url, query = {})
        handle_response http_client.get(url, query, headers)
      end

      def patch(url, body = {})
        handle_response http_client.patch(url, body.to_json, headers)
      end

      def post(url, body = {})
        args = block_given? ? yield(body, headers) : [body.to_json, headers]
        handle_response http_client.post url, *args
      end

      def handle_response(response)
        api_response = ApiResponse.new(response)
        VzaarApi.rate_limit = api_response.rate_limit
        VzaarApi.rate_limit_remaining = api_response.rate_limit_remaining
        VzaarApi.rate_limit_reset = api_response.rate_limit_reset
        return api_response if api_response.ok?
        raise Error.new(api_response.error)
      end

      def http_client
        HTTPClient.new.tap do |c|
          c.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end
      end

      def headers
        {
          'X-Auth-Token' => VzaarApi.auth_token,
          'X-Client-Id'  => VzaarApi.client_id,
          'Content-Type' => 'application/json'
        }
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vzaar_api-2.0.2 lib/vzaar_api/lib/api.rb
vzaar_api-2.0.1 lib/vzaar_api/lib/api.rb