Class: Provide::ApiClient
- Inherits:
-
Object
- Object
- Provide::ApiClient
show all
- Defined in:
- lib/provide-ruby/api_client.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#delete(uri, params = nil) ⇒ Object
-
#get(uri, params = nil) ⇒ Object
-
#initialize(scheme = API_SCHEME, host = API_HOST, path = 'api/', token = nil) ⇒ ApiClient
constructor
A new instance of ApiClient.
-
#post(uri, params = nil) ⇒ Object
-
#put(uri, params = nil) ⇒ Object
-
#send_request(method, uri, params = nil, headers = nil) ⇒ Object
Constructor Details
#initialize(scheme = API_SCHEME, host = API_HOST, path = 'api/', token = nil) ⇒ ApiClient
Returns a new instance of ApiClient.
13
14
15
16
|
# File 'lib/provide-ruby/api_client.rb', line 13
def initialize(scheme = API_SCHEME, host = API_HOST, path = 'api/', token = nil)
@base_url = "#{scheme}://#{host}/#{path}"
@token = token
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
11
12
13
|
# File 'lib/provide-ruby/api_client.rb', line 11
def base_url
@base_url
end
|
#token ⇒ Object
Returns the value of attribute token.
11
12
13
|
# File 'lib/provide-ruby/api_client.rb', line 11
def token
@token
end
|
Instance Method Details
#delete(uri, params = nil) ⇒ Object
30
31
32
|
# File 'lib/provide-ruby/api_client.rb', line 30
def delete(uri, params = nil)
send_request(:delete, uri, params || {})
end
|
#get(uri, params = nil) ⇒ Object
18
19
20
|
# File 'lib/provide-ruby/api_client.rb', line 18
def get(uri, params = nil)
send_request(:get, uri, params || {})
end
|
#post(uri, params = nil) ⇒ Object
22
23
24
|
# File 'lib/provide-ruby/api_client.rb', line 22
def post(uri, params = nil)
send_request(:post, uri, params || {})
end
|
#put(uri, params = nil) ⇒ Object
26
27
28
|
# File 'lib/provide-ruby/api_client.rb', line 26
def put(uri, params = nil)
send_request(:put, uri, params || {})
end
|
#send_request(method, uri, params = nil, headers = nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/provide-ruby/api_client.rb', line 34
def send_request(method, uri, params = nil, = nil)
attempts = 0
begin
params = [:post, :put, :patch].include?(method.to_s.downcase.to_sym) ? { body: JSON.dump(params) } : { params: params }
= .merge( || {})
['Content-Type'] = 'application/json' if [:post, :put, :patch].include?(method.to_s.downcase.to_sym)
params.merge!(headers: )
Typhoeus.send(method.to_s.downcase.to_sym, "#{base_url}#{uri}", params)
rescue
attempts = attempts + 1
retry if attempts < API_MAX_ATTEMPTS
end
end
|