Class: Reddit::Base

Inherits:
Object
  • Object
show all
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.

Author:

  • James Cook

Direct Known Subclasses

Api, Thing, Vote

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  @debug    = StringIO.new
end

Class Attribute Details

Returns the value of attribute cookie.



10
11
12
# File 'lib/ruby_reddit_api/base.rb', line 10

def cookie
  @cookie
end

.modhashObject (readonly)

Returns the value of attribute modhash.



10
11
12
# File 'lib/ruby_reddit_api/base.rb', line 10

def modhash
  @modhash
end

.throttle_durationObject (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

.userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'lib/ruby_reddit_api/base.rb', line 10

def user
  @user
end

.user_idObject (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

#debugObject (readonly)

Returns the value of attribute debug.



8
9
10
# File 'lib/ruby_reddit_api/base.rb', line 8

def debug
  @debug
end

#last_actionObject (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

.base_headersObject



124
125
126
# File 'lib/ruby_reddit_api/base.rb', line 124

def base_headers
  {'Cookie' => Reddit::Base.cookie.to_s, 'user-agent' => user_agent}
end

.user_agentObject



128
129
130
# File 'lib/ruby_reddit_api/base.rb', line 128

def user_agent
  "Ruby Reddit Client v#{Reddit::VERSION}"
end

Instance Method Details

#base_headersObject

HTTP headers to be sent in all API requests. At a minimum, ‘User-agent’ and ‘Cookie’ are needed.

Returns:

  • Hash



72
73
74
# File 'lib/ruby_reddit_api/base.rb', line 72

def base_headers
  self.class.base_headers
end

Returns:

  • (String, nil)


37
38
39
# File 'lib/ruby_reddit_api/base.rb', line 37

def cookie
  Reddit::Base.cookie
end

#inspectString

Returns:

  • (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

Returns:

  • (true, false)


61
62
63
# File 'lib/ruby_reddit_api/base.rb', line 61

def logged_in?
  !!(cookie && (cookie =~ /reddit_session/) != nil)
end

#loginBoolean

Login to Reddit and capture the cookie

Returns:

  • (Boolean)

    Login success or failure



25
26
27
28
# File 'lib/ruby_reddit_api/base.rb', line 25

def 
  capture_session(self.class.post( "/api/login", {:body => {:user => @user, :passwd => @password}, :debug_output => @debug} ) )
  logged_in?
end

#logoutnil

Remove the cookie to effectively logout of Reddit

Returns:

  • (nil)


32
33
34
# File 'lib/ruby_reddit_api/base.rb', line 32

def logout
  Reddit::Base.instance_variable_set("@cookie",nil)
end

#modhashString?

A kind of authenticity token for many API calls

Returns:

  • (String, nil)


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

Returns:



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, options={})
  unless throttled?
    @debug.rewind
    verb      = (options[:verb] || "get")
    param_key = (verb == "get") ? :query : :body
    resp      = self.class.send( verb, url, {param_key => (options[param_key] || {}), :headers => base_headers, :debug_output => @debug})
    if valid_response?(resp)
      @last_action = Time.now
      klass = Reddit.const_get(options[:handler] || "Submission")
      resp  = klass.parse( JSON.parse(resp.body, :max_nesting => 9_999) )
      return resp
    else
      return false
    end
  end
end

#userString

Logged in user

Returns:

  • (String)


55
56
57
# File 'lib/ruby_reddit_api/base.rb', line 55

def user
  Reddit::Base.user
end

#user_agentObject

Returns String.

Returns:

  • String



66
67
68
# File 'lib/ruby_reddit_api/base.rb', line 66

def user_agent
  self.class.user_agent
end

#user_idString

Reddit’s displayed ID for the logged in user

Returns:

  • (String)


49
50
51
# File 'lib/ruby_reddit_api/base.rb', line 49

def user_id
  Reddit::Base.user_id
end