Class: PCO::API::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/pco/api/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: URL, oauth_access_token: nil, basic_auth_token: nil, basic_auth_secret: nil, connection: nil) ⇒ Endpoint

Returns a new instance of Endpoint.



17
18
19
20
21
22
23
24
25
# File 'lib/pco/api/endpoint.rb', line 17

def initialize(url: URL, oauth_access_token: nil, basic_auth_token: nil, basic_auth_secret: nil, connection: nil)
  @url = url
  @oauth_access_token = oauth_access_token
  @basic_auth_token = basic_auth_token
  @basic_auth_secret = basic_auth_secret
  @connection = connection || _build_connection
  @cache = {}
  @retry_when_rate_limited = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



27
28
29
# File 'lib/pco/api/endpoint.rb', line 27

def method_missing(method_name, *_args)
  _build_endpoint(method_name.to_s)
end

Instance Attribute Details

#last_resultObject (readonly)

Returns the value of attribute last_result.



13
14
15
# File 'lib/pco/api/endpoint.rb', line 13

def last_result
  @last_result
end

#retry_when_rate_limitedObject

Returns the value of attribute retry_when_rate_limited.



15
16
17
# File 'lib/pco/api/endpoint.rb', line 15

def retry_when_rate_limited
  @retry_when_rate_limited
end

#urlObject (readonly)

Returns the value of attribute url.



13
14
15
# File 'lib/pco/api/endpoint.rb', line 13

def url
  @url
end

Instance Method Details

#[](id) ⇒ Object



31
32
33
# File 'lib/pco/api/endpoint.rb', line 31

def [](id)
  _build_endpoint(id.to_s)
end

#deleteObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/pco/api/endpoint.rb', line 71

def delete
  @last_result = @connection.delete(@url)
  if @last_result.status == 204
    true
  else
    _build_response(@last_result)
  end
rescue Errors::TooManyRequests => e
  _retry_after_timeout?(e) ? retry : raise
end

#get(params = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/pco/api/endpoint.rb', line 46

def get(params = {})
  @last_result = @connection.get(@url, params)
  _build_response(@last_result)
rescue Errors::TooManyRequests => e
  _retry_after_timeout?(e) ? retry : raise
end

#patch(body = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/pco/api/endpoint.rb', line 62

def patch(body = {})
  @last_result = @connection.patch(@url) do |req|
    req.body = _build_body(body)
  end
  _build_response(@last_result)
rescue Errors::TooManyRequests => e
  _retry_after_timeout?(e) ? retry : raise
end

#post(body = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/pco/api/endpoint.rb', line 53

def post(body = {})
  @last_result = @connection.post(@url) do |req|
    req.body = _build_body(body)
  end
  _build_response(@last_result)
rescue Errors::TooManyRequests => e
  _retry_after_timeout?(e) ? retry : raise
end

#respond_to?(method_name, _include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/pco/api/endpoint.rb', line 35

def respond_to?(method_name, _include_all = false)
  endpoint = _build_endpoint(method_name.to_s)
  begin
    endpoint.get
  rescue Errors::NotFound
    false
  else
    true
  end
end