Class: Redd::Client::OAuth2

Inherits:
Authenticated show all
Includes:
Authorization, Identity
Defined in:
lib/redd/client/oauth2.rb,
lib/redd/client/oauth2/identity.rb,
lib/redd/client/oauth2/authorization.rb

Overview

The client to connect using OAuth2.

Defined Under Namespace

Modules: Authorization, Identity

Instance Attribute Summary collapse

Attributes inherited from Authenticated

#cookie, #modhash, #username

Attributes inherited from Unauthenticated

#api_endpoint, #rate_limit, #user_agent

Instance Method Summary collapse

Methods included from Identity

#me

Methods included from Authorization

#auth_url, #refresh_access_token, #request_access_token

Methods inherited from Authenticated

#headers, #new_from_credentials

Methods included from Authenticated::Subreddits

#edit_subscription, #get_subreddits, #subscribe, #unsubscribe

Methods included from Authenticated::PrivateMessages

#block_message, #compose_message, #mark_as_read, #mark_as_unread, #messages

Methods included from Authenticated::Moderation

#accept_moderator_invite, #approve, #distinguish, #get_modqueue, #get_reports, #get_spam, #get_submissions, #get_unmoderated, #ignore_reports, #leave_contributor_status, #leave_moderator_status, #remove, #undistinguish, #unignore_reports

Methods included from Authenticated::LinksComments

#add_comment, #delete, #downvote, #edit, #hide, #mark_as_nsfw, #report, #save, #submit, #unhide, #unmark_as_nsfw, #unsave, #unvote, #upvote, #vote

Methods included from Authenticated::Flair

#get_flair, #get_flair_list, #set_flair

Methods included from Authenticated::Account

#me

Methods inherited from Unauthenticated

#delete, #get, #headers, #post, #put, #request

Methods included from Unauthenticated::Wiki

#get_wikipages, #wikipage

Methods included from Unauthenticated::Utilities

#comment_stream, #comments_from_response, #extract_attribute, #extract_fullname, #extract_id, #object_from_body, #object_from_kind, #object_from_response, #objects_from_listing, #submission_stream

Methods included from Unauthenticated::Subreddits

#get_subreddits, #search_subreddits, #subreddit

Methods included from Unauthenticated::Moderation

#stylesheet, #stylesheet_url

Methods included from Unauthenticated::Listing

#by_id, #get_comments, #get_controversial, #get_hot, #get_listing, #get_new, #get_random, #get_top

Methods included from Unauthenticated::LinksComments

#get_info, #get_replies, #replace_morecomments, #submission_comments

Methods included from Unauthenticated::Captcha

#captcha_url, #needs_captcha?, #new_captcha

Methods included from Unauthenticated::Account

#login

Constructor Details

#initialize(client_id, secret, redirect_uri, options = {}) ⇒ OAuth2

Returns a new instance of OAuth2.



29
30
31
32
33
34
35
36
37
# File 'lib/redd/client/oauth2.rb', line 29

def initialize(client_id, secret, redirect_uri, options = {})
  @client_id = client_id
  @secret = secret
  @redirect_uri = redirect_uri

  @rate_limit = options[:rate_limit] || Redd::RateLimit.new(1)
  @api_endpoint = options[:api_endpoint] || "https://oauth.reddit.com/"
  @auth_endpoint = options[:auth_endpoint] || "https://ssl.reddit.com/"
end

Instance Attribute Details

#accessString

Returns The access info used to make requests.

Returns:

  • (String)

    The access info used to make requests.



27
28
29
# File 'lib/redd/client/oauth2.rb', line 27

def access
  @access
end

#auth_endpointString

Returns The site to connect to authenticate with.

Returns:

  • (String)

    The site to connect to authenticate with.



15
16
17
# File 'lib/redd/client/oauth2.rb', line 15

def auth_endpoint
  @auth_endpoint
end

#client_idString (readonly)

Returns The client_id of the oauth application.

Returns:

  • (String)

    The client_id of the oauth application.



19
20
21
# File 'lib/redd/client/oauth2.rb', line 19

def client_id
  @client_id
end

#redirect_uriString (readonly)

Returns The exact redirect_uri of the oauth application.

Returns:

  • (String)

    The exact redirect_uri of the oauth application.



23
24
25
# File 'lib/redd/client/oauth2.rb', line 23

def redirect_uri
  @redirect_uri
end

Instance Method Details

#auth_connectionObject (private)



53
54
55
56
57
58
59
60
61
62
# File 'lib/redd/client/oauth2.rb', line 53

def auth_connection
  @auth_connection ||= Faraday.new(url: auth_endpoint) do |faraday|
    faraday.use Faraday::Request::UrlEncoded
    faraday.use Redd::Response::RaiseError
    faraday.use Redd::Response::ParseJson
    faraday.adapter Faraday.default_adapter

    faraday.basic_auth(@client_id, @secret)
  end
end

#connection(access_token = @access.access_token) ⇒ Object (private)



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redd/client/oauth2.rb', line 41

def connection(access_token = @access.access_token)
  @connection ||= Faraday.new(url: api_endpoint) do |faraday|
    faraday.use Faraday::Request::UrlEncoded
    faraday.use Redd::Response::RaiseError
    faraday.use Redd::Response::ParseJson
    faraday.adapter Faraday.default_adapter

    faraday.headers["Authorization"] = "bearer #{access_token}"
    faraday.headers["User-Agent"] = "Redd/Ruby, v#{Redd::VERSION}"
  end
end