Class: Reddit::Base
- Inherits:
-
Object
- Object
- Reddit::Base
- Includes:
- HTTParty
- Defined in:
- lib/ruby_reddit_api/base.rb
Overview
Base module all other classes descend from. Stores the cookie, modhash and user info for many API calls. Handles authentication and directs JSON to the relevant handler.
Class Attribute Summary collapse
-
.cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
.modhash ⇒ Object
readonly
Returns the value of attribute modhash.
-
.throttle_duration ⇒ Object
readonly
Returns the value of attribute throttle_duration.
-
.user ⇒ Object
readonly
Returns the value of attribute user.
-
.user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Attribute Summary collapse
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#last_action ⇒ Object
readonly
Returns the value of attribute last_action.
Class Method Summary collapse
Instance Method Summary collapse
-
#base_headers ⇒ Object
HTTP headers to be sent in all API requests.
- #cookie ⇒ String?
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ String
-
#logged_in? ⇒ true, false
The session is authenticated if the captured cookie shows a valid session is in place.
-
#login ⇒ Boolean
Login to Reddit and capture the cookie.
-
#logout ⇒ nil
Remove the cookie to effectively logout of Reddit.
-
#modhash ⇒ String?
A kind of authenticity token for many API calls.
-
#read(url, options = {}) ⇒ Reddit::Submission, ...
Base communication function for nearly all API calls.
-
#user ⇒ String
Logged in user.
-
#user_agent ⇒ Object
String.
-
#user_id ⇒ String
Reddit’s displayed ID for the logged in user.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
14 15 16 |
# File 'lib/ruby_reddit_api/base.rb', line 14 def initialize(={}) @debug = StringIO.new end |
Class Attribute Details
.cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
10 11 12 |
# File 'lib/ruby_reddit_api/base.rb', line 10 def @cookie end |
.modhash ⇒ Object (readonly)
Returns the value of attribute modhash.
10 11 12 |
# File 'lib/ruby_reddit_api/base.rb', line 10 def modhash @modhash end |
.throttle_duration ⇒ Object (readonly)
Returns the value of attribute throttle_duration.
10 11 12 |
# File 'lib/ruby_reddit_api/base.rb', line 10 def throttle_duration @throttle_duration end |
.user ⇒ Object (readonly)
Returns the value of attribute user.
10 11 12 |
# File 'lib/ruby_reddit_api/base.rb', line 10 def user @user end |
.user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
10 11 12 |
# File 'lib/ruby_reddit_api/base.rb', line 10 def user_id @user_id end |
Instance Attribute Details
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
8 9 10 |
# File 'lib/ruby_reddit_api/base.rb', line 8 def debug @debug end |
#last_action ⇒ Object (readonly)
Returns the value of attribute last_action.
8 9 10 |
# File 'lib/ruby_reddit_api/base.rb', line 8 def last_action @last_action end |
Class Method Details
Instance Method Details
#base_headers ⇒ Object
HTTP headers to be sent in all API requests. At a minimum, ‘User-agent’ and ‘Cookie’ are needed.
72 73 74 |
# File 'lib/ruby_reddit_api/base.rb', line 72 def base_headers self.class.base_headers end |
#inspect ⇒ String
19 20 21 |
# File 'lib/ruby_reddit_api/base.rb', line 19 def inspect "<Reddit::Base>" end |
#logged_in? ⇒ true, false
The session is authenticated if the captured cookie shows a valid session is in place
61 62 63 |
# File 'lib/ruby_reddit_api/base.rb', line 61 def logged_in? !!( && ( =~ /reddit_session/) != nil) end |
#login ⇒ Boolean
Login to Reddit and capture the cookie
25 26 27 28 |
# File 'lib/ruby_reddit_api/base.rb', line 25 def login capture_session(self.class.post( "/api/login", {:body => {:user => @user, :passwd => @password}, :debug_output => @debug} ) ) logged_in? end |
#logout ⇒ nil
Remove the cookie to effectively logout of Reddit
32 33 34 |
# File 'lib/ruby_reddit_api/base.rb', line 32 def logout Reddit::Base.instance_variable_set("@cookie",nil) end |
#modhash ⇒ String?
A kind of authenticity token for many API calls
43 44 45 |
# File 'lib/ruby_reddit_api/base.rb', line 43 def modhash Reddit::Base.modhash end |
#read(url, options = {}) ⇒ Reddit::Submission, ...
Base communication function for nearly all API calls
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ruby_reddit_api/base.rb', line 78 def read(url, ={}) unless throttled? @debug.rewind verb = ([:verb] || "get") param_key = (verb == "get") ? :query : :body resp = self.class.send( verb, url, {param_key => ([param_key] || {}), :headers => base_headers, :debug_output => @debug}) if valid_response?(resp) @last_action = Time.now klass = Reddit.const_get([:handler] || "Submission") resp = klass.parse( JSON.parse(resp.body, :max_nesting => 9_999) ) return resp else return false end end end |
#user ⇒ String
Logged in user
55 56 57 |
# File 'lib/ruby_reddit_api/base.rb', line 55 def user Reddit::Base.user end |
#user_agent ⇒ Object
Returns String.
66 67 68 |
# File 'lib/ruby_reddit_api/base.rb', line 66 def user_agent self.class.user_agent end |