Class: Redd::Clients::Base
- Inherits:
-
Object
- Object
- Redd::Clients::Base
- 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
Defined Under Namespace
Modules: Account, Identity, None, Privatemessages, Read, Stream, Submit, Utilities, Wikiread
Constant Summary
Constants included from Utilities
Instance Attribute Summary collapse
-
#access ⇒ Access
The access object to make API requests with.
-
#api_endpoint ⇒ String
readonly
The site to make API requests with.
-
#auth_endpoint ⇒ String
readonly
The site to connect to for authentication.
-
#rate_limit ⇒ #after_limit
readonly
The handler that takes care of rate limiting.
-
#user_agent ⇒ String
readonly
The user-agent used to communicate with reddit.
Instance Method Summary collapse
-
#delete(path, params = {}) ⇒ Faraday::Response
Sends the request to the given path with the given params and return the body of the response.
-
#get(path, params = {}) ⇒ Faraday::Response
Sends the request to the given path with the given params and return the body of the response.
-
#initialize(**options) ⇒ Base
constructor
Create a Client.
-
#patch(path, params = {}) ⇒ Faraday::Response
Sends the request to the given path with the given params and return the body of the response.
-
#post(path, params = {}) ⇒ Faraday::Response
Sends the request to the given path with the given params and return the body of the response.
-
#put(path, params = {}) ⇒ Faraday::Response
Sends the request to the given path with the given params and return the body of the response.
-
#refresh_access! ⇒ Access
Obtain a new access token using a refresh token.
-
#revoke_access!(remove_refresh_token = false) ⇒ Object
Dispose of an access or refresh token when you’re done with it.
- #with(new_access) { ... } ⇒ Object
Methods included from Wikiread
Methods included from Stream
Methods included from Submit
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
Methods included from Account
Methods included from Utilities
#append_to_listing, #flat_comments, #object_from_body, #property, #request_object
Constructor Details
#initialize(**options) ⇒ Base
HTTPS is mandatory for OAuth2.
Create a Client.
64 65 66 67 68 69 70 |
# File 'lib/redd/clients/base.rb', line 64 def initialize(**) @user_agent = [:user_agent] || "Redd/Ruby, v#{Redd::VERSION}" @rate_limit = [:rate_limit] || RateLimit.new(1) @auth_endpoint = [:auth_endpoint] || 'https://www.reddit.com/' @api_endpoint = [:api_endpoint] || 'https://oauth.reddit.com/' @access = Access.new(expires_at: Time.at(0)) end |
Instance Attribute Details
#access ⇒ Access
Returns The access object to make API requests with.
50 51 52 |
# File 'lib/redd/clients/base.rb', line 50 def access @access end |
#api_endpoint ⇒ String (readonly)
Returns 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_endpoint ⇒ String (readonly)
Returns 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.
38 39 40 |
# File 'lib/redd/clients/base.rb', line 38 def rate_limit @rate_limit end |
#user_agent ⇒ String (readonly)
Returns 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.
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.
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.
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.
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.
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.
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.
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
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 |