redd 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +34 -33
  3. data/.rspec +3 -4
  4. data/.rubocop.yml +5 -5
  5. data/.travis.yml +9 -7
  6. data/{LICENSE.md → LICENSE.txt} +22 -22
  7. data/README.md +126 -241
  8. data/Rakefile +5 -6
  9. data/{RedditKit.LICENSE.md → RedditKit.LICENSE.txt} +13 -13
  10. data/lib/redd.rb +50 -4
  11. data/lib/redd/access.rb +76 -0
  12. data/lib/redd/clients/base.rb +178 -0
  13. data/lib/redd/clients/base/account.rb +20 -0
  14. data/lib/redd/clients/base/identity.rb +22 -0
  15. data/lib/redd/clients/base/none.rb +27 -0
  16. data/lib/redd/clients/base/privatemessages.rb +28 -0
  17. data/lib/redd/clients/base/read.rb +66 -0
  18. data/lib/redd/clients/base/stream.rb +74 -0
  19. data/lib/redd/clients/base/submit.rb +54 -0
  20. data/lib/redd/clients/base/utilities.rb +80 -0
  21. data/lib/redd/clients/base/wikiread.rb +33 -0
  22. data/lib/redd/clients/installed.rb +55 -0
  23. data/lib/redd/clients/script.rb +37 -0
  24. data/lib/redd/clients/userless.rb +31 -0
  25. data/lib/redd/clients/web.rb +57 -0
  26. data/lib/redd/error.rb +138 -153
  27. data/lib/redd/objects/base.rb +36 -0
  28. data/lib/redd/objects/comment.rb +22 -0
  29. data/lib/redd/objects/listing.rb +29 -0
  30. data/lib/redd/objects/more_comments.rb +10 -0
  31. data/lib/redd/objects/private_message.rb +18 -0
  32. data/lib/redd/objects/submission.rb +72 -0
  33. data/lib/redd/objects/subreddit.rb +152 -0
  34. data/lib/redd/objects/thing.rb +33 -0
  35. data/lib/redd/objects/thing/editable.rb +22 -0
  36. data/lib/redd/objects/thing/hideable.rb +18 -0
  37. data/lib/redd/objects/thing/inboxable.rb +25 -0
  38. data/lib/redd/objects/thing/messageable.rb +34 -0
  39. data/lib/redd/objects/thing/moderatable.rb +43 -0
  40. data/lib/redd/objects/thing/refreshable.rb +14 -0
  41. data/lib/redd/objects/thing/saveable.rb +21 -0
  42. data/lib/redd/objects/thing/votable.rb +33 -0
  43. data/lib/redd/objects/user.rb +52 -0
  44. data/lib/redd/objects/wiki_page.rb +15 -0
  45. data/lib/redd/rate_limit.rb +50 -49
  46. data/lib/redd/response/parse_json.rb +17 -33
  47. data/lib/redd/response/raise_error.rb +16 -25
  48. data/lib/redd/version.rb +4 -5
  49. data/redd.gemspec +30 -31
  50. data/spec/redd/objects/base_spec.rb +1 -0
  51. data/spec/redd/rate_limit_spec.rb +29 -29
  52. data/spec/redd/response/parse_json_spec.rb +12 -0
  53. data/spec/redd/response/raise_error_spec.rb +11 -0
  54. data/spec/redd_spec.rb +7 -5
  55. data/spec/spec_helper.rb +69 -50
  56. metadata +73 -136
  57. data/.yardopts +0 -1
  58. data/lib/redd/base.rb +0 -56
  59. data/lib/redd/client/authenticated.rb +0 -83
  60. data/lib/redd/client/authenticated/account.rb +0 -13
  61. data/lib/redd/client/authenticated/apps.rb +0 -13
  62. data/lib/redd/client/authenticated/flair.rb +0 -71
  63. data/lib/redd/client/authenticated/gold.rb +0 -13
  64. data/lib/redd/client/authenticated/links_comments.rb +0 -189
  65. data/lib/redd/client/authenticated/live.rb +0 -13
  66. data/lib/redd/client/authenticated/moderation.rb +0 -126
  67. data/lib/redd/client/authenticated/multis.rb +0 -9
  68. data/lib/redd/client/authenticated/private_messages.rb +0 -73
  69. data/lib/redd/client/authenticated/subreddits.rb +0 -172
  70. data/lib/redd/client/authenticated/users.rb +0 -9
  71. data/lib/redd/client/authenticated/wiki.rb +0 -9
  72. data/lib/redd/client/oauth2.rb +0 -71
  73. data/lib/redd/client/oauth2/authorization.rb +0 -108
  74. data/lib/redd/client/oauth2/identity.rb +0 -16
  75. data/lib/redd/client/oauth2_script.rb +0 -24
  76. data/lib/redd/client/oauth2_script/authorization.rb +0 -21
  77. data/lib/redd/client/unauthenticated.rb +0 -118
  78. data/lib/redd/client/unauthenticated/account.rb +0 -30
  79. data/lib/redd/client/unauthenticated/captcha.rb +0 -27
  80. data/lib/redd/client/unauthenticated/links_comments.rb +0 -60
  81. data/lib/redd/client/unauthenticated/listing.rb +0 -65
  82. data/lib/redd/client/unauthenticated/live.rb +0 -9
  83. data/lib/redd/client/unauthenticated/moderation.rb +0 -26
  84. data/lib/redd/client/unauthenticated/subreddits.rb +0 -49
  85. data/lib/redd/client/unauthenticated/users.rb +0 -67
  86. data/lib/redd/client/unauthenticated/utilities.rb +0 -109
  87. data/lib/redd/client/unauthenticated/wiki.rb +0 -33
  88. data/lib/redd/oauth2_access.rb +0 -70
  89. data/lib/redd/object/comment.rb +0 -74
  90. data/lib/redd/object/listing.rb +0 -29
  91. data/lib/redd/object/more_comments.rb +0 -14
  92. data/lib/redd/object/private_message.rb +0 -35
  93. data/lib/redd/object/submission.rb +0 -94
  94. data/lib/redd/object/subreddit.rb +0 -74
  95. data/lib/redd/object/user.rb +0 -34
  96. data/lib/redd/object/wiki_page.rb +0 -27
  97. data/lib/redd/thing.rb +0 -27
  98. data/lib/redd/thing/commentable.rb +0 -27
  99. data/lib/redd/thing/editable.rb +0 -16
  100. data/lib/redd/thing/hideable.rb +0 -16
  101. data/lib/redd/thing/inboxable.rb +0 -20
  102. data/lib/redd/thing/messageable.rb +0 -12
  103. data/lib/redd/thing/moderatable.rb +0 -32
  104. data/lib/redd/thing/reportable.rb +0 -12
  105. data/lib/redd/thing/saveable.rb +0 -16
  106. data/lib/redd/thing/voteable.rb +0 -22
  107. data/spec/README.md +0 -18
  108. data/spec/redd/base_spec.rb +0 -36
  109. data/spec/redd/client/authenticated/account_spec.rb +0 -5
  110. data/spec/redd/client/authenticated/apps_spec.rb +0 -2
  111. data/spec/redd/client/authenticated/flair_spec.rb +0 -26
  112. data/spec/redd/client/authenticated/gold_spec.rb +0 -2
  113. data/spec/redd/client/authenticated/links_comments_spec.rb +0 -231
  114. data/spec/redd/client/authenticated/live_spec.rb +0 -2
  115. data/spec/redd/client/unauthenticated/account_spec.rb +0 -15
  116. data/spec/redd/client/unauthenticated/captcha_spec.rb +0 -23
  117. data/spec/redd/client/unauthenticated/links_comments_spec.rb +0 -28
  118. data/spec/redd/client/unauthenticated/listing_spec.rb +0 -23
  119. data/spec/redd/client/unauthenticated/live_spec.rb +0 -2
  120. data/spec/redd/client/unauthenticated/moderation_spec.rb +0 -14
  121. data/spec/redd/client/unauthenticated/subreddits_spec.rb +0 -35
  122. data/spec/redd/client/unauthenticated/users_spec.rb +0 -34
  123. data/spec/redd/client/unauthenticated/wiki_spec.rb +0 -18
  124. data/spec/redd/oauth2_access_spec.rb +0 -83
  125. data/spec/redd/thing_spec.rb +0 -22
@@ -1,67 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods to interact with other redditors
5
- module Users
6
- # Return a User object from the username of a redditor.
7
- # @param username [String] The username.
8
- # @return [Redd::Object::User]
9
- def user(username)
10
- object_from_response :get, "/user/#{username}/about.json"
11
- end
12
-
13
- # @!method get_user_overview
14
- # @!method get_user_submitted
15
- # @!method get_user_comments
16
- # @!method get_user_liked
17
- # @!method get_user_disliked
18
- # @!method get_user_hidden
19
- # @!method get_user_saved
20
- # @!method get_user_gilded
21
- #
22
- # Get the appropriate listing.
23
- # @param user [Redd::Object::User] The user to query.
24
- # @param params [Hash] A list of params to send with the request.
25
- # @return [Redd::Object::Listing]
26
- #
27
- # @see #get_user_listing
28
- %w(
29
- overview submitted comments liked disliked hidden saved gilded
30
- ).each do |sort|
31
- define_method :"get_user_#{sort}" do |user, params = {}|
32
- get_user_listing(user, sort, params)
33
- end
34
- end
35
-
36
- private
37
-
38
- # Get comments and submissions related to a user.
39
- #
40
- # @param user [Redd::Object::User, String] The user to query.
41
- # @param type [:overview, :submitted, :comments, :liked, :disliked,
42
- # :hidden, :saved, :gilded] The type of listing to return.
43
- # @param params [Hash] A list of params to send with the request.
44
- # @option params [String] :after Return results after the given
45
- # fullname.
46
- # @option params [String] :before Return results before the given
47
- # fullname.
48
- # @option params [Integer] :count (0) The number of items already seen
49
- # in the listing.
50
- # @option params [1..100] :limit (25) The maximum number of things to
51
- # return.
52
- # @option params [:hot, :new, :top, :controversial] :sort The order to
53
- # sort the results by.
54
- # @option params [:hour, :day, :week, :month, :year, :all] :t The
55
- # time period to consider when sorting.
56
- # @return [Redd::Object::Listing] A listing of submissions or comments.
57
- def get_user_listing(user, type, params = {})
58
- name = extract_attribute(user, :name)
59
- path = "/user/#{name}/#{type}.json"
60
- params[:show] ||= :given
61
-
62
- object_from_response :get, path, params
63
- end
64
- end
65
- end
66
- end
67
- end
@@ -1,109 +0,0 @@
1
- require "redd/thing"
2
- require "redd/object/listing"
3
- require "redd/object/comment"
4
- require "redd/object/more_comments"
5
- require "redd/object/private_message"
6
- require "redd/object/submission"
7
- require "redd/object/subreddit"
8
- require "redd/object/user"
9
- require "redd/object/wiki_page"
10
-
11
- module Redd
12
- module Client
13
- class Unauthenticated
14
- # Non-API methods that make life easier
15
- module Utilities
16
- def comment_stream(*args, &block)
17
- submission_stream(:comments, *args, &block)
18
- end
19
-
20
- def submission_stream(listing, subreddit = nil, params = {}, &block)
21
- loop do
22
- # Get the latest comments from the subreddit. By the way, this line
23
- # is the one where the sleeping/rate-limiting happens.
24
- objects = get_listing(listing, subreddit, params)
25
- unless objects.empty?
26
- # Run the loop for each of the new comments accessed.
27
- # I should probably add it to a Set to avoid duplicates.
28
- objects.reverse_each { |object| block.call(object) }
29
- # Set the latest comment.
30
- params[:before] = objects.first.fullname
31
- end
32
- end
33
- end
34
-
35
- private
36
-
37
- def extract_attribute(object, attribute)
38
- case object
39
- when ::String
40
- object
41
- else
42
- object.send(attribute)
43
- end
44
- end
45
-
46
- def extract_fullname(object)
47
- extract_attribute(object, :fullname)
48
- end
49
-
50
- def extract_id(object)
51
- extract_attribute(object, :id)
52
- end
53
-
54
- # rubocop:disable Style/MethodLength, Style/CyclomaticComplexity
55
- def object_from_kind(kind)
56
- case kind
57
- when "Listing"
58
- Redd::Object::Listing
59
- when "more"
60
- Redd::Object::MoreComments
61
- when "t1"
62
- Redd::Object::Comment
63
- when "t2"
64
- Redd::Object::User
65
- when "t3"
66
- Redd::Object::Submission
67
- when "t4"
68
- Redd::Object::PrivateMessage
69
- when "t5"
70
- Redd::Object::Subreddit
71
- when "wikipage"
72
- Redd::Object::WikiPage
73
- else
74
- Redd::Thing
75
- end
76
- end
77
- # rubocop:enable Style/MethodLength, Style/CyclomaticComplexity
78
-
79
- def objects_from_listing(thing)
80
- thing[:data][:children].map do |child|
81
- object_from_body(child)
82
- end
83
- end
84
-
85
- def object_from_body(body)
86
- return nil unless body.is_a?(Hash) && body.key?(:kind)
87
- object = object_from_kind(body[:kind])
88
-
89
- if object == Redd::Object::Listing
90
- body[:data][:children] = objects_from_listing(body)
91
- object.new(body)
92
- else
93
- object.new(self, body)
94
- end
95
- end
96
-
97
- def comments_from_response(*args)
98
- body = request(*args).body[1]
99
- object_from_body(body)
100
- end
101
-
102
- def object_from_response(*args)
103
- body = request(*args).body
104
- object_from_body(body)
105
- end
106
- end
107
- end
108
- end
109
- end
@@ -1,33 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods to interact with subreddit wikis
5
- module Wiki
6
- # Get a list of pages in the subreddit wiki.
7
- # @param subreddit [Redd::Object::Subreddit, String] The subreddit to
8
- # look in.
9
- # @return [Array] An array of wikipage titles.
10
- def get_wikipages(subreddit = nil)
11
- name = extract_attribute(subreddit, :display_name)
12
-
13
- path = "/wiki/pages.json"
14
- path.prepend("/r/#{name}") if subreddit
15
- get(path)[:data]
16
- end
17
-
18
- # Get a wiki page.
19
- # @param page [String] The title of the wiki page.
20
- # @param subreddit [Redd::Object::Subreddit, String] The subreddit to
21
- # look in.
22
- # @return [Redd::Object::WikiPage] A wiki page.
23
- def wikipage(page, subreddit = nil)
24
- name = extract_attribute(subreddit, :display_name)
25
-
26
- path = "/wiki/#{page}.json"
27
- path.prepend("/r/#{name}") if subreddit
28
- object_from_response :get, path
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,70 +0,0 @@
1
- require "multi_json"
2
-
3
- module Redd
4
- # A container for the client's access to their account via OAuth2
5
- class OAuth2Access
6
- # @!attribute [r] access_token
7
- # @return [String] The access token used to access the users account.
8
- attr_reader :access_token
9
-
10
- # @!attribute [r] refresh_token
11
- # @return [String, nil] The refresh token, if the access was permanent.
12
- attr_reader :refresh_token
13
-
14
- # @!attribute [r] scope
15
- # @return [Array] The scope that the client is allowed to access.
16
- attr_reader :scope
17
-
18
- # @!attribute [r] duration
19
- # @return [Symbol] Time the client is given access (temporary/permanent)
20
- attr_reader :duration
21
-
22
- # @!attribute [r] expires_at
23
- # @return [Time] The time when the access token expires.
24
- attr_reader :expires_at
25
-
26
- # @param response [String] The response body containing the required info.
27
- def initialize(response)
28
- @access_token = response[:access_token]
29
- @refresh_token = response[:refresh_token]
30
- @scope = response[:scope].split(",").map { |s| s.to_sym }
31
- @duration = @refresh_token ? :permanent : :temporary
32
- @expires_at =
33
- if response[:expires_at]
34
- Time.at(response[:expires_at])
35
- else
36
- Time.now + response[:expires_in]
37
- end
38
- end
39
-
40
- # Refresh the object with the new response.
41
- # This happens when a new access token is requested using a request token.
42
- # @param response [Hash] The new response body.
43
- def refresh(response)
44
- @access_token = response[:access_token]
45
- @expires_at = Time.now + response[:expires_in]
46
- end
47
-
48
- # @return [Boolean] Whether the access has expired.
49
- def expired?
50
- Time.now > @expires_at
51
- end
52
-
53
- # @return [String] A JSON representation of the data that can be loaded later.
54
- def to_json
55
- MultiJson.dump(
56
- access_token: @access_token,
57
- refresh_token: @refresh_token,
58
- scope: @scope.join(","),
59
- expires_at: @expires_at.to_i
60
- )
61
- end
62
-
63
- # Create a new instance of the class from the JSON returned from #to_json
64
- # @return [OAuth2Access]
65
- def self.from_json(json)
66
- hash = MultiJson.load(json, symbolize_keys: true)
67
- new(hash)
68
- end
69
- end
70
- end
@@ -1,74 +0,0 @@
1
- require "redd/thing"
2
-
3
- module Redd
4
- module Object
5
- # A comment made on links.
6
- class Comment < Redd::Thing
7
- require "redd/thing/commentable"
8
- require "redd/thing/editable"
9
- require "redd/thing/inboxable"
10
- require "redd/thing/moderatable"
11
- require "redd/thing/reportable"
12
- require "redd/thing/voteable"
13
-
14
- include Redd::Thing::Commentable
15
- include Redd::Thing::Editable
16
- include Redd::Thing::Inboxable
17
- include Redd::Thing::Moderatable
18
- include Redd::Thing::Reportable
19
- include Redd::Thing::Voteable
20
-
21
- attr_reader :author
22
-
23
- attr_reader :edited
24
- attr_reader :saved
25
- attr_reader :gilded
26
-
27
- attr_reader :ups
28
- attr_reader :downs
29
- attr_reader :score
30
- attr_reader :likes
31
- attr_reader :controversiality
32
-
33
- attr_reader :banned_by
34
- attr_reader :approved_by
35
- attr_reader :score_hidden
36
- attr_reader :distinguished
37
- attr_reader :num_reports
38
-
39
- attr_reader :parent_id
40
- attr_reader :link_id
41
- attr_reader :body
42
- attr_reader :body_html
43
- attr_reader :author_flair_text
44
- attr_reader :author_flair_css_class
45
-
46
- alias_method :reports_count, :num_reports
47
-
48
- def comments
49
- @comments ||= client.get_replies(self)
50
- end
51
- alias_method :replies, :comments
52
-
53
- def subreddit
54
- @subreddit ||= client.subreddit(@attributes[:subreddit])
55
- end
56
-
57
- def submission
58
- @submission ||= client.get_info(id: link_id).first
59
- end
60
-
61
- def created
62
- @created ||= Time.at(@attributes[:created_utc])
63
- end
64
-
65
- def root?
66
- !parent_id || parent_id == link_id
67
- end
68
-
69
- def gilded?
70
- gilded > 0
71
- end
72
- end
73
- end
74
- end
@@ -1,29 +0,0 @@
1
- require "forwardable"
2
-
3
- module Redd
4
- module Object
5
- # A listing of various reddit things.
6
- # @see http://www.reddit.com/dev/api#listings
7
- # @see http://stackoverflow.com/a/2080118
8
- class Listing
9
- include Enumerable
10
- extend Forwardable
11
- def_delegators :@things, :[], :length, :size, :each, :map, :empty?
12
-
13
- # @!attribute [r] things
14
- # @return [Array] A list of things in the listing.
15
- attr_reader :things
16
-
17
- attr_reader :kind
18
- attr_reader :after
19
- attr_reader :before
20
-
21
- def initialize(listing)
22
- @kind = "Listing"
23
- @things = listing[:data][:children]
24
- @after = listing[:data][:after]
25
- @before = listing[:data][:before]
26
- end
27
- end
28
- end
29
- end
@@ -1,14 +0,0 @@
1
- module Redd
2
- module Object
3
- # The model for a morecomments object
4
- class MoreComments < Redd::Base
5
- attr_reader :count
6
- attr_reader :parent_id
7
- attr_reader :children
8
-
9
- def expand(submission = nil)
10
- client.expand_morecomments(self, submission)
11
- end
12
- end
13
- end
14
- end
@@ -1,35 +0,0 @@
1
- require "redd/thing"
2
-
3
- module Redd
4
- module Object
5
- # The model for private messages
6
- class PrivateMessage < Redd::Thing
7
- require "redd/thing/inboxable"
8
-
9
- include Redd::Thing::Inboxable
10
-
11
- attr_reader :body
12
- attr_reader :body_html
13
- attr_reader :subreddit
14
- attr_reader :parent_id
15
- attr_reader :distinguished
16
- attr_reader :was_comment
17
- attr_reader :first_message_name
18
- attr_reader :context
19
-
20
- attr_reader :dest
21
- attr_reader :author
22
-
23
- alias_method :from, :author
24
- alias_method :to, :dest
25
-
26
- def created
27
- @created ||= Time.at(@attributes[:created_utc])
28
- end
29
-
30
- def replies
31
- @replies ||= client.objects_from_listing(@attributes[:replies])
32
- end
33
- end
34
- end
35
- end
@@ -1,94 +0,0 @@
1
- require "redd/thing"
2
-
3
- module Redd
4
- module Object
5
- # A submission made in a subreddit.
6
- class Submission < Redd::Thing
7
- require "redd/thing/commentable"
8
- require "redd/thing/editable"
9
- require "redd/thing/hideable"
10
- require "redd/thing/moderatable"
11
- require "redd/thing/reportable"
12
- require "redd/thing/saveable"
13
- require "redd/thing/voteable"
14
-
15
- include Redd::Thing::Commentable
16
- include Redd::Thing::Editable
17
- include Redd::Thing::Hideable
18
- include Redd::Thing::Moderatable
19
- include Redd::Thing::Saveable
20
- include Redd::Thing::Voteable
21
-
22
- attr_reader :author
23
-
24
- attr_reader :edited
25
- attr_reader :saved
26
- attr_reader :gilded
27
- attr_reader :clicked
28
- attr_reader :visited
29
- attr_reader :stickied
30
- attr_reader :hidden
31
-
32
- attr_reader :ups
33
- attr_reader :downs
34
- attr_reader :score
35
- attr_reader :likes
36
-
37
- attr_reader :banned_by
38
- attr_reader :approved_by
39
- attr_reader :distinguished
40
- attr_reader :num_reports
41
-
42
- attr_reader :link_flair_text
43
- attr_reader :link_flair_css_class
44
- attr_reader :author_flair_css_class
45
- attr_reader :author_flair_text
46
-
47
- attr_reader :domain
48
- attr_reader :media
49
- attr_reader :media_embed
50
- attr_reader :selftext
51
- attr_reader :selftext_html
52
- attr_reader :secure_media
53
- attr_reader :secure_media_embed
54
- attr_reader :over_18
55
- attr_reader :thumbnail
56
- attr_reader :is_self
57
- attr_reader :url
58
- attr_reader :title
59
- attr_reader :num_comments
60
-
61
- alias_method :nsfw?, :over_18
62
- alias_method :self?, :is_self
63
- alias_method :comments_count, :num_comments
64
-
65
- def subreddit_name
66
- @subreddit_name ||= @attributes[:subreddit]
67
- end
68
-
69
- def subreddit
70
- @subreddit ||= client.subreddit(@attributes[:subreddit])
71
- end
72
-
73
- def comments
74
- @comments ||= client.submission_comments(id)
75
- end
76
-
77
- def created
78
- @created ||= Time.at(@attributes[:created_utc])
79
- end
80
-
81
- def permalink
82
- "http://www.reddit.com" + attributes[:permalink]
83
- end
84
-
85
- def short_url
86
- "http://redd.it/" + id
87
- end
88
-
89
- def gilded?
90
- gilded > 0
91
- end
92
- end
93
- end
94
- end