redd 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41d1b17c6950acfb37e70062b5ef884ccd842b48
4
- data.tar.gz: f5d053a918a167558cb3b4d22bf0939d42fb8f5e
3
+ metadata.gz: 36eb57cd9b39fd9bc40fa5fb242f0ce23808c623
4
+ data.tar.gz: 167e62283d79e46b12e3bdc55f44acbf8f87703e
5
5
  SHA512:
6
- metadata.gz: 57df8f09c68ab163d15c4589a2f268b3fd393c63d4afcb04977dd89842af88d6c0b4d84f2e76e35e289ca878430cfa4e810611c399714e3062c11ebdc6c423fd
7
- data.tar.gz: 341a6eae53925c15d7a70fa07e241fdba578fc099c0c11d2e3a0b18c1e4bd59f4daf46d502e24fa664a9073221869edd0943776a8f7355d837a0049300f1dd8f
6
+ metadata.gz: f94502a932a54d1f6c63bb14ae42f08897ba3e5a4958f211119ce71ad3bdb72a96f2b2699ae31856a66c6c5baf8ac5f5be90050f18b5cbb63f9457afe33db0a3
7
+ data.tar.gz: b17c0e4d62fc82f8dd31c62d25ecf4e38e87f59071b54229e96f4ec3a68954d143ec2b7d76b2fe251375dbc38030750df6e8cf0065cf854caad2adf4053b7bde
File without changes
data/README.md CHANGED
@@ -136,6 +136,6 @@ Redd::Objects::Submission.include(MyGildingExtension)
136
136
  ```ruby
137
137
  # Copyright.rb
138
138
  #
139
- # Copyright (c) Avinash Dwarapu under the MIT License. See LICENSE.txt for more details.
140
- # Redd::Error has been modified from RedditKit.rb. See RedditKit.LICENSE.txt for its license.
139
+ # Copyright (c) Avinash Dwarapu under the MIT License. See LICENSE.md for more details.
140
+ # Redd::Error has been modified from RedditKit.rb. See RedditKit.LICENSE.md for its license.
141
141
  ```
@@ -1,7 +1,3 @@
1
- error.rb would have been impossible if it weren't for @samsymons :)
2
-
3
- ---
4
-
5
1
  Copyright (c) 2013 Sam Symons
6
2
 
7
3
  MIT License
@@ -130,6 +130,7 @@ module Redd
130
130
  @middleware ||= Faraday::RackBuilder.new do |builder|
131
131
  builder.use Response::RaiseError
132
132
  builder.use Response::ParseJson
133
+ builder.use Faraday::Request::Multipart
133
134
  builder.use Faraday::Request::UrlEncoded
134
135
  builder.adapter Faraday.default_adapter
135
136
  end
@@ -56,19 +56,26 @@ module Redd
56
56
  bset = PRAWBoundedQueueSet.new(10)
57
57
  before = ""
58
58
  loop do
59
- # Get the latest comments from the subreddit. By the way, this line
60
- # is the one where the sleeping/rate-limiting happens.
61
- params = kwargs.merge(before: before)
62
- listing = send(meth, *args, **params)
63
- # Run the loop for each of the item in the listing
64
- listing.reverse_each do |thing|
65
- yield thing if bset.enqueue?(thing.fullname)
59
+ begin
60
+ # Get the latest comments from the subreddit.
61
+ params = kwargs.merge(before: before)
62
+ listing = send(meth, *args, **params)
63
+ # Run the loop for each of the item in the listing
64
+ listing.reverse_each do |thing|
65
+ yield thing if bset.enqueue?(thing.fullname)
66
+ end
67
+ # Set the latest comment.
68
+ before = listing.first.fullname unless listing.empty?
69
+ rescue Redd::Error::RateLimited => error
70
+ # If this error pops up, you probably have an issue with your bot.
71
+ sleep(error.time)
72
+ rescue Redd::Error => error
73
+ # 5-something errors are usually errors on reddit's end.
74
+ raise error unless (500...600).include?(error.code)
66
75
  end
67
- # Set the latest comment.
68
- before = listing.first.fullname unless listing.empty?
69
76
  end
70
77
  end
71
- end
72
- end
78
+ end # <- See, this is why I sometimes prefer Python.
79
+ end # <- Thank god for code folding.
73
80
  end
74
81
  end
@@ -3,41 +3,6 @@ module Redd
3
3
  class Base
4
4
  # Methods that require the "submit" scope
5
5
  module Submit
6
- # Submit a link or a text post to a subreddit.
7
- #
8
- # @param [Objects::Submission, String] subreddit The subreddit
9
- # to submit to.
10
- # @param [String] title The title of the submission.
11
- # @param [String] captcha A possible captcha result to send if one
12
- # is required.
13
- # @param [String] identifier The identifier for the captcha if one
14
- # is required.
15
- # @param [String] text The text of the self-post.
16
- # @param [String] url The URL of the link.
17
- # @param [Boolean] resubmit Whether to post a link to a subreddit
18
- # despite it having been posted there before (you monster).
19
- # @param [Boolean] sendreplies Whether to send the replies to your
20
- # inbox.
21
- # @return [Objects::Thing] The returned result (url, id and name).
22
- def submit(
23
- subreddit, title, captcha = nil, identifier = nil, text: nil,
24
- url: nil, resubmit: false, sendreplies: true
25
- )
26
-
27
- params = {
28
- extension: "json", title: title,
29
- resubmit: resubmit, sendreplies: sendreplies,
30
- sr: property(subreddit, :display_name)
31
- }
32
-
33
- params << {captcha: captcha, iden: identifier} if captcha
34
- params[:kind], params[:text] = :self, text if text
35
- params[:kind], params[:url] = :link, url if url
36
-
37
- response = post("/api/submit", params)
38
- Objects::Thing.new(self, response.body[:json][:data])
39
- end
40
-
41
6
  # Add a comment to a link, reply to a comment or reply to a message.
42
7
  # Bit of an all-purpose method, this one.
43
8
  # @param thing [Objects::Submission, Objects::Comment,
@@ -54,7 +54,7 @@ module Redd
54
54
  # @author Bryce Boe (@bboe) in Python
55
55
  # @return [Array<Objects::Comment, Objects::MoreComments>] A linear
56
56
  # array of the submission's comments or the comments' replies.
57
- def flat_comments(base)
57
+ def flat_comments(base)
58
58
  meth = (base.is_a?(Objects::Submission) ? :comments : :replies)
59
59
  stack = base.send(meth).dup
60
60
  flattened = []
@@ -63,7 +63,7 @@ module Redd
63
63
  comment = stack.shift
64
64
  if comment.is_a?(Objects::Comment)
65
65
  replies = comment.replies
66
- stack += replies if replies
66
+ stack = replies + stack if replies
67
67
  end
68
68
  flattened << comment
69
69
  end
@@ -11,8 +11,10 @@ module Redd
11
11
  include Hashie::Extensions::MethodQuery
12
12
  include Hashie::Extensions::DeepMerge
13
13
 
14
+ # The `delete` method is called `delete_path` because it conflicts with
15
+ # Hash#delete.
14
16
  extend Forwardable
15
- def_delegators :@client, :get, :post, :put, :delete
17
+ def_delegators :@client, :get, :post, :put, :delete_path
16
18
 
17
19
  # @!attribute [r] client
18
20
  # @return [Clients::Base] The client that used to make requests.
@@ -67,6 +67,38 @@ module Redd
67
67
  }
68
68
  )
69
69
  end
70
+
71
+ # Get the related articles.
72
+ # @param [Hash] params A list of params to send with the request.
73
+ # @option params [String] :after Return results after the given
74
+ # fullname.
75
+ # @option params [String] :before Return results before the given
76
+ # fullname.
77
+ # @option params [Integer] :count The number of items already seen
78
+ # in the listing.
79
+ # @option params [1..100] :limit The maximum number of things to
80
+ # return.
81
+ # @return [Objects::Listing<Objects::Thing>]
82
+ def get_related(**params)
83
+ related = get("/related/#{id}.json", params).body[1]
84
+ client.object_from_body(related)
85
+ end
86
+
87
+ # Get other articles with the same URL.
88
+ # @param [Hash] params A list of params to send with the request.
89
+ # @option params [String] :after Return results after the given
90
+ # fullname.
91
+ # @option params [String] :before Return results before the given
92
+ # fullname.
93
+ # @option params [Integer] :count The number of items already seen
94
+ # in the listing.
95
+ # @option params [1..100] :limit The maximum number of things to
96
+ # return.
97
+ # @return [Objects::Listing<Objects::Submission>]
98
+ def get_duplicates(**params)
99
+ duplicates = get("/duplicates/#{id}.json", params).body[1]
100
+ client.object_from_body(duplicates)
101
+ end
70
102
  end
71
103
  end
72
104
  end
@@ -1,3 +1,4 @@
1
+ require "fastimage"
1
2
  require_relative "thing"
2
3
 
3
4
  module Redd
@@ -14,6 +15,49 @@ module Redd
14
15
  alias_property :type, :subreddit_type
15
16
  alias_property :times_gilded, :gilded
16
17
 
18
+ # @!group Submissions
19
+
20
+ # Submit a link or a text post to the subreddit.
21
+ #
22
+ # @param [String] title The title of the submission.
23
+ # @param [String] captcha A possible captcha result to send if one
24
+ # is required.
25
+ # @param [String] identifier The identifier for the captcha if one
26
+ # is required.
27
+ # @param [String] text The text of the self-post.
28
+ # @param [String] url The URL of the link.
29
+ # @param [Boolean] resubmit Whether to post a link to the subreddit
30
+ # despite it having been posted there before (you monster).
31
+ # @param [Boolean] sendreplies Whether to send the replies to your
32
+ # inbox.
33
+ # @return [Objects::Thing] The returned result (url, id and name).
34
+ def submit(
35
+ title, captcha = nil, identifier = nil, text: nil, url: nil,
36
+ resubmit: false, sendreplies: true
37
+ )
38
+
39
+ params = {
40
+ extension: "json", title: title, sr: display_name,
41
+ resubmit: resubmit, sendreplies: sendreplies
42
+ }
43
+
44
+ params << {captcha: captcha, iden: identifier} if captcha
45
+ params[:kind], params[:text] = :self, text if text
46
+ params[:kind], params[:url] = :link, url if url
47
+
48
+ response = post("/api/submit", params)
49
+ Objects::Thing.new(self, response.body[:json][:data])
50
+ end
51
+
52
+ # Add a comment to the submission.
53
+ # @param text [String] The text to comment.
54
+ # @return [Objects::Comment] The reply.
55
+ def add_comment(text)
56
+ client.add_comment(self, text)
57
+ end
58
+
59
+ # @!endgroup
60
+
17
61
  # @!group Stylesheets
18
62
 
19
63
  # @return [String] The url for the subreddit's stylesheet.
@@ -172,6 +216,10 @@ module Redd
172
216
  client.search(query, self, **params)
173
217
  end
174
218
 
219
+ # @!endgroup
220
+
221
+ # @!group Moderation
222
+
175
223
  # @!method get_reports(**params)
176
224
  # @!method get_spam(**params)
177
225
  # @!method get_modqueue(**params)
@@ -201,10 +249,6 @@ module Redd
201
249
  end
202
250
  end
203
251
 
204
- # @!endgroup
205
-
206
- # @!group Moderator Settings
207
-
208
252
  # @return [Objects::Base] The current settings of a subreddit.
209
253
  def admin_about
210
254
  client.request_object(:get, "/r/#{display_name}/about/edit.json")
@@ -212,33 +256,62 @@ module Redd
212
256
 
213
257
  # Edit the subreddit's settings
214
258
  # @param [Hash] attributes The subreddit's new settings.
215
- # @author Takashi M (@beatak) and Avinash Dwarapu (@avidw)
216
259
  # @note This method may make additional requests if not all of the
217
- # required attributes are provided.
260
+ # required attributes are provided. Take a look at the source for the
261
+ # required attributes required to avoid making the additional request.
218
262
  # @see https://github.com/alaycock/MeetCal-bot/blob/master/serverInfo.conf
219
263
  def admin_edit(attributes)
220
- params = {sr: fullname}
221
- required_attributes = %i(
264
+ params = {
265
+ # Subreddit name
266
+ sr: fullname,
267
+ # Apparently useless options
268
+ show_cname_sidebar: true,
269
+ :"header-title" => title
270
+ }
271
+
272
+ required = %i(
222
273
  allow_top collapse_deleted_comments comment_score_hide_mins
223
- css_on_cname description exclude_banned_modqueue lang name over_18
224
- public_description public_traffic show_cname_sidebar show_media
225
- spam_comments spam_links spam_selfposts submit_link_label
226
- submit_text submit_text_label title type wiki_edit_age
227
- wiki_edit_karma wikimode header-title
274
+ css_on_cname description exclude_banned_modqueue lang link_type name
275
+ over_18 public_description public_traffic show_media spam_comments
276
+ spam_links spam_selfposts submit_link_label submit_text
277
+ submit_text_label title type wiki_edit_age wiki_edit_karma wikimode
228
278
  )
229
279
 
230
- if required_attributes.all? { |key| attributes.key?(key) }
280
+ if required.all? { |key| attributes.key?(key) }
231
281
  params.merge!(attributes)
232
282
  else
233
- current = admin_about
234
- current.delete(:kind)
235
- complete = current.merge(attributes)
236
- params.merge!(complete)
283
+ about = admin_about
284
+ final = about
285
+ .select { |k, _| required.include?(k) }
286
+ .merge(
287
+ name: display_name,
288
+ type: about[:subreddit_type],
289
+ lang: about[:language],
290
+ link_type: about[:content_options],
291
+ allow_top: true,
292
+ css_on_cname: true
293
+ )
294
+ .merge(attributes)
295
+ params.merge!(final)
237
296
  end
238
297
 
239
298
  post("/api/site_admin", params)
240
299
  end
241
300
 
301
+ # Add or replace the subreddit image or header logo.
302
+ # @param [String, IO] file The path/url to the file or the file itself.
303
+ # @param [String] name The name of the uploaded file.
304
+ # @return [String] The url of the image on reddit's CDN.
305
+ def upload_image(file, name = nil)
306
+ io = (file.is_a?(IO) ? file : File.open(file, "r"))
307
+ type = FastImage.type(io)
308
+ payload = Faraday::UploadIO.new(io, "image/#{type}")
309
+
310
+ params = {file: payload, header: (name ? 0 : 1), img_type: type}
311
+ params[:name] = name if name
312
+ post("/r/#{display_name}/api/upload_sr_img", params).body[:img_src]
313
+ end
314
+
242
315
  # @!endgroup
243
316
  end
244
317
  end
@@ -21,13 +21,6 @@ module Redd
21
21
  def fullname
22
22
  self[:name] || "#{kind}_#{id}"
23
23
  end
24
-
25
- private
26
-
27
- def response_merge!(meth, path, params = {})
28
- body = client.send(meth, path, params).body
29
- deep_merge!(body[:data])
30
- end
31
24
  end
32
25
  end
33
26
  end
@@ -1,4 +1,4 @@
1
1
  module Redd # rubocop:disable Style/Documentation
2
2
  # The semantic version number.
3
- VERSION = "0.7.3"
3
+ VERSION = "0.7.4"
4
4
  end
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency "hashie", "~> 3.4.0"
28
28
  spec.add_dependency "faraday", "~> 0.9.1"
29
29
  spec.add_dependency "multi_json", "~> 1.10.1"
30
+ spec.add_dependency "fastimage", "~> 1.6.6"
30
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-15 00:00:00.000000000 Z
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 1.10.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: fastimage
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.6.6
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.6.6
125
139
  description:
126
140
  email:
127
141
  - avinash@dwarapu.me
@@ -134,10 +148,10 @@ files:
134
148
  - ".rubocop.yml"
135
149
  - ".travis.yml"
136
150
  - Gemfile
137
- - LICENSE.txt
151
+ - LICENSE.md
138
152
  - README.md
139
153
  - Rakefile
140
- - RedditKit.LICENSE.txt
154
+ - RedditKit.LICENSE.md
141
155
  - lib/redd.rb
142
156
  - lib/redd/access.rb
143
157
  - lib/redd/clients/base.rb