Class: Redd::Clients::Base

Inherits:
Object
  • Object
show all
Includes:
Account, Identity, None, Privatemessages, Read, Stream, Submit, Utilities, Wikiread
Defined in:
lib/redd/clients/base.rb,
lib/redd/clients/base/none.rb,
lib/redd/clients/base/read.rb,
lib/redd/clients/base/stream.rb,
lib/redd/clients/base/submit.rb,
lib/redd/clients/base/account.rb,
lib/redd/clients/base/identity.rb,
lib/redd/clients/base/wikiread.rb,
lib/redd/clients/base/utilities.rb,
lib/redd/clients/base/privatemessages.rb

Overview

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

Direct Known Subclasses

Installed, Script, Userless, Web

Defined Under Namespace

Modules: Account, Identity, None, Privatemessages, Read, Stream, Submit, Utilities, Wikiread

Constant Summary

Constants included from Utilities

Utilities::OBJECT_KINDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wikiread

#get_wikipages, #wikipage

Methods included from Stream

#stream

Methods included from Submit

#add_comment

Methods included from 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 Privatemessages

#my_messages, #read_all_messages

Methods included from None

#captcha_url, #needs_captcha?, #new_captcha

Methods included from Identity

#me, #my_prefs

Methods included from Account

#edit_my_prefs

Methods included from Utilities

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

Constructor Details

#initialize(**options) ⇒ Base

Note:

HTTPS is mandatory for OAuth2.

Create a Client.

Parameters:

  • options (Hash)

    The options to create the client with.

Options Hash (**options):

  • :user_agent (String)

    The User-Agent string to use in the header of every request.

  • :rate_limit (#after_limit)

    The handler that takes care of rate limiting.

  • :auth_endpoint (String)

    The main domain to authenticate with.

  • :api_endpoint (String)

    The main domain to make requests with.



64
65
66
67
68
69
70
# 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

Instance Attribute Details

#accessAccess

Returns The access object to make API requests with.

Returns:

  • (Access)

    The access object to make API requests with.



50
51
52
# File 'lib/redd/clients/base.rb', line 50

def access
  @access
end

#api_endpointString (readonly)

Returns The site to make API requests with.

Returns:

  • (String)

    The site to make API requests with.



46
47
48
# File 'lib/redd/clients/base.rb', line 46

def api_endpoint
  @api_endpoint
end

#auth_endpointString (readonly)

Returns The site to connect to for authentication.

Returns:

  • (String)

    The site to connect to for authentication.



42
43
44
# File 'lib/redd/clients/base.rb', line 42

def auth_endpoint
  @auth_endpoint
end

#rate_limit#after_limit (readonly)

Returns The handler that takes care of rate limiting.

Returns:

  • (#after_limit)

    The handler that takes care of rate limiting.



38
39
40
# File 'lib/redd/clients/base.rb', line 38

def rate_limit
  @rate_limit
end

#user_agentString (readonly)

Returns The user-agent used to communicate with reddit.

Returns:

  • (String)

    The user-agent used to communicate with reddit.



34
35
36
# File 'lib/redd/clients/base.rb', line 34

def user_agent
  @user_agent
end

Instance Method Details

#delete(path, params = {}) ⇒ Faraday::Response

Sends the request to the given path with the given params and return the body of the response.

Parameters:

  • path (String)

    The path under the api_endpoint to request.

  • params (Hash) (defaults to: {})

    The parameters to send with the request.

Returns:

  • (Faraday::Response)

    The response.



83
84
85
86
87
88
89
90
# File 'lib/redd/clients/base.rb', line 83

[:get, :post, :put, :patch, :delete].each do |meth|
  define_method(meth) do |path, params = {}|
    @rate_limit.after_limit do
      final_params = default_params.merge(params)
      connection.send(meth, path, final_params)
    end
  end
end

#get(path, params = {}) ⇒ Faraday::Response

Sends the request to the given path with the given params and return the body of the response.

Parameters:

  • path (String)

    The path under the api_endpoint to request.

  • params (Hash) (defaults to: {})

    The parameters to send with the request.

Returns:

  • (Faraday::Response)

    The response.



83
84
85
86
87
88
89
90
# File 'lib/redd/clients/base.rb', line 83

[:get, :post, :put, :patch, :delete].each do |meth|
  define_method(meth) do |path, params = {}|
    @rate_limit.after_limit do
      final_params = default_params.merge(params)
      connection.send(meth, path, final_params)
    end
  end
end

#patch(path, params = {}) ⇒ Faraday::Response

Sends the request to the given path with the given params and return the body of the response.

Parameters:

  • path (String)

    The path under the api_endpoint to request.

  • params (Hash) (defaults to: {})

    The parameters to send with the request.

Returns:

  • (Faraday::Response)

    The response.



83
84
85
86
87
88
89
90
# File 'lib/redd/clients/base.rb', line 83

[:get, :post, :put, :patch, :delete].each do |meth|
  define_method(meth) do |path, params = {}|
    @rate_limit.after_limit do
      final_params = default_params.merge(params)
      connection.send(meth, path, final_params)
    end
  end
end

#post(path, params = {}) ⇒ Faraday::Response

Sends the request to the given path with the given params and return the body of the response.

Parameters:

  • path (String)

    The path under the api_endpoint to request.

  • params (Hash) (defaults to: {})

    The parameters to send with the request.

Returns:

  • (Faraday::Response)

    The response.



83
84
85
86
87
88
89
90
# File 'lib/redd/clients/base.rb', line 83

[:get, :post, :put, :patch, :delete].each do |meth|
  define_method(meth) do |path, params = {}|
    @rate_limit.after_limit do
      final_params = default_params.merge(params)
      connection.send(meth, path, final_params)
    end
  end
end

#put(path, params = {}) ⇒ Faraday::Response

Sends the request to the given path with the given params and return the body of the response.

Parameters:

  • path (String)

    The path under the api_endpoint to request.

  • params (Hash) (defaults to: {})

    The parameters to send with the request.

Returns:

  • (Faraday::Response)

    The response.



83
84
85
86
87
88
89
90
# File 'lib/redd/clients/base.rb', line 83

[:get, :post, :put, :patch, :delete].each do |meth|
  define_method(meth) do |path, params = {}|
    @rate_limit.after_limit do
      final_params = default_params.merge(params)
      connection.send(meth, path, final_params)
    end
  end
end

#refresh_access!Access

Obtain a new access token using a refresh token.

Returns:

  • (Access)

    The refreshed information.



104
105
106
107
108
109
110
111
# 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) ⇒ Object

Dispose of an access or refresh token when you’re done with it.

Parameters:

  • remove_refresh_token (Boolean) (defaults to: false)

    Whether or not to remove all tokens associated with the user.



116
117
118
119
120
121
122
123
124
125
# 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) { ... } ⇒ Object

Parameters:

  • new_access (Access)

    The access to use.

Yields:

  • The client with the given access.



94
95
96
97
98
99
100
# 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