Class: Ecoportal::API::Common::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/common/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, version: "v1", host: "live.ecoportal.com") ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
# File 'lib/ecoportal/api/common/client.rb', line 6

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

Instance Method Details

#base_requestObject



48
49
50
# File 'lib/ecoportal/api/common/client.rb', line 48

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

#delete(path) ⇒ Object



34
35
36
37
38
# File 'lib/ecoportal/api/common/client.rb', line 34

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

#get(path, params: {}) ⇒ Object



16
17
18
19
20
# File 'lib/ecoportal/api/common/client.rb', line 16

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

#patch(path, data:) ⇒ Object



28
29
30
31
32
# File 'lib/ecoportal/api/common/client.rb', line 28

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

#post(path, data:) ⇒ Object



22
23
24
25
26
# File 'lib/ecoportal/api/common/client.rb', line 22

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

#requestObject



40
41
42
# File 'lib/ecoportal/api/common/client.rb', line 40

def request
  wrap_response yield(base_request)
end

#url_for(path) ⇒ Object



52
53
54
# File 'lib/ecoportal/api/common/client.rb', line 52

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

#wrap_response(response) ⇒ Object



44
45
46
# File 'lib/ecoportal/api/common/client.rb', line 44

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