redd 0.7.10 → 0.8.0.pre.1

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 (91) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -30
  3. data/.rspec +1 -1
  4. data/.rubocop.yml +16 -3
  5. data/.travis.yml +13 -7
  6. data/Gemfile +3 -1
  7. data/LICENSE.txt +21 -0
  8. data/README.md +40 -126
  9. data/Rakefile +10 -3
  10. data/TODO.md +11 -0
  11. data/bin/console +84 -0
  12. data/bin/setup +8 -0
  13. data/lib/redd.rb +84 -46
  14. data/lib/redd/api_client.rb +109 -0
  15. data/lib/redd/auth_strategies/auth_strategy.rb +60 -0
  16. data/lib/redd/auth_strategies/installed.rb +22 -0
  17. data/lib/redd/auth_strategies/script.rb +23 -0
  18. data/lib/redd/auth_strategies/userless.rb +17 -0
  19. data/lib/redd/auth_strategies/web.rb +29 -0
  20. data/lib/redd/client.rb +88 -0
  21. data/lib/redd/error.rb +19 -142
  22. data/lib/redd/models/access.rb +20 -0
  23. data/lib/redd/models/basic_model.rb +124 -0
  24. data/lib/redd/models/comment.rb +51 -0
  25. data/lib/redd/models/front_page.rb +71 -0
  26. data/lib/redd/models/inboxable.rb +23 -0
  27. data/lib/redd/models/lazy_model.rb +63 -0
  28. data/lib/redd/models/listing.rb +26 -0
  29. data/lib/redd/models/messageable.rb +20 -0
  30. data/lib/redd/models/moderatable.rb +41 -0
  31. data/lib/redd/models/more_comments.rb +10 -0
  32. data/lib/redd/models/multireddit.rb +32 -0
  33. data/lib/redd/models/postable.rb +70 -0
  34. data/lib/redd/models/private_message.rb +29 -0
  35. data/lib/redd/models/replyable.rb +16 -0
  36. data/lib/redd/models/session.rb +86 -0
  37. data/lib/redd/models/submission.rb +40 -0
  38. data/lib/redd/models/subreddit.rb +201 -0
  39. data/lib/redd/models/user.rb +72 -0
  40. data/lib/redd/models/wiki_page.rb +24 -0
  41. data/lib/redd/utilities/error_handler.rb +35 -0
  42. data/lib/redd/utilities/rate_limiter.rb +21 -0
  43. data/lib/redd/utilities/stream.rb +63 -0
  44. data/lib/redd/utilities/unmarshaller.rb +39 -0
  45. data/lib/redd/version.rb +4 -3
  46. data/logo.png +0 -0
  47. data/redd.gemspec +26 -22
  48. metadata +73 -99
  49. data/LICENSE.md +0 -22
  50. data/RedditKit.LICENSE.md +0 -9
  51. data/lib/redd/access.rb +0 -76
  52. data/lib/redd/clients/base.rb +0 -188
  53. data/lib/redd/clients/base/account.rb +0 -20
  54. data/lib/redd/clients/base/identity.rb +0 -22
  55. data/lib/redd/clients/base/none.rb +0 -27
  56. data/lib/redd/clients/base/privatemessages.rb +0 -33
  57. data/lib/redd/clients/base/read.rb +0 -113
  58. data/lib/redd/clients/base/stream.rb +0 -81
  59. data/lib/redd/clients/base/submit.rb +0 -19
  60. data/lib/redd/clients/base/utilities.rb +0 -104
  61. data/lib/redd/clients/base/wikiread.rb +0 -33
  62. data/lib/redd/clients/installed.rb +0 -57
  63. data/lib/redd/clients/script.rb +0 -41
  64. data/lib/redd/clients/userless.rb +0 -32
  65. data/lib/redd/clients/web.rb +0 -58
  66. data/lib/redd/objects/base.rb +0 -39
  67. data/lib/redd/objects/comment.rb +0 -22
  68. data/lib/redd/objects/labeled_multi.rb +0 -13
  69. data/lib/redd/objects/listing.rb +0 -29
  70. data/lib/redd/objects/more_comments.rb +0 -11
  71. data/lib/redd/objects/private_message.rb +0 -28
  72. data/lib/redd/objects/submission.rb +0 -139
  73. data/lib/redd/objects/subreddit.rb +0 -330
  74. data/lib/redd/objects/thing.rb +0 -26
  75. data/lib/redd/objects/thing/editable.rb +0 -22
  76. data/lib/redd/objects/thing/hideable.rb +0 -18
  77. data/lib/redd/objects/thing/inboxable.rb +0 -25
  78. data/lib/redd/objects/thing/messageable.rb +0 -34
  79. data/lib/redd/objects/thing/moderatable.rb +0 -43
  80. data/lib/redd/objects/thing/refreshable.rb +0 -14
  81. data/lib/redd/objects/thing/saveable.rb +0 -21
  82. data/lib/redd/objects/thing/votable.rb +0 -33
  83. data/lib/redd/objects/user.rb +0 -52
  84. data/lib/redd/objects/wiki_page.rb +0 -15
  85. data/lib/redd/rate_limit.rb +0 -88
  86. data/lib/redd/response/parse_json.rb +0 -18
  87. data/lib/redd/response/raise_error.rb +0 -16
  88. data/spec/redd/objects/base_spec.rb +0 -1
  89. data/spec/redd/response/raise_error_spec.rb +0 -11
  90. data/spec/redd_spec.rb +0 -5
  91. data/spec/spec_helper.rb +0 -71
data/LICENSE.md DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2015 Avinash Dwarapu
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,9 +0,0 @@
1
- Copyright (c) 2013 Sam Symons
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,76 +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 Access
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] expires_at
19
- # @return [Time] The time when the access token expires.
20
- attr_reader :expires_at
21
-
22
- # @param [String] body The response body containing the required info.
23
- def initialize(body)
24
- @access_token = body[:access_token]
25
- @refresh_token = body[:refresh_token]
26
- @scope = (body[:scope] ? body[:scope].split(",") : [])
27
- @expires_at =
28
- if body[:expires_at]
29
- Time.at(body[:expires_at])
30
- else
31
- Time.now + (body[:expires_in] || 0)
32
- end
33
- end
34
-
35
- # @return [Boolean] Whether the access is temporary.
36
- def temporary?
37
- !refresh_token
38
- end
39
-
40
- # @return [Boolean] Whether the access is permanent.
41
- def permanent?
42
- !temporary?
43
- end
44
-
45
- # @return [Boolean] Whether the access has expired.
46
- def expired?
47
- Time.now > (@expires_at + 60)
48
- end
49
-
50
- # Refresh the object with the new response body.
51
- # This happens when a new access token is requested using a request token.
52
- # @param [Hash] body The new response body.
53
- def refreshed!(body)
54
- @access_token = body[:access_token]
55
- @expires_at = Time.now + body[:expires_in]
56
- end
57
-
58
- # @return [String] A JSON version of the data that can be loaded later.
59
- def to_json
60
- MultiJson.dump(
61
- access_token: @access_token,
62
- refresh_token: @refresh_token,
63
- scope: @scope.join(","),
64
- expires_at: @expires_at.to_i
65
- )
66
- end
67
-
68
- # Create a new instance of the class from the JSON returned from #to_json
69
- # @param [String] json
70
- # @return [Access]
71
- def self.from_json(json)
72
- hash = MultiJson.load(json, symbolize_keys: true)
73
- new(hash)
74
- end
75
- end
76
- end
@@ -1,188 +0,0 @@
1
- require "faraday"
2
- require_relative "../version"
3
- require_relative "../response/raise_error"
4
- require_relative "../response/parse_json"
5
- require_relative "../rate_limit"
6
- require_relative "../access"
7
-
8
- module Redd
9
- # The module containing the multiple types of clients.
10
- module Clients
11
- # The basic client to inherit from. Don't use this directly, prefer
12
- # {Redd.it}
13
- class Base
14
- # @!parse include Utilities
15
- # @!parse include Account
16
- # @!parse include Identity
17
- # @!parse include None
18
- # @!parse include Privatemessages
19
- # @!parse include Read
20
- # @!parse include Submit
21
- # @!parse include Stream
22
- # @!parse include Wikiread
23
- %w(
24
- utilities account identity none privatemessages read submit stream
25
- wikiread
26
- ).each do |mixin_name|
27
- require_relative "base/#{mixin_name}"
28
- camel_case = mixin_name.split("_").map(&:capitalize).join
29
- include const_get(camel_case)
30
- end
31
-
32
- # @!attribute [r] user_agent
33
- # @return [String] The user-agent used to communicate with reddit.
34
- attr_reader :user_agent
35
-
36
- # @!attribute [r] rate_limit
37
- # @return [#after_limit] The handler that takes care of rate limiting.
38
- attr_reader :rate_limit
39
-
40
- # @!attribute [r] auth_endpoint
41
- # @return [String] The site to connect to for authentication.
42
- attr_reader :auth_endpoint
43
-
44
- # @!attribute [r] api_endpoint
45
- # @return [String] The site to make API requests with.
46
- attr_reader :api_endpoint
47
-
48
- # @!attribute [rw] access
49
- # @return [Access] The access object to make API requests with.
50
- attr_accessor :access
51
-
52
- # Create a Client.
53
- #
54
- # @param [Hash] options The options to create the client with.
55
- # @option options [String] :user_agent The User-Agent string to use in
56
- # the header of every request.
57
- # @option options [#after_limit] :rate_limit The handler that takes care
58
- # of rate limiting.
59
- # @option options [String] :auth_endpoint The main domain to authenticate
60
- # with.
61
- # @option options [String] :api_endpoint The main domain to make requests
62
- # with.
63
- # @note HTTPS is mandatory for OAuth2.
64
- def initialize(**options)
65
- @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
66
- @rate_limit = options[:rate_limit] || RateLimit.new(1)
67
- @auth_endpoint = options[:auth_endpoint] || "https://www.reddit.com/"
68
- @api_endpoint = options[:api_endpoint] || "https://oauth.reddit.com/"
69
- @access = Access.new(expires_at: Time.at(0))
70
- end
71
-
72
- # @!method get(path, params = {})
73
- # @!method post(path, params = {})
74
- # @!method put(path, params = {})
75
- # @!method patch(path, params = {})
76
- # @!method delete(path, params = {})
77
- #
78
- # Sends the request to the given path with the given params and return
79
- # the body of the response.
80
- # @param [String] path The path under the api_endpoint to request.
81
- # @param [Hash] params The parameters to send with the request.
82
- # @return [Faraday::Response] The response.
83
- [:get, :post, :put, :patch, :delete].each do |meth|
84
- define_method(meth) do |path, params = {}|
85
- @rate_limit.after_limit do
86
- final_params = default_params.merge(params)
87
- connection.send(meth, path, final_params)
88
- end
89
- end
90
- end
91
-
92
- # @param [Access] new_access The access to use.
93
- # @yield The client with the given access.
94
- def with(new_access)
95
- old_access = @access
96
- @access = new_access
97
- response = yield(self)
98
- @access = old_access
99
- response
100
- end
101
-
102
- # Obtain a new access token using a refresh token.
103
- # @return [Access] The refreshed information.
104
- def refresh_access!
105
- response = auth_connection.post(
106
- "/api/v1/access_token",
107
- grant_type: "refresh_token",
108
- refresh_token: access.refresh_token
109
- )
110
- access.refreshed!(response.body)
111
- reset_connection!
112
- end
113
-
114
- # Dispose of an access or refresh token when you're done with it.
115
- # @param [Boolean] remove_refresh_token Whether or not to remove all
116
- # tokens associated with the user.
117
- def revoke_access!(remove_refresh_token = false)
118
- token_type = remove_refresh_token ? :refresh_token : :access_token
119
- token = access.send(token_type)
120
- @access = nil
121
- reset_connection!
122
- auth_connection.post(
123
- "/api/v1/revoke_token",
124
- token: token,
125
- token_type_hint: token_type
126
- )
127
- end
128
-
129
- private
130
-
131
- # @return [Faraday::RackBuilder] The middleware to use when creating the
132
- # connection.
133
- def middleware
134
- @middleware ||= Faraday::RackBuilder.new do |builder|
135
- builder.use Response::RaiseError
136
- builder.use Response::ParseJson
137
- builder.use Faraday::Request::Multipart
138
- builder.use Faraday::Request::UrlEncoded
139
- builder.adapter Faraday.default_adapter
140
- end
141
- end
142
-
143
- # @return [Hash] The minimum parameters to send with every request.
144
- def default_params
145
- @default_params ||= {api_type: "json"}
146
- end
147
-
148
- # @return [Hash] A hash of the headers used.
149
- def default_headers
150
- {
151
- "User-Agent" => @user_agent,
152
- "Authorization" => "bearer #{@access.access_token}"
153
- }
154
- end
155
-
156
- def reset_connection!
157
- @connection = nil
158
- @access
159
- end
160
-
161
- # @return [Faraday::Connection] A new or existing connection.
162
- def connection
163
- @connection ||= Faraday.new(
164
- @api_endpoint,
165
- headers: default_headers,
166
- builder: middleware
167
- )
168
- end
169
-
170
- # @return [Hash] A hash of the headers with basic auth.
171
- def auth_headers
172
- {
173
- "User-Agent" => @user_agent,
174
- "Authorization" => Faraday.basic_auth(@client_id, @secret)
175
- }
176
- end
177
-
178
- # @return [Faraday::Connection] A new or existing connection.
179
- def auth_connection
180
- @auth_connection ||= Faraday.new(
181
- @auth_endpoint,
182
- headers: auth_headers,
183
- builder: middleware
184
- )
185
- end
186
- end
187
- end
188
- end
@@ -1,20 +0,0 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "account" scope
5
- module Account
6
- # Edit some user preferences
7
- # @param [Hash] changed_prefs The preferences to override.
8
- # @return [Objects::Base] A Hashie-style container of the new prefs.
9
- # @see https://www.reddit.com/dev/api/oauth#PATCH_api_v1_me_prefs
10
- def edit_my_prefs(changed_prefs)
11
- response = connection.patch do |req|
12
- req.url "/api/v1/me/prefs"
13
- req.body = MultiJson.dump(changed_prefs)
14
- end
15
- Objects::Base.new(self, response.body)
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,22 +0,0 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "identity" scope
5
- module Identity
6
- # @return [Objects::User] The logged-in user.
7
- def me
8
- response = get("/api/v1/me")
9
- object_from_body(kind: "t2", data: response.body)
10
- end
11
-
12
- # @return [Objects::Base] Your current preferences.
13
- # @see https://www.reddit.com/dev/api/oauth#GET_api_v1_me_prefs
14
- def my_prefs
15
- response = get("/api/v1/me/prefs")
16
- # Basically an uneditable mashie.
17
- Objects::Base.new(self, response.body)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,27 +0,0 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that don't require any scope.
5
- module None
6
- # @return [Boolean] Whether a captcha is required for some API methods.
7
- def needs_captcha?
8
- get("/api/needs_captcha.json").body
9
- end
10
-
11
- # Create a new captcha identifier.
12
- # @return [String] The identifier.
13
- # @todo Maybe create some kind of set_captcha!(...) method for the
14
- # client to send automatically with the next response.
15
- def new_captcha
16
- post("/api/new_captcha").body[:json][:data][:iden]
17
- end
18
-
19
- # @param iden [String] The captcha identifier.
20
- # @return The url for the captcha image.
21
- def captcha_url(iden)
22
- "http://www.reddit.com/captcha/#{iden}.png"
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,33 +0,0 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "privatemessages" scope
5
- module Privatemessages
6
- # Return a listing of a user's private messages.
7
- #
8
- # @param category ["inbox", "unread", "sent"] The category of messages
9
- # to view.
10
- # @param mark [Boolean] Whether to remove the orangered from the
11
- # user's inbox.
12
- # @param params [Hash] A list of params to send with the request.
13
- # @option params [String] :after Return results after the given
14
- # fullname.
15
- # @option params [String] :before Return results before the given
16
- # fullname.
17
- # @option params [Integer] :count (0) The number of items already seen
18
- # in the listing.
19
- # @option params [1..100] :limit (25) The maximum number of things to
20
- # return.
21
- def my_messages(category = "inbox", mark = false, params = {})
22
- params[:mark] = mark
23
- request_object(:get, "/message/#{category}.json", params)
24
- end
25
-
26
- # Mark all messages as read.
27
- def read_all_messages
28
- post("/api/read_all_messages")
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,113 +0,0 @@
1
- module Redd
2
- module Clients
3
- class Base
4
- # Methods that require the "read" scope
5
- module Read
6
- # @param [Array<String>] fnames A list of fullnames.
7
- # @return [Objects::Listing<Objects::Thing>] A listing of things with
8
- # the fullname.
9
- def from_fullname(*fnames)
10
- names = fnames.join(",")
11
- request_object(:get, "/api/info", id: names)
12
- end
13
-
14
- # @param [String] url The url of the thing.
15
- # @return [Objects::Thing] The thing.
16
- def from_url(url)
17
- request_object(:get, "/api/info", url: url).first
18
- end
19
-
20
- # @param [String] name The username.
21
- # @return [Objects::User] The user.
22
- def user_from_name(name)
23
- request_object(:get, "/user/#{name}/about.json")
24
- end
25
-
26
- # @param [String] name The subreddit's display name.
27
- # @return [Objects::Subreddit] The subreddit if found.
28
- def subreddit_from_name(name)
29
- request_object(:get, "/r/#{name}/about.json")
30
- end
31
-
32
- # Fetch a list of multis belonging to the user.
33
- def my_multis
34
- multis = get("/api/multi/mine").body
35
- multis.map { |thing| object_from_body(thing) }
36
- end
37
-
38
- # Fetch an individual multi from its path.
39
- # @param [String] path The multi's path.
40
- # @return [Objects::LabeledMulti]
41
- def multi_from_path(path)
42
- without_slash = path.gsub(/^\//, "")
43
- request_object(:get, "/api/multi/" + without_slash)
44
- end
45
-
46
- # @!method get_hot(subreddit = nil, **params)
47
- # @!method get_new(subreddit = nil, **params)
48
- # @!method get_top(subreddit = nil, **params)
49
- # @!method get_controversial(subreddit = nil, **params)
50
- # @!method get_comments(subreddit = nil, **params)
51
- #
52
- # Get the appropriate listing.
53
- # @param subreddit [Objects::Subreddit, String] The subreddit to query.
54
- # @param params [Hash] A list of params to send with the request.
55
- # @option params [String] :after Return results after the given
56
- # fullname.
57
- # @option params [String :before Return results before the given
58
- # fullname.
59
- # @option params [Integer] :count The number of items already seen in
60
- # the listing.
61
- # @option params [1..100] :limit The maximum number of things to
62
- # return.
63
- # @option params [:hour, :day, :week, :month, :year, :all] :t The
64
- # time period to consider when sorting.
65
- #
66
- # @note The option :t only applies to the top and controversial sorts.
67
- # @return [Objects::Listing<Objects::Thing>]
68
- # @todo Move all listing methods into a helper?
69
- %w(hot new top controversial comments).each do |sort|
70
- define_method :"get_#{sort}" do |subreddit = nil, **params|
71
- srname = property(subreddit, :display_name) if subreddit
72
- path = "/#{sort}.json"
73
- path = path.prepend("/r/#{srname}") if subreddit
74
- request_object(:get, path, params)
75
- end
76
- end
77
-
78
- # Search.
79
- # @param query [String] The query string.
80
- # @param subreddit [Objects::Subreddit, String] The subreddit to query.
81
- # @param params [Hash] A list of params to send with the request.
82
- # @option params [String] :after Return results after the given
83
- # fullname.
84
- # @option params [String] :before Return results before the given
85
- # fullname.
86
- # @option params [Integer] :count The number of items already seen in
87
- # the listing.
88
- # @option params [1..100] :limit The maximum number of things to
89
- # return.
90
- # @option params [:cloudsearch, :lucene, :plain] :syntax The type of
91
- # syntax to use.
92
- # @option params [:relevance, :new, :hot, :top, :comments] :sort The
93
- # way to sort the results.
94
- # @option params [:hour, :day, :week, :month, :year, :all] :t The
95
- # time period to consider when sorting.
96
- #
97
- # @note The option :t only applies to the top and controversial sorts.
98
- # @return [Objects::Listing<Objects::Thing>]
99
- def search(query, subreddit = nil, **params)
100
- path = "/search.json"
101
- params[:q] = query
102
- if subreddit
103
- params[:restrict_sr] = true
104
- srname = property(subreddit, :display_name)
105
- path = path.prepend("/r/#{srname}")
106
- end
107
-
108
- request_object(:get, path, params)
109
- end
110
- end
111
- end
112
- end
113
- end