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.



11
12
13
14
15
16
17
18
# File 'lib/pco/api/endpoint.rb', line 11

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 = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



20
21
22
# File 'lib/pco/api/endpoint.rb', line 20

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.



9
10
11
# File 'lib/pco/api/endpoint.rb', line 9

def last_result
  @last_result
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/pco/api/endpoint.rb', line 9

def url
  @url
end

Instance Method Details

#[](id) ⇒ Object



24
25
26
# File 'lib/pco/api/endpoint.rb', line 24

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

#deleteObject



58
59
60
61
62
63
64
65
# File 'lib/pco/api/endpoint.rb', line 58

def delete
  @last_result = @connection.delete(@url)
  if @last_result.status == 204
    true
  else
    _build_response(@last_result)
  end
end

#get(params = {}) ⇒ Object



39
40
41
42
# File 'lib/pco/api/endpoint.rb', line 39

def get(params = {})
  @last_result = @connection.get(@url, params)
  _build_response(@last_result)
end

#patch(body = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/pco/api/endpoint.rb', line 51

def patch(body = {})
  @last_result = @connection.patch(@url) do |req|
    req.body = _build_body(body)
  end
  _build_response(@last_result)
end

#post(body = {}) ⇒ Object



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

def post(body = {})
  @last_result = @connection.post(@url) do |req|
    req.body = _build_body(body)
  end
  _build_response(@last_result)
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
# File 'lib/pco/api/endpoint.rb', line 28

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