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,19 +1,19 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "submit" scope
5
- module Submit
6
- # Add a comment to a link, reply to a comment or reply to a message.
7
- # Bit of an all-purpose method, this one.
8
- # @param thing [Objects::Submission, Objects::Comment,
9
- # Objects::PrivateMessage] A thing to add a comment to.
10
- # @param text [String] The text to comment.
11
- # @return [Objects::Comment, Objects::PrivateMessage] The reply.
12
- def add_comment(thing, text)
13
- response = post("/api/comment", text: text, thing_id: thing.fullname)
14
- object_from_body(response.body[:json][:data][:things][0])
15
- end
16
- end
17
- end
18
- end
19
- end
1
+ module Redd
2
+ module Clients
3
+ class Base
4
+ # Methods that require the "submit" scope
5
+ module Submit
6
+ # Add a comment to a link, reply to a comment or reply to a message.
7
+ # Bit of an all-purpose method, this one.
8
+ # @param thing [Objects::Submission, Objects::Comment,
9
+ # Objects::PrivateMessage] A thing to add a comment to.
10
+ # @param text [String] The text to comment.
11
+ # @return [Objects::Comment, Objects::PrivateMessage] The reply.
12
+ def add_comment(thing, text)
13
+ response = post("/api/comment", text: text, thing_id: thing.fullname)
14
+ object_from_body(response.body[:json][:data][:things][0])
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,104 +1,104 @@
1
- require_relative "../../objects/base"
2
- require_relative "../../objects/thing"
3
- require_relative "../../objects/listing"
4
- require_relative "../../objects/wiki_page"
5
- require_relative "../../objects/labeled_multi"
6
- require_relative "../../objects/more_comments"
7
- require_relative "../../objects/comment"
8
- require_relative "../../objects/user"
9
- require_relative "../../objects/submission"
10
- require_relative "../../objects/private_message"
11
- require_relative "../../objects/subreddit"
12
-
13
- module Redd
14
- module Clients
15
- class Base
16
- # Internal methods that make life easier.
17
- # @todo Move this out to Redd::Utils?
18
- module Utilities
19
- # The kind strings and the objects that should be used for them.
20
- OBJECT_KINDS = {
21
- "Listing" => Objects::Listing,
22
- "wikipage" => Objects::WikiPage,
23
- "LabeledMulti" => Objects::LabeledMulti,
24
- "more" => Objects::MoreComments,
25
- "t1" => Objects::Comment,
26
- "t2" => Objects::User,
27
- "t3" => Objects::Submission,
28
- "t4" => Objects::PrivateMessage,
29
- "t5" => Objects::Subreddit
30
- }
31
-
32
- # Request and create an object from the response.
33
- # @param [Symbol] meth The method to use.
34
- # @param [String] path The path to visit.
35
- # @param [Hash] params The data to send with the request.
36
- # @return [Objects::Base] The object returned from the request.
37
- def request_object(meth, path, params = {})
38
- body = send(meth, path, params).body
39
- object_from_body(body)
40
- end
41
-
42
- # Create an object instance with the correct attributes when given a
43
- # body.
44
- #
45
- # @param [Hash] body A JSON hash.
46
- # @return [Objects::Thing, Objects::Listing]
47
- def object_from_body(body)
48
- return nil unless body.is_a?(Hash)
49
- object = object_from_kind(body[:kind])
50
- flat = flatten_body(body)
51
- object.new(self, flat)
52
- end
53
-
54
- # @param [Objects::Submission, Objects::Comment] base The start of the
55
- # comment tree.
56
- # @author Bryce Boe (@bboe) in Python
57
- # @return [Array<Objects::Comment, Objects::MoreComments>] A linear
58
- # array of the submission's comments or the comments' replies.
59
- def flat_comments(base)
60
- meth = (base.is_a?(Objects::Submission) ? :comments : :replies)
61
- stack = base.send(meth).dup
62
- flattened = []
63
-
64
- until stack.empty?
65
- comment = stack.shift
66
- if comment.is_a?(Objects::Comment)
67
- replies = comment.replies
68
- stack = replies + stack if replies
69
- end
70
- flattened << comment
71
- end
72
-
73
- flattened
74
- end
75
-
76
- # Get a given property of a given object.
77
- # @param [Objects::Base, String] object The object with the property.
78
- # @param [Symbol] property The property to get.
79
- def property(object, property)
80
- object.respond_to?(property) ? object.send(property) : object.to_s
81
- end
82
-
83
- private
84
-
85
- # Take a multilevel body ({kind: "tx", data: {...}}) and flatten it
86
- # into something like {kind: "tx", ...}
87
- # @param [Hash] body The response body.
88
- # @return [Hash] The flattened hash.
89
- def flatten_body(body)
90
- data = body[:data] || body
91
- data[:kind] = body[:kind]
92
- data
93
- end
94
-
95
- # @param [String] kind A kind in the format /t[1-5]/.
96
- # @return [Objects::Base, Objects::Listing] The appropriate object for
97
- # a given kind.
98
- def object_from_kind(kind)
99
- OBJECT_KINDS.fetch(kind, Objects::Base)
100
- end
101
- end
102
- end
103
- end
104
- end
1
+ require_relative "../../objects/base"
2
+ require_relative "../../objects/thing"
3
+ require_relative "../../objects/listing"
4
+ require_relative "../../objects/wiki_page"
5
+ require_relative "../../objects/labeled_multi"
6
+ require_relative "../../objects/more_comments"
7
+ require_relative "../../objects/comment"
8
+ require_relative "../../objects/user"
9
+ require_relative "../../objects/submission"
10
+ require_relative "../../objects/private_message"
11
+ require_relative "../../objects/subreddit"
12
+
13
+ module Redd
14
+ module Clients
15
+ class Base
16
+ # Internal methods that make life easier.
17
+ # @todo Move this out to Redd::Utils?
18
+ module Utilities
19
+ # The kind strings and the objects that should be used for them.
20
+ OBJECT_KINDS = {
21
+ "Listing" => Objects::Listing,
22
+ "wikipage" => Objects::WikiPage,
23
+ "LabeledMulti" => Objects::LabeledMulti,
24
+ "more" => Objects::MoreComments,
25
+ "t1" => Objects::Comment,
26
+ "t2" => Objects::User,
27
+ "t3" => Objects::Submission,
28
+ "t4" => Objects::PrivateMessage,
29
+ "t5" => Objects::Subreddit
30
+ }
31
+
32
+ # Request and create an object from the response.
33
+ # @param [Symbol] meth The method to use.
34
+ # @param [String] path The path to visit.
35
+ # @param [Hash] params The data to send with the request.
36
+ # @return [Objects::Base] The object returned from the request.
37
+ def request_object(meth, path, params = {})
38
+ body = send(meth, path, params).body
39
+ object_from_body(body)
40
+ end
41
+
42
+ # Create an object instance with the correct attributes when given a
43
+ # body.
44
+ #
45
+ # @param [Hash] body A JSON hash.
46
+ # @return [Objects::Thing, Objects::Listing]
47
+ def object_from_body(body)
48
+ return nil unless body.is_a?(Hash)
49
+ object = object_from_kind(body[:kind])
50
+ flat = flatten_body(body)
51
+ object.new(self, flat)
52
+ end
53
+
54
+ # @param [Objects::Submission, Objects::Comment] base The start of the
55
+ # comment tree.
56
+ # @author Bryce Boe (@bboe) in Python
57
+ # @return [Array<Objects::Comment, Objects::MoreComments>] A linear
58
+ # array of the submission's comments or the comments' replies.
59
+ def flat_comments(base)
60
+ meth = (base.is_a?(Objects::Submission) ? :comments : :replies)
61
+ stack = base.send(meth).dup
62
+ flattened = []
63
+
64
+ until stack.empty?
65
+ comment = stack.shift
66
+ if comment.is_a?(Objects::Comment)
67
+ replies = comment.replies
68
+ stack = replies + stack if replies
69
+ end
70
+ flattened << comment
71
+ end
72
+
73
+ flattened
74
+ end
75
+
76
+ # Get a given property of a given object.
77
+ # @param [Objects::Base, String] object The object with the property.
78
+ # @param [Symbol] property The property to get.
79
+ def property(object, property)
80
+ object.respond_to?(property) ? object.send(property) : object.to_s
81
+ end
82
+
83
+ private
84
+
85
+ # Take a multilevel body ({kind: "tx", data: {...}}) and flatten it
86
+ # into something like {kind: "tx", ...}
87
+ # @param [Hash] body The response body.
88
+ # @return [Hash] The flattened hash.
89
+ def flatten_body(body)
90
+ data = body[:data] || body
91
+ data[:kind] = body[:kind]
92
+ data
93
+ end
94
+
95
+ # @param [String] kind A kind in the format /t[1-5]/.
96
+ # @return [Objects::Base, Objects::Listing] The appropriate object for
97
+ # a given kind.
98
+ def object_from_kind(kind)
99
+ OBJECT_KINDS.fetch(kind, Objects::Base)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -1,33 +1,33 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "wikiread" scope.
5
- # @note This method is not limited to {Objects::Subreddit} because there
6
- # are also top-level wiki pages.
7
- module Wikiread
8
- # Get a list of pages in the subreddit wiki.
9
- # @param subreddit [Objects::Subreddit, String] The subreddit to
10
- # look in.
11
- # @return [Array<String>] An array of wikipage titles.
12
- def get_wikipages(subreddit = nil)
13
- path = "/wiki/pages.json"
14
- name = property(subreddit, :display_name)
15
- path.prepend("/r/#{name}") if subreddit
16
- get(path).body[:data]
17
- end
18
-
19
- # Get a wiki page.
20
- # @param page [String] The title of the wiki page.
21
- # @param subreddit [Objects::Subreddit, String] The subreddit to
22
- # look in.
23
- # @return [Objects::WikiPage] A wiki page.
24
- def wikipage(page, subreddit = nil)
25
- path = "/wiki/#{page}.json"
26
- name = property(subreddit, :display_name)
27
- path.prepend("/r/#{name}") if subreddit
28
- request_object(:get, path)
29
- end
30
- end
31
- end
32
- end
33
- end
1
+ module Redd
2
+ module Clients
3
+ class Base
4
+ # Methods that require the "wikiread" scope.
5
+ # @note This method is not limited to {Objects::Subreddit} because there
6
+ # are also top-level wiki pages.
7
+ module Wikiread
8
+ # Get a list of pages in the subreddit wiki.
9
+ # @param subreddit [Objects::Subreddit, String] The subreddit to
10
+ # look in.
11
+ # @return [Array<String>] An array of wikipage titles.
12
+ def get_wikipages(subreddit = nil)
13
+ path = "/wiki/pages.json"
14
+ name = property(subreddit, :display_name)
15
+ path.prepend("/r/#{name}") if subreddit
16
+ get(path).body[:data]
17
+ end
18
+
19
+ # Get a wiki page.
20
+ # @param page [String] The title of the wiki page.
21
+ # @param subreddit [Objects::Subreddit, String] The subreddit to
22
+ # look in.
23
+ # @return [Objects::WikiPage] A wiki page.
24
+ def wikipage(page, subreddit = nil)
25
+ path = "/wiki/#{page}.json"
26
+ name = property(subreddit, :display_name)
27
+ path.prepend("/r/#{name}") if subreddit
28
+ request_object(:get, path)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,56 +1,56 @@
1
- require "cgi"
2
- require_relative "base"
3
-
4
- module Redd
5
- module Clients
6
- # The client for installed apps that can't keep a secret.
7
- # It might even work with Rubymotion (fingers crossed).
8
- class Installed < Base
9
- # @!attribute [r] client_id
10
- attr_reader :client_id
11
-
12
- # @!attribute [r] redirect_uri
13
- attr_reader :redirect_uri
14
-
15
- # @param [Hash] options The options to create the client with.
16
- # @see Base#initialize
17
- # @see Redd.it
18
- def initialize(client_id, redirect_uri, **options)
19
- @client_id = client_id
20
- @redirect_uri = redirect_uri
21
- super(**options)
22
- end
23
-
24
- # @param [String] state A random string to double-check later.
25
- # @param [Array<String>] scope The scope to request access to.
26
- # @param [:temporary, :permanent] duration
27
- # @return [String] The url to redirect the user to.
28
- def auth_url(state, scope = ["identity"], duration = :temporary)
29
- query = {
30
- response_type: "token",
31
- client_id: @client_id,
32
- redirect_uri: @redirect_uri,
33
- state: state,
34
- scope: scope.join(","),
35
- duration: duration
36
- }
37
-
38
- url = URI.join(auth_endpoint, "/api/v1/authorize")
39
- url.query = URI.encode_www_form(query)
40
- url.to_s
41
- end
42
-
43
- # Authorize using the url fragment.
44
- # @param [String] fragment The part of the url after the "#".
45
- # @return [Access] The access given by reddit.
46
- def authorize!(fragment)
47
- parsed = CGI.parse(fragment)
48
- @access = Access.new(
49
- access_token: parsed[:access_token].first,
50
- expires_in: parsed[:expires_in].first,
51
- scope: parsed[:scope]
52
- )
53
- end
54
- end
55
- end
56
- end
1
+ require "cgi"
2
+ require_relative "base"
3
+
4
+ module Redd
5
+ module Clients
6
+ # The client for installed apps that can't keep a secret.
7
+ # It might even work with Rubymotion (fingers crossed).
8
+ class Installed < Base
9
+ # @!attribute [r] client_id
10
+ attr_reader :client_id
11
+
12
+ # @!attribute [r] redirect_uri
13
+ attr_reader :redirect_uri
14
+
15
+ # @param [Hash] options The options to create the client with.
16
+ # @see Base#initialize
17
+ # @see Redd.it
18
+ def initialize(client_id, redirect_uri, **options)
19
+ @client_id = client_id
20
+ @redirect_uri = redirect_uri
21
+ super(**options)
22
+ end
23
+
24
+ # @param [String] state A random string to double-check later.
25
+ # @param [Array<String>] scope The scope to request access to.
26
+ # @param [:temporary, :permanent] duration
27
+ # @return [String] The url to redirect the user to.
28
+ def auth_url(state, scope = ["identity"], duration = :temporary)
29
+ query = {
30
+ response_type: "token",
31
+ client_id: @client_id,
32
+ redirect_uri: @redirect_uri,
33
+ state: state,
34
+ scope: scope.join(","),
35
+ duration: duration
36
+ }
37
+
38
+ url = URI.join(auth_endpoint, "/api/v1/authorize")
39
+ url.query = URI.encode_www_form(query)
40
+ url.to_s
41
+ end
42
+
43
+ # Authorize using the url fragment.
44
+ # @param [String] fragment The part of the url after the "#".
45
+ # @return [Access] The access given by reddit.
46
+ def authorize!(fragment)
47
+ parsed = CGI.parse(fragment)
48
+ @access = Access.new(
49
+ access_token: parsed[:access_token].first,
50
+ expires_in: parsed[:expires_in].first,
51
+ scope: parsed[:scope]
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,41 +1,41 @@
1
- require_relative "base"
2
-
3
- module Redd
4
- module Clients
5
- # The client for an account you own (e.g. bots).
6
- class Script < Base
7
- # @!attribute [r] client_id
8
- attr_reader :client_id
9
-
10
- # @!attribute [r] username
11
- attr_reader :username
12
-
13
- # @param [Hash] options The options to create the client with.
14
- # @see Base#initialize
15
- # @see Redd.it
16
- def initialize(client_id, secret, username, password, **options)
17
- @client_id = client_id
18
- @secret = secret
19
- @username = username
20
- @password = password
21
- super(**options)
22
- end
23
-
24
- # Authorize using the given data.
25
- # @return [Access] The access given by reddit.
26
- def authorize!
27
- response = auth_connection.post(
28
- "/api/v1/access_token",
29
- grant_type: "password",
30
- username: @username,
31
- password: @password
32
- )
33
-
34
- @access = Access.new(response.body)
35
- end
36
-
37
- alias_method :refresh_access!, :authorize!
38
-
39
- end
40
- end
41
- end
1
+ require_relative "base"
2
+
3
+ module Redd
4
+ module Clients
5
+ # The client for an account you own (e.g. bots).
6
+ class Script < Base
7
+ # @!attribute [r] client_id
8
+ attr_reader :client_id
9
+
10
+ # @!attribute [r] username
11
+ attr_reader :username
12
+
13
+ # @param [Hash] options The options to create the client with.
14
+ # @see Base#initialize
15
+ # @see Redd.it
16
+ def initialize(client_id, secret, username, password, **options)
17
+ @client_id = client_id
18
+ @secret = secret
19
+ @username = username
20
+ @password = password
21
+ super(**options)
22
+ end
23
+
24
+ # Authorize using the given data.
25
+ # @return [Access] The access given by reddit.
26
+ def authorize!
27
+ response = auth_connection.post(
28
+ "/api/v1/access_token",
29
+ grant_type: "password",
30
+ username: @username,
31
+ password: @password
32
+ )
33
+
34
+ @access = Access.new(response.body)
35
+ end
36
+
37
+ alias_method :refresh_access!, :authorize!
38
+
39
+ end
40
+ end
41
+ end