class Redd::Client
The base class for JSON-based HTTP clients. Generic enough to be used for basically anything.
Constants
- Response
Holds a returned HTTP response.
- USER_AGENT
The default User-Agent to use if none was provided.
Public Class Methods
Create a new client. @param endpoint [String] the base endpoint to make all requests from @param user_agent [String] a user agent string
# File lib/redd/client.rb, line 22 def initialize(endpoint:, user_agent: USER_AGENT) @endpoint = endpoint @user_agent = user_agent end
Public Instance Methods
Make a DELETE request. @param path [String] the path relative to the endpoint @param options [Hash] the parameters to supply @return [Response] the response
# File lib/redd/client.rb, line 79 def delete(path, options = {}) request(:delete, path, form: options) end
Make a GET request. @param path [String] the path relative to the endpoint @param options [Hash] the parameters to supply @return [Response] the response
# File lib/redd/client.rb, line 47 def get(path, options = {}) request(:get, path, params: options) end
Make a PATCH request. @param path [String] the path relative to the endpoint @param options [Hash] the parameters to supply @return [Response] the response
# File lib/redd/client.rb, line 71 def patch(path, options = {}) request(:patch, path, form: options) end
Make a POST request. @param path [String] the path relative to the endpoint @param options [Hash] the parameters to supply @return [Response] the response
# File lib/redd/client.rb, line 55 def post(path, options = {}) request(:post, path, form: options) end
Make a PUT request. @param path [String] the path relative to the endpoint @param options [Hash] the parameters to supply @return [Response] the response
# File lib/redd/client.rb, line 63 def put(path, options = {}) request(:put, path, form: options) end
Make an HTTP request. @param verb [:get, :post, :put, :patch, :delete] the HTTP verb to use @param path [String] the path relative to the endpoint @param options [Hash] the request parameters @option options [Hash] :params the parameters to supply with the url @option options [Hash] :form the parameters to supply in the body @option options [Hash] :body the direct body contents @return [Response] the response
# 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
Private Instance Methods
@return [HTTP::Connection] the base connection object
# File lib/redd/client.rb, line 86 def connection # TODO: Make timeouts configurable @connection ||= HTTP.persistent(@endpoint) .headers('User-Agent' => @user_agent) .timeout(:per_operation, write: 5, connect: 5, read: 5) end