redd 0.7.8 → 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +34 -34
- data/.rspec +3 -3
- data/.rubocop.yml +5 -5
- data/.travis.yml +9 -9
- data/LICENSE.md +22 -22
- data/README.md +143 -143
- data/Rakefile +5 -5
- data/RedditKit.LICENSE.md +8 -8
- data/lib/redd.rb +50 -50
- data/lib/redd/access.rb +76 -76
- data/lib/redd/clients/base.rb +181 -181
- data/lib/redd/clients/base/account.rb +20 -20
- data/lib/redd/clients/base/identity.rb +22 -22
- data/lib/redd/clients/base/none.rb +27 -27
- data/lib/redd/clients/base/privatemessages.rb +33 -33
- data/lib/redd/clients/base/read.rb +113 -113
- data/lib/redd/clients/base/stream.rb +81 -81
- data/lib/redd/clients/base/submit.rb +19 -19
- data/lib/redd/clients/base/utilities.rb +104 -104
- data/lib/redd/clients/base/wikiread.rb +33 -33
- data/lib/redd/clients/installed.rb +56 -56
- data/lib/redd/clients/script.rb +41 -41
- data/lib/redd/clients/userless.rb +32 -32
- data/lib/redd/clients/web.rb +58 -58
- data/lib/redd/error.rb +151 -151
- data/lib/redd/objects/base.rb +39 -39
- data/lib/redd/objects/comment.rb +22 -22
- data/lib/redd/objects/labeled_multi.rb +13 -13
- data/lib/redd/objects/listing.rb +29 -29
- data/lib/redd/objects/more_comments.rb +11 -10
- data/lib/redd/objects/private_message.rb +28 -28
- data/lib/redd/objects/submission.rb +139 -139
- data/lib/redd/objects/subreddit.rb +330 -319
- data/lib/redd/objects/thing.rb +26 -26
- data/lib/redd/objects/thing/editable.rb +22 -22
- data/lib/redd/objects/thing/hideable.rb +18 -18
- data/lib/redd/objects/thing/inboxable.rb +25 -25
- data/lib/redd/objects/thing/messageable.rb +34 -34
- data/lib/redd/objects/thing/moderatable.rb +43 -43
- data/lib/redd/objects/thing/refreshable.rb +14 -14
- data/lib/redd/objects/thing/saveable.rb +21 -21
- data/lib/redd/objects/thing/votable.rb +33 -33
- data/lib/redd/objects/user.rb +52 -52
- data/lib/redd/objects/wiki_page.rb +15 -15
- data/lib/redd/rate_limit.rb +88 -88
- data/lib/redd/response/parse_json.rb +18 -18
- data/lib/redd/response/raise_error.rb +16 -16
- data/lib/redd/version.rb +4 -4
- data/redd.gemspec +31 -31
- data/spec/redd/objects/base_spec.rb +1 -1
- data/spec/redd/response/raise_error_spec.rb +11 -11
- data/spec/redd_spec.rb +5 -5
- data/spec/spec_helper.rb +71 -71
- metadata +21 -21
data/lib/redd/objects/user.rb
CHANGED
@@ -1,52 +1,52 @@
|
|
1
|
-
require_relative "thing"
|
2
|
-
|
3
|
-
module Redd
|
4
|
-
module Objects
|
5
|
-
# The model for a reddit user
|
6
|
-
class User < Thing
|
7
|
-
include Thing::Messageable
|
8
|
-
|
9
|
-
# @!method get_overview(**params)
|
10
|
-
# @!method get_submitted(**params)
|
11
|
-
# @!method get_comments(**params)
|
12
|
-
# @!method get_liked(**params)
|
13
|
-
# @!method get_disliked(**params)
|
14
|
-
# @!method get_hidden(**params)
|
15
|
-
# @!method get_saved(**params)
|
16
|
-
# @!method get_gilded(**params)
|
17
|
-
#
|
18
|
-
# Get the appropriate listing.
|
19
|
-
# @option params [String] :after Return results after the given
|
20
|
-
# fullname.
|
21
|
-
# @option params [String] :before Return results before the given
|
22
|
-
# fullname.
|
23
|
-
# @option params [Integer] :count The number of items already seen
|
24
|
-
# in the listing.
|
25
|
-
# @option params [1..100] :limit The maximum number of things to
|
26
|
-
# return.
|
27
|
-
# @option params [:hot, :new, :top, :controversial] :sort The type of
|
28
|
-
# sort to use.
|
29
|
-
# @option params [:hour, :day, :week, :month, :year, :all] :t The
|
30
|
-
# time period to consider when sorting.
|
31
|
-
# @option params [:given] :show For {#get_gilded}, whether to show the
|
32
|
-
# gildings given.
|
33
|
-
# @note The option :t only applies to the top and controversial sorts.
|
34
|
-
# @return [Listing<Submission>]
|
35
|
-
%w(
|
36
|
-
overview submitted comments liked disliked hidden saved gilded
|
37
|
-
).each do |type|
|
38
|
-
define_method :"get_#{type}" do |**params|
|
39
|
-
client.request_object(
|
40
|
-
:get, "/user/#{name}/#{type}.json", **params
|
41
|
-
)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# Get posts that the user has gilded.
|
46
|
-
# @see #get_gilded
|
47
|
-
def get_gildings_given(**params)
|
48
|
-
get_gilded(**params.merge(show: "given"))
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
1
|
+
require_relative "thing"
|
2
|
+
|
3
|
+
module Redd
|
4
|
+
module Objects
|
5
|
+
# The model for a reddit user
|
6
|
+
class User < Thing
|
7
|
+
include Thing::Messageable
|
8
|
+
|
9
|
+
# @!method get_overview(**params)
|
10
|
+
# @!method get_submitted(**params)
|
11
|
+
# @!method get_comments(**params)
|
12
|
+
# @!method get_liked(**params)
|
13
|
+
# @!method get_disliked(**params)
|
14
|
+
# @!method get_hidden(**params)
|
15
|
+
# @!method get_saved(**params)
|
16
|
+
# @!method get_gilded(**params)
|
17
|
+
#
|
18
|
+
# Get the appropriate listing.
|
19
|
+
# @option params [String] :after Return results after the given
|
20
|
+
# fullname.
|
21
|
+
# @option params [String] :before Return results before the given
|
22
|
+
# fullname.
|
23
|
+
# @option params [Integer] :count The number of items already seen
|
24
|
+
# in the listing.
|
25
|
+
# @option params [1..100] :limit The maximum number of things to
|
26
|
+
# return.
|
27
|
+
# @option params [:hot, :new, :top, :controversial] :sort The type of
|
28
|
+
# sort to use.
|
29
|
+
# @option params [:hour, :day, :week, :month, :year, :all] :t The
|
30
|
+
# time period to consider when sorting.
|
31
|
+
# @option params [:given] :show For {#get_gilded}, whether to show the
|
32
|
+
# gildings given.
|
33
|
+
# @note The option :t only applies to the top and controversial sorts.
|
34
|
+
# @return [Listing<Submission>]
|
35
|
+
%w(
|
36
|
+
overview submitted comments liked disliked hidden saved gilded
|
37
|
+
).each do |type|
|
38
|
+
define_method :"get_#{type}" do |**params|
|
39
|
+
client.request_object(
|
40
|
+
:get, "/user/#{name}/#{type}.json", **params
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Get posts that the user has gilded.
|
46
|
+
# @see #get_gilded
|
47
|
+
def get_gildings_given(**params)
|
48
|
+
get_gilded(**params.merge(show: "given"))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
require_relative "thing"
|
2
|
-
|
3
|
-
module Redd
|
4
|
-
module Objects
|
5
|
-
# A submission made in a subreddit.
|
6
|
-
class WikiPage < Thing
|
7
|
-
alias_property :content, :content_md
|
8
|
-
alias_property :body, :content_md
|
9
|
-
|
10
|
-
def revision_user
|
11
|
-
@revision_by ||= client.object_from_body(revision_by)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
1
|
+
require_relative "thing"
|
2
|
+
|
3
|
+
module Redd
|
4
|
+
module Objects
|
5
|
+
# A submission made in a subreddit.
|
6
|
+
class WikiPage < Thing
|
7
|
+
alias_property :content, :content_md
|
8
|
+
alias_property :body, :content_md
|
9
|
+
|
10
|
+
def revision_user
|
11
|
+
@revision_by ||= client.object_from_body(revision_by)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/redd/rate_limit.rb
CHANGED
@@ -1,88 +1,88 @@
|
|
1
|
-
# The main Redd module.
|
2
|
-
module Redd
|
3
|
-
# The class that handles rate limiting for reddit.
|
4
|
-
#
|
5
|
-
# If you'd rather have an asynchronous or queue-based limiting, it's easy to
|
6
|
-
# write one yourself. A rate limiting class is any class that has an
|
7
|
-
# {#after_limit} method. The block returns a Faraday::Response object, so you
|
8
|
-
# can also extract the headers from the response and use those instead. To
|
9
|
-
# remove rate limiting entirely, follow the example below.
|
10
|
-
#
|
11
|
-
# @example To remove rate limiting entirely, just burst forever.
|
12
|
-
# rt = Redd::RateLimit.new
|
13
|
-
# rt.burst!(Float::INFINITY)
|
14
|
-
class RateLimit
|
15
|
-
# @!attribute [rw] gap
|
16
|
-
# @return [Integer, Float] The minimum time between requests.
|
17
|
-
attr_accessor :gap
|
18
|
-
|
19
|
-
# @!attribute [rw] burst_length
|
20
|
-
# @return [Integer] The number of requests left to burst.
|
21
|
-
attr_accessor :burst_length
|
22
|
-
|
23
|
-
# @!attribute [r] last_request
|
24
|
-
# @return [Time] The time the last request was made.
|
25
|
-
attr_reader :last_request
|
26
|
-
|
27
|
-
# @!attribute [r] used
|
28
|
-
# @!attribute [r] remaining
|
29
|
-
# @!attribute [r] reset
|
30
|
-
# @return [Integer] The data from reddit's response headers.
|
31
|
-
attr_reader :used, :remaining, :reset
|
32
|
-
|
33
|
-
# @param [Float, Integer] gap The minimum time between each request.
|
34
|
-
def initialize(gap)
|
35
|
-
# Some time ages ago, because we never made a request.
|
36
|
-
@last_request = Time.at(0)
|
37
|
-
@gap = gap
|
38
|
-
@burst_length = 0
|
39
|
-
end
|
40
|
-
|
41
|
-
# Don't sleep for the next few requests.
|
42
|
-
# @param [Integer] times The number of times to ignore limiting.
|
43
|
-
# @return [Integer] The total times rate limiting will be ignored.
|
44
|
-
def burst!(times)
|
45
|
-
@burst_length += times
|
46
|
-
end
|
47
|
-
|
48
|
-
# Sleep until 1 second has passed since the last request and perform the
|
49
|
-
# given request unless bursting.
|
50
|
-
#
|
51
|
-
# @yield [Faraday::Response] A response.
|
52
|
-
# @return [Faraday::Response] The response.
|
53
|
-
def after_limit
|
54
|
-
response = yield
|
55
|
-
update!(response)
|
56
|
-
sleep(wait_time)
|
57
|
-
response
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
# Update necessary info with each request.
|
63
|
-
# @param [Faraday::Response] response The response to the request made.
|
64
|
-
def update!(response)
|
65
|
-
@last_request_time = Time.now
|
66
|
-
%w(used remaining reset).each do |type|
|
67
|
-
value = response.headers["x-ratelimit-#{type}"]
|
68
|
-
instance_variable_set("@#{type}", value.to_i) unless value.nil?
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# @return [Float, Integer] The number of seconds to sleep.
|
73
|
-
def wait_time
|
74
|
-
if @burst_length > 0 && @remaining > 0
|
75
|
-
# Don't sleep if we are in burst mode.
|
76
|
-
@burst_length -= 1
|
77
|
-
0
|
78
|
-
elsif @reset.nil? || @remaining.nil?
|
79
|
-
# Just guess if no headers were given (max 1 sec after last request).
|
80
|
-
time = @last_request_time - Time.now + @gap
|
81
|
-
time > 0 ? time : 0
|
82
|
-
else
|
83
|
-
# Most cases, spread out requests over available time.
|
84
|
-
@reset.to_f / (@gap * @remaining + 1)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
1
|
+
# The main Redd module.
|
2
|
+
module Redd
|
3
|
+
# The class that handles rate limiting for reddit.
|
4
|
+
#
|
5
|
+
# If you'd rather have an asynchronous or queue-based limiting, it's easy to
|
6
|
+
# write one yourself. A rate limiting class is any class that has an
|
7
|
+
# {#after_limit} method. The block returns a Faraday::Response object, so you
|
8
|
+
# can also extract the headers from the response and use those instead. To
|
9
|
+
# remove rate limiting entirely, follow the example below.
|
10
|
+
#
|
11
|
+
# @example To remove rate limiting entirely, just burst forever.
|
12
|
+
# rt = Redd::RateLimit.new
|
13
|
+
# rt.burst!(Float::INFINITY)
|
14
|
+
class RateLimit
|
15
|
+
# @!attribute [rw] gap
|
16
|
+
# @return [Integer, Float] The minimum time between requests.
|
17
|
+
attr_accessor :gap
|
18
|
+
|
19
|
+
# @!attribute [rw] burst_length
|
20
|
+
# @return [Integer] The number of requests left to burst.
|
21
|
+
attr_accessor :burst_length
|
22
|
+
|
23
|
+
# @!attribute [r] last_request
|
24
|
+
# @return [Time] The time the last request was made.
|
25
|
+
attr_reader :last_request
|
26
|
+
|
27
|
+
# @!attribute [r] used
|
28
|
+
# @!attribute [r] remaining
|
29
|
+
# @!attribute [r] reset
|
30
|
+
# @return [Integer] The data from reddit's response headers.
|
31
|
+
attr_reader :used, :remaining, :reset
|
32
|
+
|
33
|
+
# @param [Float, Integer] gap The minimum time between each request.
|
34
|
+
def initialize(gap)
|
35
|
+
# Some time ages ago, because we never made a request.
|
36
|
+
@last_request = Time.at(0)
|
37
|
+
@gap = gap
|
38
|
+
@burst_length = 0
|
39
|
+
end
|
40
|
+
|
41
|
+
# Don't sleep for the next few requests.
|
42
|
+
# @param [Integer] times The number of times to ignore limiting.
|
43
|
+
# @return [Integer] The total times rate limiting will be ignored.
|
44
|
+
def burst!(times)
|
45
|
+
@burst_length += times
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sleep until 1 second has passed since the last request and perform the
|
49
|
+
# given request unless bursting.
|
50
|
+
#
|
51
|
+
# @yield [Faraday::Response] A response.
|
52
|
+
# @return [Faraday::Response] The response.
|
53
|
+
def after_limit
|
54
|
+
response = yield
|
55
|
+
update!(response)
|
56
|
+
sleep(wait_time)
|
57
|
+
response
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Update necessary info with each request.
|
63
|
+
# @param [Faraday::Response] response The response to the request made.
|
64
|
+
def update!(response)
|
65
|
+
@last_request_time = Time.now
|
66
|
+
%w(used remaining reset).each do |type|
|
67
|
+
value = response.headers["x-ratelimit-#{type}"]
|
68
|
+
instance_variable_set("@#{type}", value.to_i) unless value.nil?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [Float, Integer] The number of seconds to sleep.
|
73
|
+
def wait_time
|
74
|
+
if @burst_length > 0 && @remaining > 0
|
75
|
+
# Don't sleep if we are in burst mode.
|
76
|
+
@burst_length -= 1
|
77
|
+
0
|
78
|
+
elsif @reset.nil? || @remaining.nil?
|
79
|
+
# Just guess if no headers were given (max 1 sec after last request).
|
80
|
+
time = @last_request_time - Time.now + @gap
|
81
|
+
time > 0 ? time : 0
|
82
|
+
else
|
83
|
+
# Most cases, spread out requests over available time.
|
84
|
+
@reset.to_f / (@gap * @remaining + 1)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
|
-
require "faraday/response"
|
2
|
-
require_relative "../error"
|
3
|
-
|
4
|
-
module Redd
|
5
|
-
# The module that contains middleware that alters the Faraday response.
|
6
|
-
module Response
|
7
|
-
# Faraday Middleware that parses JSON using OJ, via MultiJson.
|
8
|
-
class ParseJson < Faraday::Response::Middleware
|
9
|
-
dependency "multi_json"
|
10
|
-
|
11
|
-
def on_complete(env)
|
12
|
-
env[:body] = MultiJson.load(env[:body], symbolize_keys: true)
|
13
|
-
rescue MultiJson::ParseError
|
14
|
-
raise JSONError.new(env), env[:body]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
1
|
+
require "faraday/response"
|
2
|
+
require_relative "../error"
|
3
|
+
|
4
|
+
module Redd
|
5
|
+
# The module that contains middleware that alters the Faraday response.
|
6
|
+
module Response
|
7
|
+
# Faraday Middleware that parses JSON using OJ, via MultiJson.
|
8
|
+
class ParseJson < Faraday::Response::Middleware
|
9
|
+
dependency "multi_json"
|
10
|
+
|
11
|
+
def on_complete(env)
|
12
|
+
env[:body] = MultiJson.load(env[:body], symbolize_keys: true)
|
13
|
+
rescue MultiJson::ParseError
|
14
|
+
raise ::Redd::Error::JSONError.new(env), env[:body]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require "faraday/response"
|
2
|
-
require_relative "../error"
|
3
|
-
|
4
|
-
module Redd
|
5
|
-
# The module that contains middleware that alters the Faraday response.
|
6
|
-
module Response
|
7
|
-
# Faraday Middleware that raises an error if there's one.
|
8
|
-
# @see Error
|
9
|
-
class RaiseError < Faraday::Response::Middleware
|
10
|
-
def on_complete(env)
|
11
|
-
error = Redd::Error.from_response(env)
|
12
|
-
fail error.new(env), env[:body] if error
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
1
|
+
require "faraday/response"
|
2
|
+
require_relative "../error"
|
3
|
+
|
4
|
+
module Redd
|
5
|
+
# The module that contains middleware that alters the Faraday response.
|
6
|
+
module Response
|
7
|
+
# Faraday Middleware that raises an error if there's one.
|
8
|
+
# @see Error
|
9
|
+
class RaiseError < Faraday::Response::Middleware
|
10
|
+
def on_complete(env)
|
11
|
+
error = Redd::Error.from_response(env)
|
12
|
+
fail error.new(env), env[:body] if error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/redd/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module Redd # rubocop:disable Style/Documentation
|
2
|
-
# The semantic version number.
|
3
|
-
VERSION = "0.7.
|
4
|
-
end
|
1
|
+
module Redd # rubocop:disable Style/Documentation
|
2
|
+
# The semantic version number.
|
3
|
+
VERSION = "0.7.9"
|
4
|
+
end
|
data/redd.gemspec
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "redd/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "redd"
|
8
|
-
spec.version = Redd::VERSION
|
9
|
-
spec.authors = ["Avinash Dwarapu"]
|
10
|
-
spec.email = ["avinash@dwarapu.me"]
|
11
|
-
spec.summary = "A Reddit API Wrapper for Ruby."
|
12
|
-
spec.homepage = "https://github.com/avidw/redd"
|
13
|
-
spec.license = "MIT"
|
14
|
-
spec.required_ruby_version = ">= 2.1.0"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
|
-
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.
|
22
|
-
spec.add_development_dependency "rake", "~>
|
23
|
-
spec.add_development_dependency "rspec", "~> 3.
|
24
|
-
spec.add_development_dependency "vcr", "~>
|
25
|
-
spec.add_development_dependency "webmock", "~> 1
|
26
|
-
|
27
|
-
spec.add_dependency "hashie", "~> 3.4
|
28
|
-
spec.add_dependency "faraday", "~> 0.9
|
29
|
-
spec.add_dependency "multi_json", "~> 1.
|
30
|
-
spec.add_dependency "fastimage", "~>
|
31
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "redd/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "redd"
|
8
|
+
spec.version = Redd::VERSION
|
9
|
+
spec.authors = ["Avinash Dwarapu"]
|
10
|
+
spec.email = ["avinash@dwarapu.me"]
|
11
|
+
spec.summary = "A Reddit API Wrapper for Ruby."
|
12
|
+
spec.homepage = "https://github.com/avidw/redd"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.1.0"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 11.2"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
24
|
+
spec.add_development_dependency "vcr", "~> 3.0"
|
25
|
+
spec.add_development_dependency "webmock", "~> 2.1"
|
26
|
+
|
27
|
+
spec.add_dependency "hashie", "~> 3.4"
|
28
|
+
spec.add_dependency "faraday", "~> 0.9"
|
29
|
+
spec.add_dependency "multi_json", "~> 1.12"
|
30
|
+
spec.add_dependency "fastimage", "~> 2.0"
|
31
|
+
end
|