Class: Redd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/client.rb

Overview

The base class for JSON-based HTTP clients. Generic enough to be used for basically anything.

Direct Known Subclasses

APIClient, AuthStrategies::AuthStrategy

Defined Under Namespace

Classes: Response

Constant Summary collapse

USER_AGENT =

The default User-Agent to use if none was provided.

"Ruby:Redd:v#{Redd::VERSION} (by unknown)"

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, user_agent: USER_AGENT) ⇒ Client

Create a new client.

Parameters:

  • endpoint (String)

    the base endpoint to make all requests from

  • user_agent (String) (defaults to: USER_AGENT)

    a user agent string



22
23
24
25
# File 'lib/redd/client.rb', line 22

def initialize(endpoint:, user_agent: USER_AGENT)
  @endpoint = endpoint
  @user_agent = user_agent
end

Instance Method Details

#delete(path, options = {}) ⇒ Response

Make a DELETE request.

Parameters:

  • path (String)

    the path relative to the endpoint

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

    the parameters to supply

Returns:



79
80
81
# File 'lib/redd/client.rb', line 79

def delete(path, options = {})
  request(:delete, path, form: options)
end

#get(path, options = {}) ⇒ Response

Make a GET request.

Parameters:

  • path (String)

    the path relative to the endpoint

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

    the parameters to supply

Returns:



47
48
49
# File 'lib/redd/client.rb', line 47

def get(path, options = {})
  request(:get, path, params: options)
end

#patch(path, options = {}) ⇒ Response

Make a PATCH request.

Parameters:

  • path (String)

    the path relative to the endpoint

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

    the parameters to supply

Returns:



71
72
73
# File 'lib/redd/client.rb', line 71

def patch(path, options = {})
  request(:patch, path, form: options)
end

#post(path, options = {}) ⇒ Response

Make a POST request.

Parameters:

  • path (String)

    the path relative to the endpoint

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

    the parameters to supply

Returns:



55
56
57
# File 'lib/redd/client.rb', line 55

def post(path, options = {})
  request(:post, path, form: options)
end

#put(path, options = {}) ⇒ Response

Make a PUT request.

Parameters:

  • path (String)

    the path relative to the endpoint

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

    the parameters to supply

Returns:



63
64
65
# File 'lib/redd/client.rb', line 63

def put(path, options = {})
  request(:put, path, form: options)
end

#request(verb, path, options = {}) ⇒ Response

Make an HTTP request.

Parameters:

  • verb (:get, :post, :put, :patch, :delete)

    the HTTP verb to use

  • path (String)

    the path relative to the endpoint

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

    the request parameters

Options Hash (options):

  • :params (Hash)

    the parameters to supply with the url

  • :form (Hash)

    the parameters to supply in the body

  • :body (Hash)

    the direct body contents

Returns:



35
36
37
38
39
40
41
# File 'lib/redd/client.rb', line 35

def request(verb, path, options = {})
  # Uncomment if desperate
  # puts "#{verb} #{path}: #{options}"

  response = connection.request(verb, path, **options)
  Response.new(response.status.code, response.headers, response.body.to_s)
end