NeonRAW 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/NeonRAW.gemspec +5 -4
  4. data/lib/NeonRAW/clients/base.rb +9 -7
  5. data/lib/NeonRAW/clients/base/objectbuilder.rb +8 -5
  6. data/lib/NeonRAW/clients/base/utilities.rb +36 -3
  7. data/lib/NeonRAW/clients/installed.rb +2 -0
  8. data/lib/NeonRAW/clients/script.rb +2 -0
  9. data/lib/NeonRAW/clients/web.rb +2 -0
  10. data/lib/NeonRAW/errors.rb +49 -58
  11. data/lib/NeonRAW/objects/all.rb +20 -2
  12. data/lib/NeonRAW/objects/comment.rb +19 -5
  13. data/lib/NeonRAW/objects/inboxcomment.rb +4 -1
  14. data/lib/NeonRAW/objects/me.rb +17 -28
  15. data/lib/NeonRAW/objects/modlogaction.rb +3 -0
  16. data/lib/NeonRAW/objects/modloguser.rb +3 -0
  17. data/lib/NeonRAW/objects/morecomments.rb +15 -12
  18. data/lib/NeonRAW/objects/multireddit.rb +4 -1
  19. data/lib/NeonRAW/objects/popular.rb +57 -0
  20. data/lib/NeonRAW/objects/privatemessage.rb +6 -3
  21. data/lib/NeonRAW/objects/rule.rb +3 -0
  22. data/lib/NeonRAW/objects/submission.rb +44 -5
  23. data/lib/NeonRAW/objects/subreddit.rb +9 -10
  24. data/lib/NeonRAW/objects/subreddit/flair.rb +7 -4
  25. data/lib/NeonRAW/objects/subreddit/moderation.rb +8 -8
  26. data/lib/NeonRAW/objects/subreddit/utilities.rb +44 -27
  27. data/lib/NeonRAW/objects/thing/inboxable.rb +1 -1
  28. data/lib/NeonRAW/objects/thing/moderateable.rb +7 -4
  29. data/lib/NeonRAW/objects/thing/refreshable.rb +1 -3
  30. data/lib/NeonRAW/objects/thing/votable.rb +1 -1
  31. data/lib/NeonRAW/objects/user.rb +51 -19
  32. data/lib/NeonRAW/objects/wikipage.rb +5 -2
  33. data/lib/NeonRAW/objects/wikipagerevision.rb +4 -1
  34. data/lib/NeonRAW/version.rb +1 -1
  35. metadata +25 -10
@@ -33,8 +33,8 @@ module NeonRAW
33
33
  # @note This attribute only exists if the comment is fetched from outside
34
34
  # the thread it was posted in (so like user pages,
35
35
  # /r/subreddit/comments, that type of stuff).
36
- # @!attribute [r] link_id
37
- # @return [String] Returns the id of the link that this comment is in.
36
+ # @!attribute [r] link_name
37
+ # @return [String] Returns the name of the link that this comment is in.
38
38
  # @!attribute [r] link_title
39
39
  # @return [String] Returns the title of the parent link.
40
40
  # @note This attribute only exists if the comment is fetched from outside
@@ -48,8 +48,8 @@ module NeonRAW
48
48
  # @!attribute [r] num_reports
49
49
  # @return [Integer, nil] Returns the number of times the comment has been
50
50
  # reported or nil if it hasn't or you aren't a moderator.
51
- # @!attribute [r] parent_id
52
- # @return [String] Returns the ID of either the link or the comment that
51
+ # @!attribute [r] parent_name
52
+ # @return [String] Returns the name of either the link or the comment that
53
53
  # this comment is a reply to.
54
54
  # @!attribute [r] saved?
55
55
  # @return [Boolean] Returns whether or not you saved the comment.
@@ -81,9 +81,12 @@ module NeonRAW
81
81
  def initialize(client, data)
82
82
  @client = client
83
83
  data.each do |key, value|
84
+ # for consistency, empty strings/arrays/hashes are set to nil
85
+ # because most of the keys returned by Reddit are nil when they
86
+ # don't have a value, besides a few
84
87
  value = nil if ['', [], {}].include?(value)
85
88
  instance_variable_set(:"@#{key}", value)
86
- next if key == :created || key == :created_utc || key == :replies
89
+ next if %i[created created_utc replies].include?(key)
87
90
  self.class.send(:attr_reader, key)
88
91
  end
89
92
  class << self
@@ -92,6 +95,8 @@ module NeonRAW
92
95
  alias_method :saved?, :saved
93
96
  alias_method :score_hidden?, :score_hidden
94
97
  alias_method :archived?, :archived
98
+ alias_method :link_name, :link_id
99
+ alias_method :parent_name, :parent_id
95
100
  end
96
101
  end
97
102
 
@@ -144,6 +149,15 @@ module NeonRAW
144
149
  end
145
150
  comments
146
151
  end
152
+
153
+ # Creates the comment's permalink.
154
+ # @!method permalink
155
+ # @return [String] Returns the comment's permalink.
156
+ def permalink
157
+ submission_id = link_id[3..-1] # trims the t3_ from the fullname
158
+ # the // is intentional
159
+ "https://www.reddit.com/r/#{subreddit}/comments/#{submission_id}//#{id}"
160
+ end
147
161
  end
148
162
  end
149
163
  end
@@ -40,9 +40,12 @@ module NeonRAW
40
40
  def initialize(client, data)
41
41
  @client = client
42
42
  data.each do |key, value|
43
+ # for consistency, empty strings/arrays/hashes are set to nil
44
+ # because most of the keys returned by Reddit are nil when they
45
+ # don't have a value, besides a few
43
46
  value = nil if ['', [], {}].include?(value)
44
47
  instance_variable_set(:"@#{key}", value)
45
- next if key == :created || key == :created_utc
48
+ next if %i[created created_utc].include?(key)
46
49
  self.class.send(:attr_reader, key)
47
50
  end
48
51
  class << self
@@ -1,7 +1,7 @@
1
1
  require_relative 'user'
2
2
  require_relative 'trophy'
3
3
  require_relative 'multireddit'
4
- # rubocop:disable Metrics/AbcSize, Metrics/ClassLength
4
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
5
5
  # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6
6
 
7
7
  module NeonRAW
@@ -34,10 +34,15 @@ module NeonRAW
34
34
  # @param data [Hash] The object data.
35
35
  def initialize(client, data)
36
36
  @client = client
37
+ data[:username] = data.delete(:name) # this is for consistency
38
+ data[:name] = 't2_' + data[:id]
37
39
  data.each do |key, value|
40
+ # for consistency, empty strings/arrays/hashes are set to nil
41
+ # because most of the keys returned by Reddit are nil when they
42
+ # don't have a value, besides a few
38
43
  value = nil if ['', [], {}].include?(value)
39
44
  instance_variable_set(:"@#{key}", value)
40
- next if key == :created || key == :created_utc
45
+ next if %i[created created_utc].include?(key)
41
46
  self.class.send(:attr_reader, key)
42
47
  end
43
48
  class << self
@@ -70,7 +75,7 @@ module NeonRAW
70
75
  # @option params :limit [1..1000] The number of listing items to fetch.
71
76
  # @option params :show [String] Literally the string 'all'.
72
77
  # @return [NeonRAW::Objects::Listing] Returns a listing with all your PMs.
73
- %w(messages inbox unread sent).each do |type|
78
+ %w[messages inbox unread sent].each do |type|
74
79
  define_method :"#{type}" do |params = { limit: 25 }|
75
80
  @client.send(:build_listing, "/message/#{type}", params)
76
81
  end
@@ -88,7 +93,7 @@ module NeonRAW
88
93
  # @return [NeonRAW::Objects::Listing] Returns a listing with all your
89
94
  # modmails.
90
95
  def modmail(params = { limit: 25 })
91
- @client.send(:build_listing, '/message/moderator.json', params)
96
+ @client.send(:build_listing, '/message/moderator', params)
92
97
  end
93
98
 
94
99
  # Fetches your subreddits.
@@ -104,7 +109,7 @@ module NeonRAW
104
109
  # @option params :show [String] Literally the string 'all'.
105
110
  # @return [NeonRAW::Objects::Listing] Returns a listing with all your
106
111
  # subreddits.
107
- %w(subscribed contributed moderated).each do |type|
112
+ %w[subscribed contributed moderated].each do |type|
108
113
  define_method :"#{type}" do |params = { limit: 25 }|
109
114
  type = 'subscriber' if type == 'subscribed'
110
115
  type = 'contributor' if type == 'contributed'
@@ -144,12 +149,8 @@ module NeonRAW
144
149
  # @!method trophies
145
150
  # @return [Array<NeonRAW::Objects::Trophy>] Returns a list of trophies.
146
151
  def trophies
147
- data_arr = []
148
152
  data = @client.request_data('/api/v1/me/trophies', :get)[:data]
149
- data[:trophies].each do |trophy|
150
- data_arr << Trophy.new(trophy[:data])
151
- end
152
- data_arr
153
+ data[:trophies].map { |trophy| Trophy.new(trophy[:data]) }
153
154
  end
154
155
 
155
156
  # Fetches your friends.
@@ -163,12 +164,8 @@ module NeonRAW
163
164
  # @return [Array<Hash<Float, String, String>>] Returns the list of your
164
165
  # friends.
165
166
  def friends(params = { limit: 25 })
166
- data_arr = []
167
167
  data = @client.request_data('/prefs/friends', :get, params)
168
- data[0][:data][:children].each do |friend|
169
- data_arr << friend
170
- end
171
- data_arr
168
+ data[0][:data][:children].map { |friend| friend }
172
169
  end
173
170
 
174
171
  # Fetches your blocked users.
@@ -182,12 +179,8 @@ module NeonRAW
182
179
  # @return [Array<Hash<Float, String, String>>] Returns the list of your
183
180
  # blocked users.
184
181
  def blocked(params = { limit: 25 })
185
- data_arr = []
186
182
  data = @client.request_data('/prefs/blocked', :get, params)
187
- data[:data][:children].each do |blocked|
188
- data_arr << blocked
189
- end
190
- data_arr
183
+ data[:data][:children].map { |blocked| blocked }
191
184
  end
192
185
 
193
186
  # Mark all your messages as "read."
@@ -201,19 +194,15 @@ module NeonRAW
201
194
  # @return [Array<NeonRAW::Objects::MultiReddit>] Returns a list of
202
195
  # multireddits.
203
196
  def multireddits
204
- data_arr = []
205
197
  params = { expand_srs: false }
206
198
  data = @client.request_data('/api/multi/mine', :get, params)
207
- data.each do |multireddit|
208
- data_arr << MultiReddit.new(@client, multireddit[:data])
209
- end
210
- data_arr
199
+ data.map { |multireddit| MultiReddit.new(@client, multireddit[:data]) }
211
200
  end
212
201
 
213
202
  # Goes through and edits then deletes your post history. Defaults to
214
203
  # 2 weeks.
215
204
  # @!method purge(queue, params = {})
216
- # @param queue [String] The queue you want to get your posts from
205
+ # @param queue [Symbol] The queue you want to get your posts from
217
206
  # [overview, submitted, comments, upvoted, downvoted, hidden, saved,
218
207
  # giled]
219
208
  # @param params [Hash] The additional parameters.
@@ -240,9 +229,9 @@ module NeonRAW
240
229
  next if item.created < params[:age]
241
230
  next unless whitelist.include?(item.subreddit) || whitelist[0] == '*'
242
231
  if item.is_a?(Submission)
243
- item.edit! params[:edit] if item.selfpost? && !item.archived?
232
+ item.edit! params[:edit] if item.selfpost?
244
233
  else
245
- item.edit! params[:edit] unless item.archived?
234
+ item.edit! params[:edit]
246
235
  end
247
236
  item.delete!
248
237
  end
@@ -41,6 +41,9 @@ module NeonRAW
41
41
  def initialize(client, data)
42
42
  @client = client
43
43
  data.each do |key, value|
44
+ # for consistency, empty strings/arrays/hashes are set to nil
45
+ # because most of the keys returned by Reddit are nil when they
46
+ # don't have a value, besides a few
44
47
  value = nil if ['', [], {}].include?(value)
45
48
  instance_variable_set(:"@#{key}", value)
46
49
  next if key == :created_utc
@@ -17,6 +17,9 @@ module NeonRAW
17
17
  def initialize(client, data)
18
18
  @client = client
19
19
  data.each do |key, value|
20
+ # for consistency, empty strings/arrays/hashes are set to nil
21
+ # because most of the keys returned by Reddit are nil when they
22
+ # don't have a value, besides a few
20
23
  value = nil if ['', [], {}].include?(value)
21
24
  instance_variable_set(:"@#{key}", value)
22
25
  next if key == :date
@@ -7,6 +7,9 @@ module NeonRAW
7
7
  def initialize(client, data)
8
8
  @client = client
9
9
  data.each do |key, value|
10
+ # for consistency, empty strings/arrays/hashes are set to nil
11
+ # because most of the keys returned by Reddit are nil when they
12
+ # don't have a value, besides a few
10
13
  value = nil if ['', [], {}].include?(value)
11
14
  instance_variable_set(:"@#{key}", value)
12
15
  self.class.send(:attr_reader, key)
@@ -20,21 +23,21 @@ module NeonRAW
20
23
  true
21
24
  end
22
25
 
26
+ # Returns whether or not the object is a Comment object.
27
+ # @!method comment?
28
+ # @return [Boolean] Returns false.
29
+ def comment?
30
+ false
31
+ end
32
+
23
33
  # Expands the MoreComments object.
24
- # @!method expand(subreddit)
25
- # @param subreddit [String] The name of the subreddit where the
26
- # MoreComments object resides.
27
- # @return [Array] Returns a list of the comments that were expanded.
28
- def expand(subreddit)
29
- comments = []
34
+ # @!method expand
35
+ # @return [NeonRAW::Objects::Listing] Returns a listing with all of the
36
+ # comments that were expanded.
37
+ def expand
30
38
  return [] if children.nil?
31
- params = { id: children.map { |the_id| 't1_' + the_id }.join(',') }
32
39
  # /api/morechildren is buggy shit. This is better.
33
- data = @client.request_data("/r/#{subreddit}/api/info", :get, params)
34
- data[:data][:children].each do |comment|
35
- comments << Comment.new(@client, comment[:data])
36
- end
37
- comments
40
+ @client.info(name: children.map { |the_id| 't1_' + the_id }.join(','))
38
41
  end
39
42
  end
40
43
  end
@@ -46,9 +46,12 @@ module NeonRAW
46
46
  def initialize(client, data)
47
47
  @client = client
48
48
  data.each do |key, value|
49
+ # for consistency, empty strings/arrays/hashes are set to nil
50
+ # because most of the keys returned by Reddit are nil when they
51
+ # don't have a value, besides a few
49
52
  value = nil if ['', [], {}].include?(value)
50
53
  instance_variable_set(:"@#{key}", value)
51
- next if key == :created || key == :created_utc || key == :subreddits
54
+ next if %i[created created_utc subreddits].include?(key)
52
55
  self.class.send(:attr_reader, key)
53
56
  end
54
57
  class << self
@@ -0,0 +1,57 @@
1
+ module NeonRAW
2
+ module Objects
3
+ # The object for /r/popular.
4
+ class Popular
5
+ # @!method initialize(client)
6
+ # @param client [NeonRAW::Clients::Web/Installed/Script] The client
7
+ # object.
8
+ def initialize(client)
9
+ @client = client
10
+ end
11
+
12
+ # @!group Listings
13
+ # Fetches a listing from /r/popular.
14
+ # @!method hot(params = { limit: 25 })
15
+ # @!method rising(params = { limit: 25 })
16
+ # @!method top(params = { limit: 25 })
17
+ # @!method old(params = { limit: 25 })
18
+ # @!method new(params = { limit: 25 })
19
+ # @!method controversial(params = { limit: 25 })
20
+ # @!method comments(param = { limit: 25 })
21
+ # @param params [Hash] The parameters for the request.
22
+ # @option params :t [String] Time for relevant sorting [hour, day, week,
23
+ # month, year, all]
24
+ # @option params :after [String] The name of the next data block.
25
+ # @option params :before [String] The name of the previous data block.
26
+ # @option params :count [Integer] The number of items already in the
27
+ # listing.
28
+ # @option params :limit [1..1000] The number of items to fetch.
29
+ # @option params :show [String] Literally the string 'all'.
30
+ # @return [NeonRAW::Objects::Listing] Returns the listing object.
31
+ %w[hot rising top old new controversial comments].each do |type|
32
+ define_method :"#{type}" do |params = { limit: 25 }|
33
+ path = "/r/popular/#{type}"
34
+ @client.send(:build_listing, path, params)
35
+ end
36
+ end
37
+
38
+ # Streams content from /r/popular.
39
+ # @!method stream(queue, params = { limit: 25 })
40
+ # @param queue [Symbol] The queue to get data from [hot, top, new,
41
+ # controversial, gilded, comments]
42
+ # @param params [Hash] The parameters for the request.
43
+ # @option params :t [String] Time for relevant sorting [hour, day, week,
44
+ # month, year, all]
45
+ # @option params :after [String] The name of the next data block.
46
+ # @option params :before [String] The name of the previous data block.
47
+ # @option params :count [Integer] The number of items already in the
48
+ # listing.
49
+ # @option params :limit [1..1000] The number of items to fetch.
50
+ # @option params :show [String] Literally the string 'all'.
51
+ # @return [Enumerator] Returns an enumerator for the streamed data.
52
+ def stream(queue, params = { limit: 25 })
53
+ @client.send(:stream, "/r/popular/#{queue}", params)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -36,9 +36,12 @@ module NeonRAW
36
36
  def initialize(client, data)
37
37
  @client = client
38
38
  data.each do |key, value|
39
+ # for consistency, empty strings/arrays/hashes are set to nil
40
+ # because most of the keys returned by Reddit are nil when they
41
+ # don't have a value, besides a few
39
42
  value = nil if ['', [], {}].include?(value)
40
43
  instance_variable_set(:"@#{key}", value)
41
- next if key == :created || key == :created_utc || key == :replies
44
+ next if %i[created created_utc replies].include?(key)
42
45
  self.class.send(:attr_reader, key)
43
46
  end
44
47
  class << self
@@ -69,7 +72,7 @@ module NeonRAW
69
72
  # Toggle the read status of a message.
70
73
  # @!method mark_as_read!
71
74
  # @!method mark_as_unread!
72
- %w(read unread).each do |type|
75
+ %w[read unread].each do |type|
73
76
  define_method :"mark_as_#{type}!" do
74
77
  params = { id: name }
75
78
  @client.request_data("/api/#{type}_message", :post, params)
@@ -79,7 +82,7 @@ module NeonRAW
79
82
  # Set whether to mute a user in modmail or not.
80
83
  # @!method mute!
81
84
  # @!method unmute!
82
- %w(mute unmute).each do |type|
85
+ %w[mute unmute].each do |type|
83
86
  define_method :"#{type}!" do
84
87
  params = { id: name }
85
88
  @client.request_data("/api/#{type}_message_author", :post, params)
@@ -23,6 +23,9 @@ module NeonRAW
23
23
  def initialize(client, data)
24
24
  @client = client
25
25
  data.each do |key, value|
26
+ # for consistency, empty strings/arrays/hashes are set to nil
27
+ # because most of the keys returned by Reddit are nil when they
28
+ # don't have a value, besides a few
26
29
  value = nil if ['', [], {}].include?(value)
27
30
  instance_variable_set(:"@#{key}", value)
28
31
  next if key == :created_utc
@@ -1,6 +1,10 @@
1
1
  require_relative 'thing'
2
2
  require_relative 'comment'
3
3
  require_relative 'morecomments'
4
+ require_relative 'listing'
5
+ require_relative 'submission'
6
+
7
+ # rubocop:disable Metrics/MethodLength, Metrics/ClassLength
4
8
 
5
9
  module NeonRAW
6
10
  module Objects
@@ -84,9 +88,16 @@ module NeonRAW
84
88
  def initialize(client, data)
85
89
  @client = client
86
90
  data.each do |key, value|
91
+ # for consistency, empty strings/arrays/hashes are set to nil
92
+ # because most of the keys returned by Reddit are nil when they
93
+ # don't have a value, besides a few
87
94
  value = nil if ['', [], {}].include?(value)
88
- instance_variable_set(:"@#{key}", value)
89
- next if key == :created || key == :created_utc
95
+ if key == :permalink
96
+ instance_variable_set(:"@#{key}", 'https://www.reddit.com' + value)
97
+ else
98
+ instance_variable_set(:"@#{key}", value)
99
+ end
100
+ next if %i[created created_utc].include?(key)
90
101
  self.class.send(:attr_reader, key)
91
102
  end
92
103
  class << self
@@ -134,7 +145,7 @@ module NeonRAW
134
145
  # @return [Array] Returns an array full of Comments and MoreComments
135
146
  # objects.
136
147
  def comments
137
- data = @client.request_data("/comments/#{id}/.json", :get)
148
+ data = @client.request_data("/comments/#{id}", :get)
138
149
  data_arr = []
139
150
  data[1][:data][:children].each do |comment|
140
151
  if comment[:kind] == 't1'
@@ -146,6 +157,34 @@ module NeonRAW
146
157
  data_arr
147
158
  end
148
159
 
160
+ # Fetches duplicates for the submission.
161
+ # @!method duplicates(params = { limit: 25 })
162
+ # @param params [Hash] Optional parameters.
163
+ # @option :after [String] The fullname of the next data block.
164
+ # @option :before [String] The fullname of the previous data block.
165
+ # @option :count [Integer] The number of posts already in the listing.
166
+ # @option :limit [1..1000] The number of listing items to fetch.
167
+ # @option :show [String] Literally the string 'all'.
168
+ # @return [NeonRAW::Objects::Listing] Returns the listing with all the
169
+ # duplicate submissions.
170
+ def duplicates(params = { limit: 25 })
171
+ params[:sr_detail] = false
172
+ data_arr = []
173
+ until data_arr.length == params[:limit]
174
+ data = @client.request_data("/duplicates/#{id}", :get, params)
175
+ params[:after] = data[1][:data][:after]
176
+ params[:before] = data[1][:data][:before]
177
+ data[1][:data][:children].each do |submission|
178
+ data_arr << Submission.new(@client, submission[:data])
179
+ break if data_arr.length == params[:limit]
180
+ end
181
+ break if params[:after].nil?
182
+ end
183
+ listing = Listing.new(params[:after], params[:before])
184
+ data_arr.each { |submission| listing << submission }
185
+ listing
186
+ end
187
+
149
188
  # Set submission visibility.
150
189
  # @!method hide
151
190
  # @!method unhide
@@ -154,7 +193,7 @@ module NeonRAW
154
193
  # Set whether or not users can comment on the submission.
155
194
  # @!method lock
156
195
  # @!method unlock
157
- %w(hide unhide lock unlock).each do |type|
196
+ %w[hide unhide lock unlock].each do |type|
158
197
  define_method :"#{type}" do
159
198
  params = { id: name }
160
199
  @client.request_data("/api/#{type}", :post, params)
@@ -164,7 +203,7 @@ module NeonRAW
164
203
  # Set the submission's NSFW status.'
165
204
  # @!method mark_nsfw
166
205
  # @!method unmark_nsfw
167
- %w(mark unmark).each do |type|
206
+ %w[mark unmark].each do |type|
168
207
  define_method :"#{type}_nsfw" do
169
208
  params = { id: name }
170
209
  @client.request_data("/api/#{type}nsfw", :post, params)