Class: Wreddit
- Inherits:
-
Object
- Object
- Wreddit
- Includes:
- HTTParty
- Defined in:
- lib/wreddit.rb,
lib/wreddit/version.rb
Constant Summary collapse
- VERSION =
"1.1.2"
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.
9 10 11 12 |
# File 'lib/wreddit.rb', line 9 def initialize @client = Redis.new @uri = "https://www.reddit.com" end |
Instance Method Details
#comment(comment_id) ⇒ Object
39 40 41 42 |
# File 'lib/wreddit.rb', line 39 def comment(comment_id) @uri += "/#{comment_id}" self end |
#comments(article_id = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/wreddit.rb', line 24 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
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/wreddit.rb', line 71 def descriptions descriptions = [] response = JSON.parse(self.json) unless response['data']['children'].nil? response['data']['children'].each do |child| descriptions.push(child['data']['selftext']) end return descriptions else return response end end |
#html ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/wreddit.rb', line 126 def html res = @client.get(@uri) if res # if res exists return res else # if res doesn't exist get it from HTTParty res = HTTParty.get(URI(@uri)) if res.code == 200 # if res is an OK (we are getting data) @client.set(@uri, res) else # return 401, 429, or 500 error codes return res end return res end end |
#json ⇒ Object
parse helpers
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wreddit.rb', line 85 def json response = '' @uri+='.json' res = @client.get(@uri) if res # if res exists return res else # if res doesn't exist get it from HTTParty res = HTTParty.get(URI(@uri)) if res.code == 200 # if res is an OK (we are getting data) @client.set(@uri, res, ex: 1) else # return 401, 429, or 500 error codes return res end return res end end |
#links ⇒ Object
unique parsing (currently only works in JSON)
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wreddit.rb', line 45 def links links = [] response = JSON.parse(self.json) unless response['data']['children'].nil? 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/wreddit.rb', line 146 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
14 15 16 17 |
# File 'lib/wreddit.rb', line 14 def subreddit(subreddit_name = "all") @uri += "/r/#{subreddit_name}" self end |
#title(title_name) ⇒ Object
34 35 36 37 |
# File 'lib/wreddit.rb', line 34 def title(title_name) @uri += "/#{title_name}" self end |
#titles ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wreddit.rb', line 58 def titles titles = [] response = JSON.parse(self.json) unless response['data']['children'].nil? response['data']['children'].each do |child| titles.push(child['data']['title']) end return titles else return response end end |
#user(user_name) ⇒ Object
19 20 21 22 |
# File 'lib/wreddit.rb', line 19 def user(user_name) @uri += "/u/#{user_name}" self end |
#xml ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/wreddit.rb', line 106 def xml @uri+='.xml' res = @client.get(@uri) if res # if res exists return res else # if res doesn't exist get it from HTTParty res = HTTParty.get(URI(@uri)) if res.code == 200 # if res is an OK (we are getting data) @client.set(@uri, res) else # return 401, 429, or 500 error codes return res end return res end end |