Class: Wreddit

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/wreddit.rb,
lib/wreddit/version.rb

Constant Summary collapse

VERSION =
"1.1.4"

Instance Method Summary collapse

Constructor Details

#initializeWreddit

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

#descriptionsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wreddit.rb', line 79

def descriptions
  descriptions = []
  begin
    response = JSON.parse(self.json)
  rescue JSON::ParserError, TypeError => e
    puts e
  end
  unless response['data']['children'].nil?
    response['data']['children'].each do |child|
      descriptions.push(child['data']['selftext'])
    end
    return descriptions
  else
    return response
  end
end

#htmlObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wreddit.rb', line 138

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

#jsonObject

parse helpers



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wreddit.rb', line 97

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

unique parsing (currently only works in JSON)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wreddit.rb', line 45

def links
  links = []
  begin
    response = JSON.parse(self.json)
  rescue JSON::ParserError, TypeError => e
    puts e
  end
  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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/wreddit.rb', line 158

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

#titlesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wreddit.rb', line 62

def titles
  titles = []
  begin
    response = JSON.parse(self.json)
  rescue JSON::ParserError, TypeError => e
    puts e
  end
  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

#xmlObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wreddit.rb', line 118

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