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,16 +0,0 @@
1
- module Redd
2
- module Client
3
- class OAuth2
4
- # Methods to interact with the user logged-in via OAuth2
5
- module Identity
6
- # @return [Redd::Object::User] The logged-in user.
7
- # @note This overrides the Authenticated class's method, since that
8
- # method doesn't apply here but does the same thing.
9
- def me
10
- response = get "/api/v1/me.json"
11
- object_from_body kind: "t2", data: response
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,24 +0,0 @@
1
- require "redd/client/oauth2"
2
-
3
- module Redd
4
- module Client
5
- class OAuth2Script < Redd::Client::OAuth2
6
- require "redd/client/oauth2_script/authorization"
7
-
8
- include Redd::Client::OAuth2Script::Authorization
9
-
10
- attr_reader :username
11
-
12
- def initialize(client_id, secret, username, password, options = {})
13
- @client_id = client_id
14
- @secret = secret
15
- @username = username
16
- @password = password
17
-
18
- @rate_limit = options[:rate_limit] || Redd::RateLimit.new(1)
19
- @api_endpoint = options[:api_endpoint] || "https://oauth.reddit.com/"
20
- @auth_endpoint = options[:auth_endpoint] || "https://ssl.reddit.com/"
21
- end
22
- end
23
- end
24
- end
@@ -1,21 +0,0 @@
1
- require "redd/oauth2_access"
2
-
3
- module Redd
4
- module Client
5
- class OAuth2Script
6
- # Methods for obtaining an access token
7
- module Authorization
8
- def request_access(set_access = true)
9
- response = auth_connection.post "/api/v1/access_token",
10
- grant_type: "password",
11
- username: @username,
12
- password: @password
13
-
14
- access = Redd::OAuth2Access.new(response.body)
15
- @access = access if set_access
16
- access
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,118 +0,0 @@
1
- require "faraday"
2
- require "redd/version"
3
- require "redd/rate_limit"
4
- require "redd/response/parse_json"
5
- require "redd/response/raise_error"
6
-
7
- module Redd
8
- module Client
9
- # The Client used to connect without needing login credentials.
10
- class Unauthenticated
11
- require "redd/client/unauthenticated/account"
12
- require "redd/client/unauthenticated/captcha"
13
- require "redd/client/unauthenticated/links_comments"
14
- require "redd/client/unauthenticated/listing"
15
- require "redd/client/unauthenticated/live"
16
- require "redd/client/unauthenticated/moderation"
17
- require "redd/client/unauthenticated/subreddits"
18
- require "redd/client/unauthenticated/utilities"
19
- require "redd/client/unauthenticated/users"
20
- require "redd/client/unauthenticated/wiki"
21
-
22
- include Redd::Client::Unauthenticated::Account
23
- include Redd::Client::Unauthenticated::Captcha
24
- include Redd::Client::Unauthenticated::LinksComments
25
- include Redd::Client::Unauthenticated::Listing
26
- include Redd::Client::Unauthenticated::Live
27
- include Redd::Client::Unauthenticated::Moderation
28
- include Redd::Client::Unauthenticated::Subreddits
29
- include Redd::Client::Unauthenticated::Utilities
30
- include Redd::Client::Unauthenticated::Users
31
- include Redd::Client::Unauthenticated::Wiki
32
-
33
- # @!attribute [r] api_endpoint
34
- # @return [String] The site to connect to.
35
- attr_accessor :api_endpoint
36
-
37
- # @!attribute [r] user_agent
38
- # @return [String] The user-agent used to communicate with reddit.
39
- attr_accessor :user_agent
40
-
41
- # @!attribute [r] rate_limit
42
- # @return [#after_limit] The handler that takes care of rate limiting.
43
- attr_accessor :rate_limit
44
-
45
- # Set up an unauthenticated connection to reddit.
46
- #
47
- # @param [Hash] options A hash of options to connect using.
48
- # @option options [#after_limit] :rate_limit The handler that takes care
49
- # of rate limiting.
50
- # @option options [String] :user_agent The User-Agent string to use in the
51
- # header of every request.
52
- # @option options [String] :api_endpoint The main domain to connect
53
- # to, in this case, the URL for reddit.
54
- def initialize(options = {})
55
- @rate_limit = options[:rate_limit] || Redd::RateLimit.new
56
- @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
57
- @api_endpoint = options[:api_endpoint] || "https://www.reddit.com/"
58
- end
59
-
60
- private
61
-
62
- # @return [Hash] The headers that are sent with every request.
63
- def headers
64
- @headers ||= {"User-Agent" => @user_agent}
65
- end
66
-
67
- # Gets the Faraday connection or creates one if it doesn't exist yet.
68
- #
69
- # @return [Faraday] A new or existing Faraday connection.
70
- def connection
71
- @connection ||= Faraday.new(url: api_endpoint) do |faraday|
72
- faraday.use Faraday::Request::UrlEncoded
73
- faraday.use Redd::Response::RaiseError
74
- faraday.use Redd::Response::ParseJson
75
- faraday.adapter Faraday.default_adapter
76
-
77
- faraday.headers = headers
78
- end
79
- end
80
-
81
- # Send a request to the given path.
82
- #
83
- # @param [#to_sym] method The HTTP verb to use.
84
- # @param [String] path The path under the api endpoint to request from.
85
- # @param [Hash] params The additional parameters to send.
86
- # @return [Faraday::Response] The faraday response.
87
- def request(method, path, params = {})
88
- rate_limit.after_limit do
89
- connection.send(method.to_sym, path, params)
90
- end
91
- end
92
-
93
- # Performs a GET request via {#request}.
94
- # @see #request
95
- def get(*args)
96
- request(:get, *args).body
97
- end
98
-
99
- # Performs a POST request via {#request}.
100
- # @see #request
101
- def post(*args)
102
- request(:post, *args).body
103
- end
104
-
105
- # Performs a PUT request via {#request}.
106
- # @see #request
107
- def put(*args)
108
- request(:put, *args).body
109
- end
110
-
111
- # Performs a DELETE request via {#request}.
112
- # @see #request
113
- def delete(*args)
114
- request(:delete, *args).body
115
- end
116
- end
117
- end
118
- end
@@ -1,30 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods for managing accounts.
5
- module Account
6
- # Login to the reddit account.
7
- #
8
- # @param username [String] The username.
9
- # @param password [String] The password.
10
- # @param remember [Boolean] Indicates whether you intend to use the
11
- # returned cookie for a long time.
12
- # @param options [Hash] The options to create an instance of
13
- # {Redd::Client::Authenticated}.
14
- def login(username, password, remember = false, options = {})
15
- response = post "/api/login",
16
- api_type: "json",
17
- user: username,
18
- passwd: password,
19
- rem: remember
20
- data = response[:json][:data]
21
-
22
- require "redd/client/authenticated"
23
- Redd::Client::Authenticated.new(
24
- data[:cookie], data[:modhash], options
25
- )
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,27 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods to get captchas.
5
- # Many things like sending messages and posting links require captchas.
6
- module Captcha
7
- # @return [Boolean] Whether a captcha is required for some API methods.
8
- def needs_captcha?
9
- get "/api/needs_captcha.json"
10
- end
11
-
12
- # Create a new captcha identifier.
13
- # @return [String] The identifier.
14
- def new_captcha
15
- response = post "/api/new_captcha", api_type: "json"
16
- response[:json][:data][:iden]
17
- end
18
-
19
- # @param identifier [String] The captcha identifier.
20
- # @return The url for the captcha image.
21
- def captcha_url(identifier)
22
- "http://www.reddit.com/captcha/#{identifier}.png"
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,60 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- module LinksComments
5
- # @param params [Hash] A hash of parameters to send to reddit.
6
- # @option params [String] :id The fullname of a thing.
7
- # @option params [String] :url The url of a thing. If an id is also
8
- # provided, the id will take precedence.
9
- # @return [Redd::Object::Listing] Listing of the object or objects.
10
- #
11
- # @note Reddit does accept a subreddit, but with fullnames and urls, I
12
- # assumed that was unnecessary.
13
- def get_info(params = {})
14
- object_from_response :get, "/api/info.json", params
15
- end
16
-
17
- # Get the comments for a submission.
18
- #
19
- # @param submission [String, Redd::Object::Submission] The submission
20
- # to get the comments for.
21
- # @return [Redd::Object::Listing] A listing of comments.
22
- def submission_comments(submission)
23
- id = extract_id(submission)
24
- comments_from_response :get, "/comments/#{id}.json"
25
- end
26
-
27
- # Get the replies for a comment.
28
- #
29
- # @param comment [String, Redd::Object::Submission] The comment to get
30
- # the replies for.
31
- # @return [Redd::Object::Listing] A listing of comments.
32
- def get_replies(comment)
33
- replies = comment.attributes[:replies]
34
- return [] unless replies.is_a?(Hash) && replies.key?(:kind)
35
- object_from_body(replies)
36
- end
37
-
38
- def expand_morecomments(morecomments, submission = nil)
39
- parent_id = morecomments.parent_id
40
- link_id =
41
- if submission
42
- extract_fullname(submission)
43
- elsif parent_id.start_with?("t3_")
44
- parent_id
45
- elsif parent_id.start_with?("t1_")
46
- get_info(id: parent_id).first.link_id
47
- end
48
-
49
- response = post "/api/morechildren.json",
50
- api_type: "json",
51
- link_id: link_id,
52
- children: morecomments.children.join(",")
53
- comments = response[:json][:data][:things]
54
-
55
- object_from_body(kind: "Listing", data: {children: comments})
56
- end
57
- end
58
- end
59
- end
60
- end
@@ -1,65 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods that return a listing
5
- module Listing
6
- # Get an object based on its id.
7
- #
8
- # @param fullname [String] The fullname of a thing.
9
- # @return [Redd::Object] The object with the id.
10
- def by_id(fullname)
11
- object_from_response :get, "/by_id/#{fullname}.json"
12
- end
13
-
14
- # @!method get_hot
15
- # @!method get_new
16
- # @!method get_top
17
- # @!method get_controversial
18
- # @!method get_comments
19
- #
20
- # Get the appropriate listing.
21
- # @param subreddit [Redd::Object::Subreddit] The subreddit to query.
22
- # @param params [Hash] A list of params to send with the request.
23
- # @return [Redd::Object::Listing]
24
- #
25
- # @see #get_listing
26
- %w(hot new top controversial comments).each do |sort|
27
- define_method :"get_#{sort}" do |subreddit = nil, params = {}|
28
- get_listing(sort, subreddit, params)
29
- end
30
- end
31
-
32
- private
33
-
34
- # Get the front page of reddit or a subreddit sorted by type.
35
- #
36
- # @param type [:hot, :new, :random, :top, :controversial, :comments]
37
- # The type of listing to return
38
- # @param subreddit [Redd::Object::Subreddit, String] The subreddit to
39
- # query.
40
- # @param params [Hash] A list of params to send with the request.
41
- # @option params [String] :after Return results after the given
42
- # fullname.
43
- # @option params [String] :before Return results before the given
44
- # fullname.
45
- # @option params [Integer] :count (0) The number of items already seen
46
- # in the listing.
47
- # @option params [1..100] :limit (25) The maximum number of things to
48
- # return.
49
- # @option params [:hour, :day, :week, :month, :year, :all] :t The
50
- # time period to consider when sorting.
51
- # @return [Redd::Object::Listing] A listing of submissions or comments.
52
- #
53
- # @note The option :t only applies to the top and controversial sorts.
54
- def get_listing(type, subreddit = nil, params = {})
55
- name = extract_attribute(subreddit, :display_name) if subreddit
56
-
57
- path = "/#{type}.json"
58
- path = path.prepend("/r/#{name}") if name
59
-
60
- object_from_response :get, path, params
61
- end
62
- end
63
- end
64
- end
65
- end
@@ -1,9 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods to interact with /r/live threads
5
- module Live
6
- end
7
- end
8
- end
9
- end
@@ -1,26 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods that deal with subreddit styles
5
- module Moderation
6
- # @param subreddit [Redd::Object::Subreddit, String] The subreddit to
7
- # query.
8
- # @return [String] The url for the subreddit's css stylesheet.
9
- def stylesheet_url(subreddit = nil)
10
- name = extract_attribute(subreddit, :display_name) if subreddit
11
- path = "/stylesheet"
12
- path = path.prepend("/r/#{name}") if subreddit
13
-
14
- request(:get, path).headers[:location]
15
- end
16
-
17
- # @param subreddit [Redd::Object::Subreddit, String] The subreddit to
18
- # query.
19
- # @return [String] The css stylesheet for the subreddit.
20
- def stylesheet(subreddit = nil)
21
- get stylesheet_url(subreddit)
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,49 +0,0 @@
1
- module Redd
2
- module Client
3
- class Unauthenticated
4
- # Methods that deal with subreddits
5
- module Subreddits
6
- # Return a Subreddit object from the title of a subreddit.
7
- # @param title [String] The name of the subreddit.
8
- # @return [Redd::Object::Subreddit]
9
- def subreddit(title)
10
- object_from_response :get, "/r/#{title}/about.json"
11
- end
12
-
13
- # Get a list of subreddits sorted by the given parameter.
14
- # @param where [:popular, :new] The type of subreddits to look for.
15
- # @param params [Hash] A hash of parameters to send with the request.
16
- # @option params [String] :after Return results after the given
17
- # fullname.
18
- # @option params [String] :before Return results before the given
19
- # fullname.
20
- # @option params [Integer] :count (0) The number of items already seen
21
- # in the listing.
22
- # @option params [1..100] :limit (25) The maximum number of things to
23
- # return.
24
- # @return [Redd::Object::Listing] A listing of subreddits.
25
- def get_subreddits(where = :popular, params = {})
26
- object_from_response :get, "/subreddits/#{where}.json", params
27
- end
28
-
29
- # Look for subreddits matching the given query.
30
- #
31
- # @param query [String] The search query.
32
- # @param params [Hash] A hash of parameters to send with the request.
33
- # @option params [String] :after Return results after the given
34
- # fullname.
35
- # @option params [String] :before Return results before the given
36
- # fullname.
37
- # @option params [Integer] :count (0) The number of items already seen
38
- # in the listing.
39
- # @option params [1..100] :limit (25) The maximum number of things to
40
- # return.
41
- # @return [Redd::Object::Listing] A listing of subreddits.
42
- def search_subreddits(query, params = {})
43
- params[:q] = query
44
- object_from_response :get, "/subreddits/search.json", params
45
- end
46
- end
47
- end
48
- end
49
- end