Class: Redd::AuthStrategies::AuthStrategy

Inherits:
Client
  • Object
show all
Defined in:
lib/redd/auth_strategies/auth_strategy.rb

Overview

The API client for authentication to reddit.

Direct Known Subclasses

Script, Userless, Web

Constant Summary collapse

AUTH_ENDPOINT =

The API to make authentication requests to.

'https://www.reddit.com'

Constants inherited from Client

Client::USER_AGENT

Instance Method Summary collapse

Methods inherited from Client

#delete, #get, #patch, #post, #put, #request

Constructor Details

#initialize(client_id:, secret:, endpoint: AUTH_ENDPOINT, user_agent: USER_AGENT) ⇒ AuthStrategy

Returns a new instance of AuthStrategy.

Parameters:

  • client_id (String)

    the client id of the reddit app

  • secret (String)

    the app’s secret string

  • endpoint (String) (defaults to: AUTH_ENDPOINT)

    the url to contact for authentication requests

  • user_agent (String) (defaults to: USER_AGENT)

    the user agent to send with requests



16
17
18
19
20
# File 'lib/redd/auth_strategies/auth_strategy.rb', line 16

def initialize(client_id:, secret:, endpoint: AUTH_ENDPOINT, user_agent: USER_AGENT)
  super(endpoint: endpoint, user_agent: user_agent)
  @client_id = client_id
  @secret = secret
end

Instance Method Details

#authenticateAccess

This method is abstract.

Perform authentication and return the resulting access object

Returns the access token object.

Returns:

  • (Access)

    the access token object



24
25
26
# File 'lib/redd/auth_strategies/auth_strategy.rb', line 24

def authenticate(*)
  raise 'abstract method: this strategy cannot authenticate with reddit'
end

#refresh(_access) ⇒ Access

This method is abstract.

Refresh the authentication and return the refreshed access

Returns the new access.

Parameters:

  • _access (Access, String)

    the access to refresh

Returns:

  • (Access)

    the new access



31
32
33
# File 'lib/redd/auth_strategies/auth_strategy.rb', line 31

def refresh(_access)
  raise 'abstract method: this strategy cannot refresh access'
end

#revoke(access) ⇒ Object

Revoke the access token, making it invalid for future requests.

Parameters:

  • access (Access, String)

    the access to revoke



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redd/auth_strategies/auth_strategy.rb', line 37

def revoke(access)
  token =
    if access.is_a?(String)
      access
    elsif access.respond_to?(:refresh_token)
      access.refresh_token
    else
      access.access_token
    end
  post('/api/v1/revoke_token', token: token)
end