Class: Fluffy::API::RESTClient
- Inherits:
-
Object
- Object
- Fluffy::API::RESTClient
- Defined in:
- lib/fluffy/api.rb
Instance Attribute Summary collapse
-
#http ⇒ HTTPClient
readonly
HTTP client instance.
-
#url ⇒ String
readonly
Fluffy REST API’s URL.
Instance Method Summary collapse
-
#delete(endpoint:) ⇒ Hash?
Perform a HTTP DELETE request.
-
#get(endpoint:, query: nil) ⇒ Array[Hash], Hash
Perform a HTTP GET request.
-
#initialize(url:) ⇒ RESTClient
constructor
Create a Fluffy Client API object.
-
#patch(endpoint:, params: {}) ⇒ Hash?
Perform a HTTP PATCH request.
-
#post(endpoint:, params: {}) ⇒ Hash?
Perform a HTTP POST request.
Constructor Details
#initialize(url:) ⇒ RESTClient
Create a Fluffy Client API object
21 22 23 24 |
# File 'lib/fluffy/api.rb', line 21 def initialize(url:) @http = HTTPClient.new @url = url end |
Instance Attribute Details
#http ⇒ HTTPClient (readonly)
Returns HTTP client instance.
13 14 15 |
# File 'lib/fluffy/api.rb', line 13 def http @http end |
#url ⇒ String (readonly)
Returns Fluffy REST API’s URL.
15 16 17 |
# File 'lib/fluffy/api.rb', line 15 def url @url end |
Instance Method Details
#delete(endpoint:) ⇒ Hash?
Perform a HTTP DELETE request
70 71 72 73 74 75 |
# File 'lib/fluffy/api.rb', line 70 def delete(endpoint:) resp = self.http.delete([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), {'Content-Type' => 'application/json', 'Accept' => 'application/json'}) data = JSON.parse(resp.body) raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400 data['message'] end |
#get(endpoint:, query: nil) ⇒ Array[Hash], Hash
Perform a HTTP GET request
32 33 34 35 36 37 |
# File 'lib/fluffy/api.rb', line 32 def get(endpoint:, query: nil) resp = self.http.get([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), query, {'Content-Type' => 'application/json', 'Accept' => 'application/json'}) data = JSON.parse(resp.body) raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400 data end |
#patch(endpoint:, params: {}) ⇒ Hash?
Perform a HTTP PATCH request
58 59 60 61 62 63 |
# File 'lib/fluffy/api.rb', line 58 def patch(endpoint:, params: {}) resp = self.http.patch([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), params.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'}) data = JSON.parse(resp.body) raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400 data['message'] end |
#post(endpoint:, params: {}) ⇒ Hash?
Perform a HTTP POST request
45 46 47 48 49 50 |
# File 'lib/fluffy/api.rb', line 45 def post(endpoint:, params: {}) resp = self.http.post([self.url, endpoint.is_a?(Array) ? endpoint.join('/') : endpoint].join('/'), params.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'}) data = JSON.parse(resp.body) raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400 data['message'] end |