Module: EZApi::Client

Defined in:
lib/ezapi/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



8
9
10
# File 'lib/ezapi/client.rb', line 8

def base_url
  @base_url
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/ezapi/client.rb', line 8

def key
  @key
end

Class Method Details

.extended(base) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ezapi/client.rb', line 10

def self.extended(base)
  [:get, :post, :put, :patch, :delete].each do |method|
    define_method(method) do |path, params = {}|
      full_path = full_api_path(path)
      request(full_path, method, params)
    end
  end
end

Instance Method Details

#api_key(key) ⇒ Object



23
24
25
# File 'lib/ezapi/client.rb', line 23

def api_key(key)
  self.key = key
end

#api_url(url) ⇒ Object



19
20
21
# File 'lib/ezapi/client.rb', line 19

def api_url(url)
  self.base_url = url
end

#request(full_url, method, params = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ezapi/client.rb', line 27

def request(full_url, method, params={})
  raise(AuthenticationError, "API key is not set for #{self.app_name}.") unless self.key

  begin
    response = RestClient::Request.execute(method: method, url: full_url, payload: params.to_json, headers: request_headers)
    JSON.parse(response) unless response.empty?
  rescue RestClient::ExceptionWithResponse => e
    if response_code = e.http_code and response_body = e.http_body
      handle_api_error(response_code, JSON.parse(response_body))
    else
      handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    handle_restclient_error(e)
  end
end