Class: Reddit::Api

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_reddit_api/api.rb

Overview

Author:

  • James Cook

Direct Known Subclasses

User

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

base_headers, #base_headers, #cookie, #logged_in?, #login, #logout, #modhash, #read, #user_agent, user_agent, #user_id

Constructor Details

#initialize(user = nil, password = nil, options = {}) ⇒ Api

Returns a new instance of Api.



8
9
10
11
12
# File 'lib/ruby_reddit_api/api.rb', line 8

def initialize(user=nil,password=nil, options={})
  @user     = user
  @password = password
  @debug    = StringIO.new
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



6
7
8
# File 'lib/ruby_reddit_api/api.rb', line 6

def debug
  @debug
end

#last_actionObject (readonly)

Returns the value of attribute last_action.



6
7
8
# File 'lib/ruby_reddit_api/api.rb', line 6

def last_action
  @last_action
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/ruby_reddit_api/api.rb', line 5

def password
  @password
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'lib/ruby_reddit_api/api.rb', line 5

def user
  @user
end

Instance Method Details

#browse(subreddit, options = {}) ⇒ Array<Reddit::Submission>

Browse submissions by subreddit

Parameters:

  • Subreddit (String)

    to browse

Returns:



21
22
23
24
25
26
27
28
# File 'lib/ruby_reddit_api/api.rb', line 21

def browse(subreddit, options={})
  subreddit = sanitize_subreddit(subreddit)
  options.merge! :handler => "Submission"
  if options[:limit]
    options.merge!({:query => {:limit => options[:limit]}})
  end
  read("/r/#{subreddit}.json", options )
end

#commentsArray<Reddit::Message>

Read received comments

Returns:



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

def comments
  messages :comments
end

#inspectObject



14
15
16
# File 'lib/ruby_reddit_api/api.rb', line 14

def inspect
  "<Reddit::Api>"
end

#post_repliesArray<Reddit::Message>

Read post replies

Returns:



78
79
80
# File 'lib/ruby_reddit_api/api.rb', line 78

def post_replies
  messages :selfreply
end

#received_messagesArray<Reddit::Message>

Read received messages

Returns:



60
61
62
# File 'lib/ruby_reddit_api/api.rb', line 60

def received_messages
  messages :inbox
end

#search(terms = nil, options = {}) ⇒ Array<Reddit::Submission>

Search reddit

Examples:

search("programming", :in => "ruby", :sort => "relevance")

Parameters:

  • Search (String, Hash)

    terms and options

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_reddit_api/api.rb', line 35

def search(terms=nil, options={})
  http_options = {:verb => "get", :query => {}}
  subreddit    = options[:in]
  sort         = options.fetch(:sort){ "relevance" }
  http_options[:query].merge!({:sort => sort})

  if subreddit
    http_options[:query].merge!({:restrict_sr => "1"})
  end

  if terms
    http_options[:query].merge!({:q => terms})
  end
  path = subreddit.to_s == "" ? "/search.json" : "/r/#{subreddit}/search.json"
  read(path, http_options)
end

#sent_messagesArray<Reddit::Message>

Read sent messages

Returns:



54
55
56
# File 'lib/ruby_reddit_api/api.rb', line 54

def sent_messages
  messages :sent
end

#unread_messagesArray<Reddit::Message>

Read unread messages

Returns:



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

def unread_messages
  messages :unread
end