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:


77
78
79
# File 'lib/redd/client.rb', line 77

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:


45
46
47
# File 'lib/redd/client.rb', line 45

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:


69
70
71
# File 'lib/redd/client.rb', line 69

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:


53
54
55
# File 'lib/redd/client.rb', line 53

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:


61
62
63
# File 'lib/redd/client.rb', line 61

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
# File 'lib/redd/client.rb', line 35

def request(verb, path, options = {})
  # puts "#{verb.to_s.upcase} #{path}", '  ' + options.inspect
  response = connection.request(verb, path, **options)
  Response.new(response.status.code, response.headers, response.body.to_s)
end