Class: Fluffy::API::RESTClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ RESTClient

Create a Fluffy Client API object

Parameters:

  • url (String)

    Fluffy REST API’s URL



21
22
23
24
# File 'lib/fluffy/api.rb', line 21

def initialize(url:)
  @http = HTTPClient.new
  @url = url
end

Instance Attribute Details

#httpHTTPClient (readonly)

Returns HTTP client instance.

Returns:

  • (HTTPClient)

    HTTP client instance



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

def http
  @http
end

#urlString (readonly)

Returns Fluffy REST API’s URL.

Returns:

  • (String)

    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

Parameters:

  • endpoint (Array[String], String)

    HTTP API endpoint

Returns:

  • (Hash, nil)

    API JSON response

Raises:



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

Parameters:

  • endpoint (Array[String], String)

    HTTP API endpoint

  • query (String) (defaults to: nil)

    HTTP API query parameters

Returns:

  • (Array[Hash], Hash)

    API JSON response

Raises:



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

Parameters:

  • endpoint (Array[String], String)

    HTTP API endpoint

  • params (Hash) (defaults to: {})

    HTTP API body

Returns:

  • (Hash, nil)

    API JSON response

Raises:



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

Parameters:

  • endpoint (Array[String], String)

    HTTP API endpoint

  • params (Hash) (defaults to: {})

    HTTP API body

Returns:

  • (Hash, nil)

    API JSON response

Raises:



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