class Fluffy::API::RESTClient

Attributes

http[R]

@return [HTTPClient] HTTP client instance

url[R]

@return [String] Fluffy REST API's URL

Public Class Methods

new(url:) click to toggle source

Create a Fluffy Client API object

@param url [String] Fluffy REST API's URL

# File lib/fluffy/api.rb, line 21
def initialize(url:)
  @http = HTTPClient.new
  @url = url
end

Public Instance Methods

delete(endpoint:) click to toggle source

Perform a HTTP DELETE request

@param endpoint [Array, String] HTTP API endpoint @return [Hash, nil] API JSON response

# File lib/fluffy/api.rb, line 72
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) click to toggle source

Perform a HTTP GET request

@param endpoint [Array, String] HTTP API endpoint @param query [String] HTTP API query parameters @return [Array, Hash] API JSON response

# 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: {}) click to toggle source

Perform a HTTP PATCH request

@param endpoint [Array, String] HTTP API endpoint @param params [Hash] HTTP API body @return [Hash, nil] API JSON response

# 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'})
  unless resp.body.empty?
    data = JSON.parse(resp.body)
    raise APIError.new(data['message'], data['error'], resp.status) if resp.status >= 400
    data['message']
  end
end
post(endpoint:, params: {}) click to toggle source

Perform a HTTP POST request

@param endpoint [Array, String] HTTP API endpoint @param params [Hash] HTTP API body @return [Hash, nil] API JSON response

# 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