Class: Redd::Clients::Web

Inherits:
Base
  • Object
show all
Defined in:
lib/redd/clients/web.rb

Overview

The client for a web-based flow (e.g. “login with reddit”)

Constant Summary

Constants included from Base::Utilities

Base::Utilities::OBJECT_KINDS

Instance Attribute Summary collapse

Attributes inherited from Base

#access, #api_endpoint, #auth_endpoint, #rate_limit, #user_agent

Instance Method Summary collapse

Methods inherited from Base

#delete, #get, #patch, #post, #put, #refresh_access!, #revoke_access!, #with

Methods included from Base::Wikiread

#get_wikipages, #wikipage

Methods included from Base::Stream

#stream

Methods included from Base::Submit

#add_comment

Methods included from Base::Read

#from_fullname, #from_url, #get_comments, #get_controversial, #get_hot, #get_new, #get_top, #multi_from_path, #my_multis, #search, #subreddit_from_name, #user_from_name

Methods included from Base::Privatemessages

#my_messages, #read_all_messages

Methods included from Base::None

#captcha_url, #needs_captcha?, #new_captcha

Methods included from Base::Identity

#me, #my_prefs

Methods included from Base::Account

#edit_my_prefs

Methods included from Base::Utilities

#append_to_listing, #flat_comments, #object_from_body, #property, #request_object

Constructor Details

#initialize(client_id, secret, redirect_uri, **options) ⇒ Web

Returns a new instance of Web.

Parameters:

  • options (Hash)

    The options to create the client with.

See Also:



17
18
19
20
21
22
# File 'lib/redd/clients/web.rb', line 17

def initialize(client_id, secret, redirect_uri, **options)
  @client_id = client_id
  @secret = secret
  @redirect_uri = redirect_uri
  super(**options)
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



9
10
11
# File 'lib/redd/clients/web.rb', line 9

def client_id
  @client_id
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



12
13
14
# File 'lib/redd/clients/web.rb', line 12

def redirect_uri
  @redirect_uri
end

Instance Method Details

#auth_url(state, scope = ['identity'], duration = :temporary) ⇒ String

rubocop:disable Metrics/MethodLength

Parameters:

  • state (String)

    A random string to double-check later.

  • scope (Array<String>) (defaults to: ['identity'])

    The scope to request access to.

  • duration (:temporary, :permanent) (defaults to: :temporary)

Returns:

  • (String)

    The url to redirect the user to.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redd/clients/web.rb', line 29

def auth_url(state, scope = ['identity'], duration = :temporary)
  query = {
    response_type: 'code',
    client_id: @client_id,
    redirect_uri: @redirect_uri,
    state: state,
    scope: scope.join(','),
    duration: duration
  }

  url = URI.join(auth_endpoint, '/api/v1/authorize')
  url.query = URI.encode_www_form(query)
  url.to_s
end

#authorize!(code) ⇒ Access

Authorize using the code given.

Parameters:

  • code (String)

    The code from the get params.

Returns:

  • (Access)

    The access given by reddit.



47
48
49
50
51
52
53
54
55
56
# File 'lib/redd/clients/web.rb', line 47

def authorize!(code)
  response = auth_connection.post(
    '/api/v1/access_token',
    grant_type: 'authorization_code',
    code: code,
    redirect_uri: @redirect_uri
  )

  @access = Access.new(response.body)
end