Class: Wreddit
- Inherits:
-
Object
- Object
- Wreddit
- Defined in:
- lib/wreddit.rb,
lib/wreddit/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
- #comment(comment_id) ⇒ Object
- #comments(article_id = nil) ⇒ Object
- #descriptions ⇒ Object
- #html ⇒ Object
-
#initialize ⇒ Wreddit
constructor
A new instance of Wreddit.
-
#json ⇒ Object
parse helpers.
-
#links ⇒ Object
unique parsing (currently only works in JSON).
-
#parse(method = nil) ⇒ Object
general parsing.
-
#subreddit(subreddit_name = "all") ⇒ Object
standard requests.
- #title(title_name) ⇒ Object
- #titles ⇒ Object
- #user(user_name) ⇒ Object
- #xml ⇒ Object
Constructor Details
#initialize ⇒ Wreddit
Returns a new instance of Wreddit.
7 8 9 |
# File 'lib/wreddit.rb', line 7 def initialize @uri = "https://www.reddit.com" end |
Instance Method Details
#comment(comment_id) ⇒ Object
36 37 38 39 |
# File 'lib/wreddit.rb', line 36 def comment(comment_id) @uri += "/#{comment_id}" self end |
#comments(article_id = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/wreddit.rb', line 21 def comments(article_id = nil) # gets all comments if article_id is unspecified @uri += "/comments" # add article_id if article is specified if article_id @uri += "/#{article_id}" end self end |
#descriptions ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wreddit.rb', line 68 def descriptions descriptions = [] response = self.json if response.code == 200 response['data']['children'].each do |child| descriptions.push(child['data']['selftext']) end return descriptions else return response end end |
#html ⇒ Object
90 91 92 |
# File 'lib/wreddit.rb', line 90 def html HTTParty.get(URI(@uri)) end |
#json ⇒ Object
parse helpers
82 83 84 |
# File 'lib/wreddit.rb', line 82 def json HTTParty.get(URI(@uri + '.json')) end |
#links ⇒ Object
unique parsing (currently only works in JSON)
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wreddit.rb', line 42 def links links = [] response = self.json if response.code == 200 response['data']['children'].each do |child| links.push(child['data']['url']) end return links else return response end end |
#parse(method = nil) ⇒ Object
general parsing
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wreddit.rb', line 95 def parse(method = nil) # method of parsing your request case method when "json" self.json when "xml" self.xml when "html" self.html else # assume the user wants it in JSON format self.json end end |
#subreddit(subreddit_name = "all") ⇒ Object
standard requests
11 12 13 14 |
# File 'lib/wreddit.rb', line 11 def subreddit(subreddit_name = "all") @uri += "/r/#{subreddit_name}" self end |
#title(title_name) ⇒ Object
31 32 33 34 |
# File 'lib/wreddit.rb', line 31 def title(title_name) @uri += "/#{title_name}" self end |
#titles ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wreddit.rb', line 55 def titles titles = [] response = self.json if response.code == 200 response['data']['children'].each do |child| titles.push(child['data']['title']) end return titles else return response end end |
#user(user_name) ⇒ Object
16 17 18 19 |
# File 'lib/wreddit.rb', line 16 def user(user_name) @uri += "/u/#{user_name}" self end |
#xml ⇒ Object
86 87 88 |
# File 'lib/wreddit.rb', line 86 def xml HTTParty.get(URI(@uri + '.xml')) end |