Sha256: df0f83e12234dc2704d964be5ca42a8b4207ab6dc688b6db4ea78d495cf092e6

Contents?: true

Size: 1.3 KB

Versions: 16

Compression:

Stored size: 1.3 KB

Contents

require 'http'
module Ecoportal
  module API
    module Common
      class Client
        def initialize(api_key:, version: "v1", host: "live.ecoportal.com")
          @version  = version
          @api_key  = api_key
          if host.match(/^localhost|^127\.0\.0\.1/)
            @base_uri = "http://#{host}/api/"
          else
            @base_uri = "https://#{host}/api/"
          end
        end

        def get(path, params: {})
          request do |http|
            http.get(url_for(path), params: params)
          end
        end

        def post(path, data:)
          request do |http|
            http.post(url_for(path), json: data)
          end
        end

        def patch(path, data:)
          request do |http|
            http.patch(url_for(path), json: data)
          end
        end

        def delete(path)
          request do |http|
            http.delete(url_for(path))
          end
        end

        def request
          wrap_response yield(base_request)
        end

        def wrap_response(response)
          Ecoportal::API::Common::Response.new(response)
        end

        def base_request
          @base_request ||= HTTP.headers("X-ApiKey" => @api_key).accept(:json)
        end

        def url_for(path)
          @base_uri+@version+path
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ecoportal-api-0.2.2 lib/ecoportal/api/common/client.rb
ecoportal-api-0.2.1 lib/ecoportal/api/common/client.rb
ecoportal-api-0.2.0 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.12 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.11 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.10 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.9 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.8 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.7 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.6 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.5 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.4 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.3 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.2 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.1 lib/ecoportal/api/common/client.rb
ecoportal-api-0.1.0 lib/ecoportal/api/common/client.rb