Class: Reddit::Internal::Connection
- Inherits:
-
Object
- Object
- Reddit::Internal::Connection
- Defined in:
- lib/reddit/internal/connection.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#last_request_time ⇒ Object
readonly
Returns the value of attribute last_request_time.
-
#max_retries ⇒ Object
Retry Info.
-
#password ⇒ Object
Returns the value of attribute password.
-
#request_throttle ⇒ Object
Rate Limiting Info.
-
#requests_per_minute ⇒ Object
Returns the value of attribute requests_per_minute.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#token ⇒ Object
Token Info.
-
#token_expiration ⇒ Object
Returns the value of attribute token_expiration.
-
#user_agent_title ⇒ Object
Returns the value of attribute user_agent_title.
-
#username ⇒ Object
User Information.
Instance Method Summary collapse
-
#initialize(username, password, client_id, secret, user_agent_title, request_throttle: nil, max_retries: nil) ⇒ Connection
constructor
Creates a new connection.
-
#refresh ⇒ Object
Refreshes a token (BROKEN? 400 bad request, possibly due to token still being valid…).
-
#request(method, url, payload) ⇒ Object
Handles Requests From This Users Auth.
-
#sign_in ⇒ Object
Signs In A User, Making The Connection Active.
-
#sign_out ⇒ Object
Signs Out A User, Killing the connection.
Constructor Details
#initialize(username, password, client_id, secret, user_agent_title, request_throttle: nil, max_retries: nil) ⇒ Connection
Creates a new connection
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/reddit/internal/connection.rb', line 25 def initialize(username, password, client_id, secret, user_agent_title, request_throttle: nil, max_retries: nil) # Set up the module @username = username @password = password @client_id = client_id @secret = secret @user_agent_title = user_agent_title @token = nil @request_throttle = request_throttle == nil ? true : false @requests_per_minute = 60 @last_request_time = Time.now.to_f @max_retries = max_retries ||= 3 at_exit do self.sign_out() end end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
8 9 10 |
# File 'lib/reddit/internal/connection.rb', line 8 def client_id @client_id end |
#last_request_time ⇒ Object (readonly)
Returns the value of attribute last_request_time.
19 20 21 |
# File 'lib/reddit/internal/connection.rb', line 19 def last_request_time @last_request_time end |
#max_retries ⇒ Object
Retry Info
22 23 24 |
# File 'lib/reddit/internal/connection.rb', line 22 def max_retries @max_retries end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/reddit/internal/connection.rb', line 7 def password @password end |
#request_throttle ⇒ Object
Rate Limiting Info
17 18 19 |
# File 'lib/reddit/internal/connection.rb', line 17 def request_throttle @request_throttle end |
#requests_per_minute ⇒ Object
Returns the value of attribute requests_per_minute.
18 19 20 |
# File 'lib/reddit/internal/connection.rb', line 18 def requests_per_minute @requests_per_minute end |
#secret ⇒ Object
Returns the value of attribute secret.
9 10 11 |
# File 'lib/reddit/internal/connection.rb', line 9 def secret @secret end |
#token ⇒ Object
Token Info
13 14 15 |
# File 'lib/reddit/internal/connection.rb', line 13 def token @token end |
#token_expiration ⇒ Object
Returns the value of attribute token_expiration.
14 15 16 |
# File 'lib/reddit/internal/connection.rb', line 14 def token_expiration @token_expiration end |
#user_agent_title ⇒ Object
Returns the value of attribute user_agent_title.
10 11 12 |
# File 'lib/reddit/internal/connection.rb', line 10 def user_agent_title @user_agent_title end |
#username ⇒ Object
User Information
6 7 8 |
# File 'lib/reddit/internal/connection.rb', line 6 def username @username end |
Instance Method Details
#refresh ⇒ Object
Refreshes a token (BROKEN? 400 bad request, possibly due to token still being valid…)
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/reddit/internal/connection.rb', line 89 def refresh() Reddit::Internal::Logger.log.debug "Refreshing Token For User #{@username} with client_id #{@client_id}" response = JSON.parse(RestClient::Request.execute(method: :post, url: "https://www.reddit.com/#{Reddit::Services::REFERENCE["Auth"]["access_token"]["url"]}", payload: "grant_type=refresh_token&refresh_token=#{@token}", user: @client_id, password: @secret)) Reddit::Internal::Logger.log.debug "Refresh Response:" Reddit::Internal::Logger.log.debug JSON.pretty_generate(response) return response end |
#request(method, url, payload) ⇒ Object
Handles Requests From This Users Auth
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/reddit/internal/connection.rb', line 104 def request(method, url, payload) raise "User Not Signed In!" unless @token refresh() if @token_expiration < Time.now.to_f Reddit::Internal::Logger.log.debug "Reqest From User #{@username}, method: #{method}, url: #{url}" if @request_throttle Reddit::Internal::Logger.log.debug "Request Throttling Enabled, Processing Request at #{Time.now.to_f}, Last Call: #{@last_request_time}..." request_wait_time = @requests_per_minute / 60 next_avaliable = @last_request_time + request_wait_time if Time.now.to_f < next_avaliable sleep_time = (next_avaliable - Time.now.to_f) Reddit::Internal::Logger.log.info "Rate Limiter Sleeping For #{sleep_time}" sleep sleep_time end end # Handle getting a new token if we expired... retries = @max_retries begin response = JSON.parse(RestClient::Request.execute(method: method, url: url, payload: payload, headers: {"Authorization" => "bearer #{token}", "User-Agent" => "ruby-reddit-api:#{user_agent_title}"} )) rescue StandardError => e retry unless (retries -= 1).zero? raise e end Reddit::Internal::Logger.log.debug "Request Response:" Reddit::Internal::Logger.log.debug JSON.pretty_generate(response) @last_request_time = Time.now.to_f return response end |
#sign_in ⇒ Object
Signs In A User, Making The Connection Active
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/reddit/internal/connection.rb', line 48 def sign_in() Reddit::Internal::Logger.log.debug "Signing In User #{@username} with client_id #{@client_id}..." response = JSON.parse(RestClient::Request.execute(method: :post, url: "https://www.reddit.com/#{Reddit::Services::REFERENCE["Auth"]["access_token"]["url"]}", payload: "grant_type=password&username=#{@username}&password=#{@password}", user: @client_id, password: @secret)) Reddit::Internal::Logger.log.debug "Sign In Response:" Reddit::Internal::Logger.log.debug JSON.pretty_generate(response) raise "Error Invalid Credentials" if response.include?("error") @token_expiration = Time.now.to_f + response["expires_in"] @token = response["access_token"] Reddit::Internal::Logger.log.info "Sign In Retrieved Token: #{@token}" return @token end |
#sign_out ⇒ Object
Signs Out A User, Killing the connection
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/reddit/internal/connection.rb', line 71 def sign_out() if @token Reddit::Internal::Logger.log.debug "Signing Out User #{@username} with client_id #{@client_id}..." response = RestClient::Request.execute(method: :post, url: "https://www.reddit.com/#{Reddit::Services::REFERENCE["Auth"]["revoke_token"]["url"]}", payload: "token=#{@token}", user: @client_id, password: @secret) Reddit::Internal::Logger.log.debug "Sign Out Response: #{response}" Reddit::Internal::Logger.log.info "Signed Out User #{@username}" @token = nil else end end |