redd 0.7.8 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +34 -34
  3. data/.rspec +3 -3
  4. data/.rubocop.yml +5 -5
  5. data/.travis.yml +9 -9
  6. data/LICENSE.md +22 -22
  7. data/README.md +143 -143
  8. data/Rakefile +5 -5
  9. data/RedditKit.LICENSE.md +8 -8
  10. data/lib/redd.rb +50 -50
  11. data/lib/redd/access.rb +76 -76
  12. data/lib/redd/clients/base.rb +181 -181
  13. data/lib/redd/clients/base/account.rb +20 -20
  14. data/lib/redd/clients/base/identity.rb +22 -22
  15. data/lib/redd/clients/base/none.rb +27 -27
  16. data/lib/redd/clients/base/privatemessages.rb +33 -33
  17. data/lib/redd/clients/base/read.rb +113 -113
  18. data/lib/redd/clients/base/stream.rb +81 -81
  19. data/lib/redd/clients/base/submit.rb +19 -19
  20. data/lib/redd/clients/base/utilities.rb +104 -104
  21. data/lib/redd/clients/base/wikiread.rb +33 -33
  22. data/lib/redd/clients/installed.rb +56 -56
  23. data/lib/redd/clients/script.rb +41 -41
  24. data/lib/redd/clients/userless.rb +32 -32
  25. data/lib/redd/clients/web.rb +58 -58
  26. data/lib/redd/error.rb +151 -151
  27. data/lib/redd/objects/base.rb +39 -39
  28. data/lib/redd/objects/comment.rb +22 -22
  29. data/lib/redd/objects/labeled_multi.rb +13 -13
  30. data/lib/redd/objects/listing.rb +29 -29
  31. data/lib/redd/objects/more_comments.rb +11 -10
  32. data/lib/redd/objects/private_message.rb +28 -28
  33. data/lib/redd/objects/submission.rb +139 -139
  34. data/lib/redd/objects/subreddit.rb +330 -319
  35. data/lib/redd/objects/thing.rb +26 -26
  36. data/lib/redd/objects/thing/editable.rb +22 -22
  37. data/lib/redd/objects/thing/hideable.rb +18 -18
  38. data/lib/redd/objects/thing/inboxable.rb +25 -25
  39. data/lib/redd/objects/thing/messageable.rb +34 -34
  40. data/lib/redd/objects/thing/moderatable.rb +43 -43
  41. data/lib/redd/objects/thing/refreshable.rb +14 -14
  42. data/lib/redd/objects/thing/saveable.rb +21 -21
  43. data/lib/redd/objects/thing/votable.rb +33 -33
  44. data/lib/redd/objects/user.rb +52 -52
  45. data/lib/redd/objects/wiki_page.rb +15 -15
  46. data/lib/redd/rate_limit.rb +88 -88
  47. data/lib/redd/response/parse_json.rb +18 -18
  48. data/lib/redd/response/raise_error.rb +16 -16
  49. data/lib/redd/version.rb +4 -4
  50. data/redd.gemspec +31 -31
  51. data/spec/redd/objects/base_spec.rb +1 -1
  52. data/spec/redd/response/raise_error_spec.rb +11 -11
  53. data/spec/redd_spec.rb +5 -5
  54. data/spec/spec_helper.rb +71 -71
  55. metadata +21 -21
@@ -1,39 +1,39 @@
1
- require "hashie"
2
- require "forwardable"
3
-
4
- module Redd
5
- # A bunch of objects that can hold properties.
6
- module Objects
7
- # A base for all objects to inherit from.
8
- class Base < Hashie::Hash
9
- include Hashie::Extensions::MergeInitializer
10
- include Hashie::Extensions::MethodReader
11
- include Hashie::Extensions::MethodQuery
12
- include Hashie::Extensions::DeepMerge
13
-
14
- # The `delete` method is called `delete_path` because it conflicts with
15
- # Hash#delete.
16
- extend Forwardable
17
- def_delegators :@client, :get, :post, :put, :delete_path
18
-
19
- # @!attribute [r] client
20
- # @return [Clients::Base] The client that used to make requests.
21
- attr_reader :client
22
-
23
- # @param [Clients::Base] client The client instance.
24
- # @param [Hash] attributes A hash of attributes.
25
- def initialize(client, attributes = {})
26
- @client = client
27
- super(attributes)
28
- end
29
-
30
- # Define an alias for a property.
31
- # @param [Symbol] new_name The alias.
32
- # @param [Symbol] old_name The existing property.
33
- def self.alias_property(new_name, old_name)
34
- define_method(new_name) { send(old_name) }
35
- define_method(:"#{new_name}?") { send(old_name) }
36
- end
37
- end
38
- end
39
- end
1
+ require "hashie"
2
+ require "forwardable"
3
+
4
+ module Redd
5
+ # A bunch of objects that can hold properties.
6
+ module Objects
7
+ # A base for all objects to inherit from.
8
+ class Base < Hashie::Hash
9
+ include Hashie::Extensions::MergeInitializer
10
+ include Hashie::Extensions::MethodReader
11
+ include Hashie::Extensions::MethodQuery
12
+ include Hashie::Extensions::DeepMerge
13
+
14
+ # The `delete` method is called `delete_path` because it conflicts with
15
+ # Hash#delete.
16
+ extend Forwardable
17
+ def_delegators :@client, :get, :post, :put, :delete_path
18
+
19
+ # @!attribute [r] client
20
+ # @return [Clients::Base] The client that used to make requests.
21
+ attr_reader :client
22
+
23
+ # @param [Clients::Base] client The client instance.
24
+ # @param [Hash] attributes A hash of attributes.
25
+ def initialize(client, attributes = {})
26
+ @client = client
27
+ super(attributes)
28
+ end
29
+
30
+ # Define an alias for a property.
31
+ # @param [Symbol] new_name The alias.
32
+ # @param [Symbol] old_name The existing property.
33
+ def self.alias_property(new_name, old_name)
34
+ define_method(new_name) { send(old_name) }
35
+ define_method(:"#{new_name}?") { send(old_name) }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,22 +1,22 @@
1
- require_relative "thing"
2
-
3
- module Redd
4
- module Objects
5
- # A comment that can be made on a link.
6
- class Comment < Thing
7
- include Thing::Editable
8
- include Thing::Inboxable
9
- include Thing::Moderatable
10
- include Thing::Refreshable
11
- include Thing::Saveable
12
- include Thing::Votable
13
-
14
- alias_property :reports_count, :num_reports
15
-
16
- # @return [Listing] The comment's replies.
17
- def replies
18
- @replies ||= (client.object_from_body(self[:replies]) || [])
19
- end
20
- end
21
- end
22
- end
1
+ require_relative "thing"
2
+
3
+ module Redd
4
+ module Objects
5
+ # A comment that can be made on a link.
6
+ class Comment < Thing
7
+ include Thing::Editable
8
+ include Thing::Inboxable
9
+ include Thing::Moderatable
10
+ include Thing::Refreshable
11
+ include Thing::Saveable
12
+ include Thing::Votable
13
+
14
+ alias_property :reports_count, :num_reports
15
+
16
+ # @return [Listing] The comment's replies.
17
+ def replies
18
+ @replies ||= (client.object_from_body(self[:replies]) || [])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,13 +1,13 @@
1
- module Redd
2
- module Objects
3
- # A comment that can be made on a link.
4
- class LabeledMulti < Base
5
- # @see Objects::Base
6
- def initialize(client, attributes = {})
7
- attr_dup = attributes.dup
8
- attr_dup[:subreddits].map! { |sub| sub[:name] }
9
- super(client, attr_dup)
10
- end
11
- end
12
- end
13
- end
1
+ module Redd
2
+ module Objects
3
+ # A comment that can be made on a link.
4
+ class LabeledMulti < Base
5
+ # @see Objects::Base
6
+ def initialize(client, attributes = {})
7
+ attr_dup = attributes.dup
8
+ attr_dup[:subreddits].map! { |sub| sub[:name] }
9
+ super(client, attr_dup)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,29 +1,29 @@
1
- module Redd
2
- module Objects
3
- # A collection of reddit things.
4
- # @see https://www.reddit.com/dev/api#listings
5
- class Listing < Array
6
- # @!attribute [r] before
7
- # @return [String] The id of the object before the listing.
8
- attr_reader :before
9
-
10
- # @!attribute [r] after
11
- # @return [String] The id of the object after the listing.
12
- attr_reader :after
13
-
14
- # @param [Clients::Base] client The client to expand the comments with.
15
- # @param [{:before => String, :after => String,
16
- # :children => Array<Hash>}] attributes The data to initialize the
17
- # class with.
18
- # @todo Only call Clients::Base#object_from_body when item is being
19
- # accessed.
20
- def initialize(client, attributes)
21
- @before = attributes[:before]
22
- @after = attributes[:after]
23
- attributes[:children].each do |child|
24
- self << (client.object_from_body(child) || child)
25
- end
26
- end
27
- end
28
- end
29
- end
1
+ module Redd
2
+ module Objects
3
+ # A collection of reddit things.
4
+ # @see https://www.reddit.com/dev/api#listings
5
+ class Listing < Array
6
+ # @!attribute [r] before
7
+ # @return [String] The id of the object before the listing.
8
+ attr_reader :before
9
+
10
+ # @!attribute [r] after
11
+ # @return [String] The id of the object after the listing.
12
+ attr_reader :after
13
+
14
+ # @param [Clients::Base] client The client to expand the comments with.
15
+ # @param [{:before => String, :after => String,
16
+ # :children => Array<Hash>}] attributes The data to initialize the
17
+ # class with.
18
+ # @todo Only call Clients::Base#object_from_body when item is being
19
+ # accessed.
20
+ def initialize(client, attributes)
21
+ @before = attributes[:before]
22
+ @after = attributes[:after]
23
+ attributes[:children].each do |child|
24
+ self << (client.object_from_body(child) || child)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,10 +1,11 @@
1
- module Redd
2
- module Objects
3
- # The model for a morecomments object
4
- class MoreComments < Array
5
- def initialize(_, attributes)
6
- super(attributes[:children])
7
- end
8
- end
9
- end
10
- end
1
+ module Redd
2
+ module Objects
3
+ # The model for a morecomments object
4
+ class MoreComments < Array
5
+ def initialize(_, attributes)
6
+ #Return an empty arrar if there are no children
7
+ super(attributes[:children]) if attributes[:children]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,28 +1,28 @@
1
- require_relative "thing"
2
-
3
- module Redd
4
- module Objects
5
- # The model for private messages
6
- class PrivateMessage < Thing
7
- include Thing::Inboxable
8
-
9
- alias_property :from, :author
10
- alias_property :to, :dest
11
-
12
- # Block the sender of the message from sending any more.
13
- def block_sender!
14
- post("/api/block", id: fullname)
15
- end
16
-
17
- # Mark the message as read.
18
- def mark_as_read
19
- post("/api/read_message", id: fullname)
20
- end
21
-
22
- # Mark the message as unread and add orangered to account.
23
- def mark_as_unread
24
- post("/api/unread_message", id: fullname)
25
- end
26
- end
27
- end
28
- end
1
+ require_relative "thing"
2
+
3
+ module Redd
4
+ module Objects
5
+ # The model for private messages
6
+ class PrivateMessage < Thing
7
+ include Thing::Inboxable
8
+
9
+ alias_property :from, :author
10
+ alias_property :to, :dest
11
+
12
+ # Block the sender of the message from sending any more.
13
+ def block_sender!
14
+ post("/api/block", id: fullname)
15
+ end
16
+
17
+ # Mark the message as read.
18
+ def mark_as_read
19
+ post("/api/read_message", id: fullname)
20
+ end
21
+
22
+ # Mark the message as unread and add orangered to account.
23
+ def mark_as_unread
24
+ post("/api/unread_message", id: fullname)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,139 +1,139 @@
1
- require_relative "thing"
2
-
3
- module Redd
4
- module Objects
5
- # A submission made in a subreddit.
6
- class Submission < Thing
7
- include Thing::Editable
8
- include Thing::Hideable
9
- include Thing::Moderatable
10
- include Thing::Refreshable
11
- include Thing::Saveable
12
- include Thing::Votable
13
-
14
- alias_property :nsfw?, :over_18
15
- alias_property :self?, :is_self
16
- alias_property :comments_count, :num_comments
17
-
18
- # The shorter url for sharing.
19
- def short_url
20
- "http://redd.it/#{self[:id]}"
21
- end
22
-
23
- # Whether the comment was gilded.
24
- def gilded?
25
- self[:gilded] > 0
26
- end
27
-
28
- # Mark the thing as Not Suitable For Work.
29
- def mark_as_nsfw
30
- get("/api/marknsfw", id: fullname)
31
- self[:over_18] = true
32
- end
33
-
34
- # No longer mark the thing as Not Suitable For Work.
35
- def unmark_as_nsfw
36
- get("/api/unmarknsfw", id: fullname)
37
- self[:over_18] = false
38
- end
39
- alias_method :mark_as_safe, :unmark_as_nsfw
40
-
41
- # Reply to the thing.
42
- # @param text [String] The text to comment.
43
- # @return [Objects::Comment] The reply.
44
- def add_comment(text)
45
- client.add_comment(self, text)
46
- end
47
-
48
- # Set the submission to "contest mode" (comments are randomly sorted)
49
- def set_contest_mode
50
- post("/api/set_contest_mode", id: fullname, state: true)
51
- end
52
-
53
- # Unset the "contest mode".
54
- def unset_contest_mode
55
- post("/api/set_contest_mode", id: fullname, state: false)
56
- end
57
-
58
- # Set the submission as the sticky post of the subreddit
59
- def set_sticky
60
- post("/api/set_subreddit_sticky", id: fullname, state: true)
61
- self[:stickied] = true
62
- end
63
-
64
- # Unsticky the post from the subreddit
65
- def unset_sticky
66
- post("/api/set_subreddit_sticky", id: fullname, state: false)
67
- self[:stickied] = false
68
- end
69
-
70
- # @return [Listing] The submission's comments.
71
- # @todo Allow for various depths and contexts and what not. Maybe a
72
- # get_comment method?
73
- def comments
74
- refresh! unless @comments
75
- @comments
76
- end
77
- alias_method :replies, :comments
78
-
79
- # Refresh the submission AND its comments.
80
- # @return [Submission] The updated submission.
81
- def refresh!
82
- body = get("/comments/#{id}.json").body
83
- @comments = client.object_from_body(body[1])
84
- deep_merge!(body[0])
85
- end
86
-
87
- # Take a MoreComments and return a listing of comments.
88
- # @param [MoreComments] more The object to expand.
89
- # @return [Listing] A listing of the expanded comments.
90
- def expand_more(more)
91
- response = client.get(
92
- "/api/morechildren",
93
- children: more.join(","),
94
- link_id: fullname
95
- )
96
-
97
- client.object_from_body(
98
- kind: "Listing",
99
- data: {
100
- before: "", after: "",
101
- children: response.body[:json][:data][:things]
102
- }
103
- )
104
- end
105
-
106
- # Get the related articles.
107
- # @param [Hash] params A list of params to send with the request.
108
- # @option params [String] :after Return results after the given
109
- # fullname.
110
- # @option params [String] :before Return results before the given
111
- # fullname.
112
- # @option params [Integer] :count The number of items already seen
113
- # in the listing.
114
- # @option params [1..100] :limit The maximum number of things to
115
- # return.
116
- # @return [Objects::Listing<Objects::Thing>]
117
- def get_related(**params)
118
- related = get("/related/#{id}.json", params).body[1]
119
- client.object_from_body(related)
120
- end
121
-
122
- # Get other articles with the same URL.
123
- # @param [Hash] params A list of params to send with the request.
124
- # @option params [String] :after Return results after the given
125
- # fullname.
126
- # @option params [String] :before Return results before the given
127
- # fullname.
128
- # @option params [Integer] :count The number of items already seen
129
- # in the listing.
130
- # @option params [1..100] :limit The maximum number of things to
131
- # return.
132
- # @return [Objects::Listing<Objects::Submission>]
133
- def get_duplicates(**params)
134
- duplicates = get("/duplicates/#{id}.json", params).body[1]
135
- client.object_from_body(duplicates)
136
- end
137
- end
138
- end
139
- end
1
+ require_relative "thing"
2
+
3
+ module Redd
4
+ module Objects
5
+ # A submission made in a subreddit.
6
+ class Submission < Thing
7
+ include Thing::Editable
8
+ include Thing::Hideable
9
+ include Thing::Moderatable
10
+ include Thing::Refreshable
11
+ include Thing::Saveable
12
+ include Thing::Votable
13
+
14
+ alias_property :nsfw?, :over_18
15
+ alias_property :self?, :is_self
16
+ alias_property :comments_count, :num_comments
17
+
18
+ # The shorter url for sharing.
19
+ def short_url
20
+ "http://redd.it/#{self[:id]}"
21
+ end
22
+
23
+ # Whether the comment was gilded.
24
+ def gilded?
25
+ self[:gilded] > 0
26
+ end
27
+
28
+ # Mark the thing as Not Suitable For Work.
29
+ def mark_as_nsfw
30
+ get("/api/marknsfw", id: fullname)
31
+ self[:over_18] = true
32
+ end
33
+
34
+ # No longer mark the thing as Not Suitable For Work.
35
+ def unmark_as_nsfw
36
+ get("/api/unmarknsfw", id: fullname)
37
+ self[:over_18] = false
38
+ end
39
+ alias_method :mark_as_safe, :unmark_as_nsfw
40
+
41
+ # Reply to the thing.
42
+ # @param text [String] The text to comment.
43
+ # @return [Objects::Comment] The reply.
44
+ def add_comment(text)
45
+ client.add_comment(self, text)
46
+ end
47
+
48
+ # Set the submission to "contest mode" (comments are randomly sorted)
49
+ def set_contest_mode
50
+ post("/api/set_contest_mode", id: fullname, state: true)
51
+ end
52
+
53
+ # Unset the "contest mode".
54
+ def unset_contest_mode
55
+ post("/api/set_contest_mode", id: fullname, state: false)
56
+ end
57
+
58
+ # Set the submission as the sticky post of the subreddit
59
+ def set_sticky
60
+ post("/api/set_subreddit_sticky", id: fullname, state: true)
61
+ self[:stickied] = true
62
+ end
63
+
64
+ # Unsticky the post from the subreddit
65
+ def unset_sticky
66
+ post("/api/set_subreddit_sticky", id: fullname, state: false)
67
+ self[:stickied] = false
68
+ end
69
+
70
+ # @return [Listing] The submission's comments.
71
+ # @todo Allow for various depths and contexts and what not. Maybe a
72
+ # get_comment method?
73
+ def comments
74
+ refresh! unless @comments
75
+ @comments
76
+ end
77
+ alias_method :replies, :comments
78
+
79
+ # Refresh the submission AND its comments.
80
+ # @return [Submission] The updated submission.
81
+ def refresh!
82
+ body = get("/comments/#{id}.json").body
83
+ @comments = client.object_from_body(body[1])
84
+ deep_merge!(body[0])
85
+ end
86
+
87
+ # Take a MoreComments and return a listing of comments.
88
+ # @param [MoreComments] more The object to expand.
89
+ # @return [Listing] A listing of the expanded comments.
90
+ def expand_more(more)
91
+ response = client.get(
92
+ "/api/morechildren",
93
+ children: more.join(","),
94
+ link_id: fullname
95
+ )
96
+
97
+ client.object_from_body(
98
+ kind: "Listing",
99
+ data: {
100
+ before: "", after: "",
101
+ children: response.body[:json][:data][:things]
102
+ }
103
+ )
104
+ end
105
+
106
+ # Get the related articles.
107
+ # @param [Hash] params A list of params to send with the request.
108
+ # @option params [String] :after Return results after the given
109
+ # fullname.
110
+ # @option params [String] :before Return results before the given
111
+ # fullname.
112
+ # @option params [Integer] :count The number of items already seen
113
+ # in the listing.
114
+ # @option params [1..100] :limit The maximum number of things to
115
+ # return.
116
+ # @return [Objects::Listing<Objects::Thing>]
117
+ def get_related(**params)
118
+ related = get("/related/#{id}.json", params).body[1]
119
+ client.object_from_body(related)
120
+ end
121
+
122
+ # Get other articles with the same URL.
123
+ # @param [Hash] params A list of params to send with the request.
124
+ # @option params [String] :after Return results after the given
125
+ # fullname.
126
+ # @option params [String] :before Return results before the given
127
+ # fullname.
128
+ # @option params [Integer] :count The number of items already seen
129
+ # in the listing.
130
+ # @option params [1..100] :limit The maximum number of things to
131
+ # return.
132
+ # @return [Objects::Listing<Objects::Submission>]
133
+ def get_duplicates(**params)
134
+ duplicates = get("/duplicates/#{id}.json", params).body[1]
135
+ client.object_from_body(duplicates)
136
+ end
137
+ end
138
+ end
139
+ end