Class: Prodigi::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/prodigi/resource.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#default_headers ⇒ Object
-
#delete_request(url, params: {}, headers: {}) ⇒ Object
-
#get_request(url, params: {}, headers: {}) ⇒ Object
-
#handle_response(response) ⇒ Object
-
#initialize(client) ⇒ Resource
constructor
A new instance of Resource.
-
#patch_request(url, body:, headers: {}) ⇒ Object
-
#post_request(url, body:, headers: {}) ⇒ Object
-
#put_request(url, body:, headers: {}) ⇒ Object
Constructor Details
#initialize(client) ⇒ Resource
Returns a new instance of Resource.
5
6
7
|
# File 'lib/prodigi/resource.rb', line 5
def initialize(client)
@client = client
end
|
Instance Attribute Details
Returns the value of attribute client.
3
4
5
|
# File 'lib/prodigi/resource.rb', line 3
def client
@client
end
|
Instance Method Details
29
30
31
|
# File 'lib/prodigi/resource.rb', line 29
def
{ "X-API-Key": client.api_key }
end
|
#delete_request(url, params: {}, headers: {}) ⇒ Object
25
26
27
|
# File 'lib/prodigi/resource.rb', line 25
def delete_request(url, params: {}, headers: {})
handle_response client.connection.delete(url, params, .merge())
end
|
#get_request(url, params: {}, headers: {}) ⇒ Object
9
10
11
|
# File 'lib/prodigi/resource.rb', line 9
def get_request(url, params: {}, headers: {})
handle_response client.connection.get(url, params, .merge())
end
|
#handle_response(response) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/prodigi/resource.rb', line 33
def handle_response(response)
message = response.body["statusText"]
case response.status
when 400
raise Error, "Bad request: the request is malformed in some manner. #{message}"
when 401
raise Error, "Unauthorised: your credentials are missing or incorrect. #{message}"
when 403
raise Error, message
when 404
raise Error, "Not found: the resource does not exist (or does not exist in your account). #{message}"
when 429
raise Error, message
when 500
raise Error, "Internal server error: Please contact [email protected]. #{message}"
end
response
end
|
#patch_request(url, body:, headers: {}) ⇒ Object
17
18
19
|
# File 'lib/prodigi/resource.rb', line 17
def patch_request(url, body:, headers: {})
handle_response client.connection.patch(url, body, .merge())
end
|
#post_request(url, body:, headers: {}) ⇒ Object
13
14
15
|
# File 'lib/prodigi/resource.rb', line 13
def post_request(url, body:, headers: {})
handle_response client.connection.post(url, body, .merge())
end
|
#put_request(url, body:, headers: {}) ⇒ Object
21
22
23
|
# File 'lib/prodigi/resource.rb', line 21
def put_request(url, body:, headers: {})
handle_response client.connection.put(url, body, .merge())
end
|