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.
-
.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.
12 13 14 |
# File 'lib/ruby_reddit_api/base.rb', line 12 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 |
.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.
70 71 72 |
# File 'lib/ruby_reddit_api/base.rb', line 70 def base_headers self.class.base_headers end |
#inspect ⇒ String
17 18 19 |
# File 'lib/ruby_reddit_api/base.rb', line 17 def inspect "<Reddit::Base>" end |
#logged_in? ⇒ true, false
The session is authenticated if the captured cookie shows a valid session is in place
59 60 61 |
# File 'lib/ruby_reddit_api/base.rb', line 59 def logged_in? !!( && ( =~ /reddit_session/) != nil) end |
#login ⇒ Boolean
Login to Reddit and capture the cookie
23 24 25 26 |
# File 'lib/ruby_reddit_api/base.rb', line 23 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
30 31 32 |
# File 'lib/ruby_reddit_api/base.rb', line 30 def logout Reddit::Base.instance_variable_set("@cookie",nil) end |
#modhash ⇒ String?
A kind of authenticity token for many API calls
41 42 43 |
# File 'lib/ruby_reddit_api/base.rb', line 41 def modhash Reddit::Base.modhash end |
#read(url, options = {}) ⇒ Reddit::Submission, ...
Base communication function for nearly all API calls
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby_reddit_api/base.rb', line 76 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
53 54 55 |
# File 'lib/ruby_reddit_api/base.rb', line 53 def user Reddit::Base.user end |
#user_agent ⇒ Object
Returns String.
64 65 66 |
# File 'lib/ruby_reddit_api/base.rb', line 64 def user_agent self.class.user_agent end |