class Redd::AuthStrategies::AuthStrategy
The API client for authentication to reddit.
Constants
- AUTH_ENDPOINT
The API to make authentication requests to.
Public Class Methods
new(client_id:, secret:, endpoint: AUTH_ENDPOINT, user_agent: USER_AGENT)
click to toggle source
@param client_id [String] the client id of the reddit app @param secret [String] the app's secret string @param endpoint [String] the url to contact for authentication requests @param user_agent [String] the user agent to send with requests
Calls superclass method
# 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
Public Instance Methods
authenticate(*)
click to toggle source
@abstract Perform authentication and return the resulting access object @return [Access] the access token object
# File lib/redd/auth_strategies/auth_strategy.rb, line 24 def authenticate(*) raise 'abstract method: this strategy cannot authenticate with reddit' end
refresh(_access)
click to toggle source
@abstract Refresh the authentication and return the refreshed access @param _access [Access, String] the access to refresh @return [Access] the new access
# File lib/redd/auth_strategies/auth_strategy.rb, line 31 def refresh(_access) raise 'abstract method: this strategy cannot refresh access' end
revoke(access)
click to toggle source
Revoke the access token, making it invalid for future requests. @param access [Access, String] the access to revoke
# 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
Private Instance Methods
connection()
click to toggle source
Calls superclass method
# File lib/redd/auth_strategies/auth_strategy.rb, line 51 def connection @connection ||= super.basic_auth(user: @client_id, pass: @secret) end
request_access(grant_type, options = {})
click to toggle source
# File lib/redd/auth_strategies/auth_strategy.rb, line 55 def request_access(grant_type, options = {}) response = post('/api/v1/access_token', { grant_type: grant_type }.merge(options)) raise AuthenticationError.new(response) if response.body.key?(:error) Models::Access.new(self, response.body) end