class Redd::Clients::Base

The basic client to inherit from. Don't use this directly, prefer {Redd.it}

Attributes

access[RW]

@!attribute [rw] access @return [Access] The access object to make API requests with.

api_endpoint[R]

@!attribute [r] api_endpoint @return [String] The site to make API requests with.

auth_endpoint[R]

@!attribute [r] auth_endpoint @return [String] The site to connect to for authentication.

rate_limit[R]

@!attribute [r] rate_limit @return [#after_limit] The handler that takes care of rate limiting.

user_agent[R]

@!attribute [r] user_agent @return [String] The user-agent used to communicate with reddit.

Public Class Methods

new(**options) click to toggle source

Create a Client.

@param [Hash] options The options to create the client with. @option options [String] :user_agent The User-Agent string to use in

the header of every request.

@option options [#after_limit] :rate_limit The handler that takes care

of rate limiting.

@option options [String] :auth_endpoint The main domain to authenticate

with.

@option options [String] :api_endpoint The main domain to make requests

with.

@note HTTPS is mandatory for OAuth2.

# File lib/redd/clients/base.rb, line 64
def initialize(**options)
  @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
  @rate_limit = options[:rate_limit] || RateLimit.new(1)
  @auth_endpoint = options[:auth_endpoint] || 'https://www.reddit.com/'
  @api_endpoint = options[:api_endpoint] || 'https://oauth.reddit.com/'
  @access = Access.new(expires_at: Time.at(0))
end

Public Instance Methods

refresh_access!() click to toggle source

Obtain a new access token using a refresh token. @return [Access] The refreshed information.

# File lib/redd/clients/base.rb, line 104
def refresh_access!
  response = auth_connection.post(
    '/api/v1/access_token',
    grant_type: 'refresh_token',
    refresh_token: access.refresh_token
  )
  access.refreshed!(response.body)
end
revoke_access!(remove_refresh_token = false) click to toggle source

Dispose of an access or refresh token when you're done with it. @param [Boolean] remove_refresh_token Whether or not to remove all

tokens associated with the user.
# File lib/redd/clients/base.rb, line 116
def revoke_access!(remove_refresh_token = false)
  token_type = remove_refresh_token ? :refresh_token : :access_token
  token = access.send(token_type)
  @access = nil
  auth_connection.post(
    '/api/v1/revoke_token',
    token: token,
    token_type_hint: token_type
  )
end
with(new_access) { |self| ... } click to toggle source

@param [Access] new_access The access to use. @yield The client with the given access.

# File lib/redd/clients/base.rb, line 94
def with(new_access)
  old_access = @access
  @access = new_access
  response = yield(self)
  @access = old_access
  response
end

Private Instance Methods

auth_connection() click to toggle source

@return [Faraday::Connection] A new or existing connection.

# File lib/redd/clients/base.rb, line 172
def auth_connection
  @auth_connection ||= Faraday.new(
    @auth_endpoint,
    headers: auth_headers,
    builder: middleware
  )
end
auth_headers() click to toggle source

@return [Hash] A hash of the headers with basic auth.

# File lib/redd/clients/base.rb, line 164
def auth_headers
  {
    'User-Agent' => @user_agent,
    'Authorization' => Faraday.basic_auth(@client_id, @secret)
  }
end
connection() click to toggle source

@return [Faraday::Connection] A new or existing connection.

# File lib/redd/clients/base.rb, line 155
def connection
  @connection ||= Faraday.new(
    @api_endpoint,
    headers: default_headers,
    builder: middleware
  )
end
default_headers() click to toggle source

@return [Hash] A hash of the headers used.

# File lib/redd/clients/base.rb, line 147
def default_headers
  {
    'User-Agent' => @user_agent,
    'Authorization' => "bearer #{@access.access_token}"
  }
end
default_params() click to toggle source

@return [Hash] The minimum parameters to send with every request.

# File lib/redd/clients/base.rb, line 142
def default_params
  @default_params ||= { api_type: 'json' }
end
middleware() click to toggle source

@return [Faraday::RackBuilder] The middleware to use when creating the

connection.
# File lib/redd/clients/base.rb, line 131
def middleware
  @middleware ||= Faraday::RackBuilder.new do |builder|
    builder.use Response::RaiseError
    builder.use Response::ParseJson
    builder.use Faraday::Request::Multipart
    builder.use Faraday::Request::UrlEncoded
    builder.adapter Faraday.default_adapter
  end
end