Class: Panoptes::Endpoints::BaseEndpoint
- Inherits:
-
Object
- Object
- Panoptes::Endpoints::BaseEndpoint
show all
- Defined in:
- lib/panoptes/endpoints/base_endpoint.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#connection ⇒ Object
-
#delete(path, query = {}, etag: nil) ⇒ Object
-
#etag_header(etag) ⇒ Object
-
#get(path, query = {}) ⇒ Object
-
#handle_response(response) ⇒ Object
-
#initialize(auth: {}, url: nil, prefix: nil, params: nil) {|faraday| ... } ⇒ BaseEndpoint
constructor
A new instance of BaseEndpoint.
-
#patch(path, body = {}, etag: nil) ⇒ Object
-
#post(path, body = {}) ⇒ Object
-
#put(path, body = {}, etag: nil) ⇒ Object
-
#request(method, path, *args) ⇒ Object
Constructor Details
#initialize(auth: {}, url: nil, prefix: nil, params: nil) {|faraday| ... } ⇒ BaseEndpoint
Returns a new instance of BaseEndpoint.
21
22
23
24
25
26
27
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 21
def initialize(auth: {}, url: nil, prefix: nil, params: nil, &config)
@auth = auth
@url = url
@prefix = prefix
@config = config
@params = params
end
|
Instance Attribute Details
#auth ⇒ Object
Returns the value of attribute auth.
10
11
12
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10
def auth
@auth
end
|
#params ⇒ Object
Returns the value of attribute params.
10
11
12
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10
def params
@params
end
|
#prefix ⇒ Object
Returns the value of attribute prefix.
10
11
12
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10
def prefix
@prefix
end
|
#url ⇒ Object
Returns the value of attribute url.
10
11
12
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 10
def url
@url
end
|
Instance Method Details
#connection ⇒ Object
29
30
31
32
33
34
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 29
def connection
@connection ||= Faraday.new(url) do |faraday|
auth_request faraday, auth
configure faraday
end
end
|
#delete(path, query = {}, etag: nil) ⇒ Object
52
53
54
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 52
def delete(path, query = {}, etag: nil)
request :delete, path, query, (etag)
end
|
56
57
58
59
60
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 56
def (etag)
{}.tap do ||
['If-Match'] = etag if etag
end
end
|
#get(path, query = {}) ⇒ Object
36
37
38
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 36
def get(path, query = {})
request :get, path, query
end
|
#handle_response(response) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 70
def handle_response(response)
case response.status
when 404
raise Panoptes::Client::ResourceNotFound, status: response.status, body: response.body
when 400..600
raise Panoptes::Client::ServerError.new, response.body
else
response.body
end
end
|
#patch(path, body = {}, etag: nil) ⇒ Object
48
49
50
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 48
def patch(path, body = {}, etag: nil)
request :patch, path, body, (etag)
end
|
#post(path, body = {}) ⇒ Object
40
41
42
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 40
def post(path, body = {})
request :post, path, body
end
|
#put(path, body = {}, etag: nil) ⇒ Object
44
45
46
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 44
def put(path, body = {}, etag: nil)
request :put, path, body, (etag)
end
|
#request(method, path, *args) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/panoptes/endpoints/base_endpoint.rb', line 62
def request(method, path, *args)
if prefix
sep = path[0] == '/' ? nil : '/'
path = "#{prefix}#{sep}#{path}"
end
handle_response connection.send(method, path, *args)
end
|