redd 0.7.8 → 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +34 -34
- data/.rspec +3 -3
- data/.rubocop.yml +5 -5
- data/.travis.yml +9 -9
- data/LICENSE.md +22 -22
- data/README.md +143 -143
- data/Rakefile +5 -5
- data/RedditKit.LICENSE.md +8 -8
- data/lib/redd.rb +50 -50
- data/lib/redd/access.rb +76 -76
- data/lib/redd/clients/base.rb +181 -181
- data/lib/redd/clients/base/account.rb +20 -20
- data/lib/redd/clients/base/identity.rb +22 -22
- data/lib/redd/clients/base/none.rb +27 -27
- data/lib/redd/clients/base/privatemessages.rb +33 -33
- data/lib/redd/clients/base/read.rb +113 -113
- data/lib/redd/clients/base/stream.rb +81 -81
- data/lib/redd/clients/base/submit.rb +19 -19
- data/lib/redd/clients/base/utilities.rb +104 -104
- data/lib/redd/clients/base/wikiread.rb +33 -33
- data/lib/redd/clients/installed.rb +56 -56
- data/lib/redd/clients/script.rb +41 -41
- data/lib/redd/clients/userless.rb +32 -32
- data/lib/redd/clients/web.rb +58 -58
- data/lib/redd/error.rb +151 -151
- data/lib/redd/objects/base.rb +39 -39
- data/lib/redd/objects/comment.rb +22 -22
- data/lib/redd/objects/labeled_multi.rb +13 -13
- data/lib/redd/objects/listing.rb +29 -29
- data/lib/redd/objects/more_comments.rb +11 -10
- data/lib/redd/objects/private_message.rb +28 -28
- data/lib/redd/objects/submission.rb +139 -139
- data/lib/redd/objects/subreddit.rb +330 -319
- data/lib/redd/objects/thing.rb +26 -26
- data/lib/redd/objects/thing/editable.rb +22 -22
- data/lib/redd/objects/thing/hideable.rb +18 -18
- data/lib/redd/objects/thing/inboxable.rb +25 -25
- data/lib/redd/objects/thing/messageable.rb +34 -34
- data/lib/redd/objects/thing/moderatable.rb +43 -43
- data/lib/redd/objects/thing/refreshable.rb +14 -14
- data/lib/redd/objects/thing/saveable.rb +21 -21
- data/lib/redd/objects/thing/votable.rb +33 -33
- data/lib/redd/objects/user.rb +52 -52
- data/lib/redd/objects/wiki_page.rb +15 -15
- data/lib/redd/rate_limit.rb +88 -88
- data/lib/redd/response/parse_json.rb +18 -18
- data/lib/redd/response/raise_error.rb +16 -16
- data/lib/redd/version.rb +4 -4
- data/redd.gemspec +31 -31
- data/spec/redd/objects/base_spec.rb +1 -1
- data/spec/redd/response/raise_error_spec.rb +11 -11
- data/spec/redd_spec.rb +5 -5
- data/spec/spec_helper.rb +71 -71
- metadata +21 -21
data/Rakefile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
5
|
-
task default: :spec
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
task default: :spec
|
data/RedditKit.LICENSE.md
CHANGED
@@ -1,9 +1,9 @@
|
|
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
|
-
|
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
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.
|
data/lib/redd.rb
CHANGED
@@ -1,50 +1,50 @@
|
|
1
|
-
require_relative "redd/version"
|
2
|
-
require_relative "redd/clients/installed"
|
3
|
-
require_relative "redd/clients/script"
|
4
|
-
require_relative "redd/clients/userless"
|
5
|
-
require_relative "redd/clients/web"
|
6
|
-
|
7
|
-
# The main Redd module.
|
8
|
-
module Redd
|
9
|
-
# @overload it(:installed, client_id, redirect_uri, **kwargs)
|
10
|
-
# Authorize a user via an installed app.
|
11
|
-
# @param [String] client_id The client ID provided by reddit.
|
12
|
-
# @param [String] redirect_uri The exact uri you provided to reddit.
|
13
|
-
# @param [Hash] kwargs The keyword arguments provided to the client.
|
14
|
-
# @return [Clients::Installed]
|
15
|
-
#
|
16
|
-
# @overload it(:script, client_id, secret, username, password, **kwargs)
|
17
|
-
# Authorize a user that you have full access to, i.e. a bot.
|
18
|
-
# @param [String] client_id The client ID provided by reddit.
|
19
|
-
# @param [String] secret The client secret provided by reddit.
|
20
|
-
# @param [String] username The username.
|
21
|
-
# @param [String] password The password of the user.
|
22
|
-
# @param [Hash] kwargs The keyword arguments provided to the client.
|
23
|
-
# @return [Clients::Script]
|
24
|
-
#
|
25
|
-
# @overload it(:userless, client_id, secret, **kwargs)
|
26
|
-
# Connect to reddit from a web-app or a script without a specific user.
|
27
|
-
# @param [String] client_id The client ID provided by reddit.
|
28
|
-
# @param [String] secret The client secret provided by reddit.
|
29
|
-
# @param [Hash] kwargs The keyword arguments provided to the client.
|
30
|
-
# @return [Clients::Userless]
|
31
|
-
#
|
32
|
-
# @overload it(:web, client_id, secret, redirect_uri, **kwargs)
|
33
|
-
# Authorize a user from a website.
|
34
|
-
# @param [String] client_id The client ID provided by reddit.
|
35
|
-
# @param [String] secret The client secret provided by reddit.
|
36
|
-
# @param [String] redirect_uri The exact uri you provided to reddit.
|
37
|
-
# @param [Hash] kwargs The keyword arguments provided to the client.
|
38
|
-
# @return [Clients::Web]
|
39
|
-
#
|
40
|
-
def self.it(type, *args, **kwargs)
|
41
|
-
types = {
|
42
|
-
installed: Clients::Installed,
|
43
|
-
script: Clients::Script,
|
44
|
-
userless: Clients::Userless,
|
45
|
-
web: Clients::Web
|
46
|
-
}
|
47
|
-
|
48
|
-
types[type].new(*args, **kwargs)
|
49
|
-
end
|
50
|
-
end
|
1
|
+
require_relative "redd/version"
|
2
|
+
require_relative "redd/clients/installed"
|
3
|
+
require_relative "redd/clients/script"
|
4
|
+
require_relative "redd/clients/userless"
|
5
|
+
require_relative "redd/clients/web"
|
6
|
+
|
7
|
+
# The main Redd module.
|
8
|
+
module Redd
|
9
|
+
# @overload it(:installed, client_id, redirect_uri, **kwargs)
|
10
|
+
# Authorize a user via an installed app.
|
11
|
+
# @param [String] client_id The client ID provided by reddit.
|
12
|
+
# @param [String] redirect_uri The exact uri you provided to reddit.
|
13
|
+
# @param [Hash] kwargs The keyword arguments provided to the client.
|
14
|
+
# @return [Clients::Installed]
|
15
|
+
#
|
16
|
+
# @overload it(:script, client_id, secret, username, password, **kwargs)
|
17
|
+
# Authorize a user that you have full access to, i.e. a bot.
|
18
|
+
# @param [String] client_id The client ID provided by reddit.
|
19
|
+
# @param [String] secret The client secret provided by reddit.
|
20
|
+
# @param [String] username The username.
|
21
|
+
# @param [String] password The password of the user.
|
22
|
+
# @param [Hash] kwargs The keyword arguments provided to the client.
|
23
|
+
# @return [Clients::Script]
|
24
|
+
#
|
25
|
+
# @overload it(:userless, client_id, secret, **kwargs)
|
26
|
+
# Connect to reddit from a web-app or a script without a specific user.
|
27
|
+
# @param [String] client_id The client ID provided by reddit.
|
28
|
+
# @param [String] secret The client secret provided by reddit.
|
29
|
+
# @param [Hash] kwargs The keyword arguments provided to the client.
|
30
|
+
# @return [Clients::Userless]
|
31
|
+
#
|
32
|
+
# @overload it(:web, client_id, secret, redirect_uri, **kwargs)
|
33
|
+
# Authorize a user from a website.
|
34
|
+
# @param [String] client_id The client ID provided by reddit.
|
35
|
+
# @param [String] secret The client secret provided by reddit.
|
36
|
+
# @param [String] redirect_uri The exact uri you provided to reddit.
|
37
|
+
# @param [Hash] kwargs The keyword arguments provided to the client.
|
38
|
+
# @return [Clients::Web]
|
39
|
+
#
|
40
|
+
def self.it(type, *args, **kwargs)
|
41
|
+
types = {
|
42
|
+
installed: Clients::Installed,
|
43
|
+
script: Clients::Script,
|
44
|
+
userless: Clients::Userless,
|
45
|
+
web: Clients::Web
|
46
|
+
}
|
47
|
+
|
48
|
+
types[type].new(*args, **kwargs)
|
49
|
+
end
|
50
|
+
end
|
data/lib/redd/access.rb
CHANGED
@@ -1,76 +1,76 @@
|
|
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
|
+
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
|
data/lib/redd/clients/base.rb
CHANGED
@@ -1,181 +1,181 @@
|
|
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
|
-
end
|
112
|
-
|
113
|
-
# Dispose of an access or refresh token when you're done with it.
|
114
|
-
# @param [Boolean] remove_refresh_token Whether or not to remove all
|
115
|
-
# tokens associated with the user.
|
116
|
-
def revoke_access!(remove_refresh_token = false)
|
117
|
-
token_type = remove_refresh_token ? :refresh_token : :access_token
|
118
|
-
token = access.send(token_type)
|
119
|
-
@access = nil
|
120
|
-
auth_connection.post(
|
121
|
-
"/api/v1/revoke_token",
|
122
|
-
token: token,
|
123
|
-
token_type_hint: token_type
|
124
|
-
)
|
125
|
-
end
|
126
|
-
|
127
|
-
private
|
128
|
-
|
129
|
-
# @return [Faraday::RackBuilder] The middleware to use when creating the
|
130
|
-
# connection.
|
131
|
-
def middleware
|
132
|
-
@middleware ||= Faraday::RackBuilder.new do |builder|
|
133
|
-
builder.use Response::RaiseError
|
134
|
-
builder.use Response::ParseJson
|
135
|
-
builder.use Faraday::Request::Multipart
|
136
|
-
builder.use Faraday::Request::UrlEncoded
|
137
|
-
builder.adapter Faraday.default_adapter
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
# @return [Hash] The minimum parameters to send with every request.
|
142
|
-
def default_params
|
143
|
-
@default_params ||= {api_type: "json"}
|
144
|
-
end
|
145
|
-
|
146
|
-
# @return [Hash] A hash of the headers used.
|
147
|
-
def default_headers
|
148
|
-
{
|
149
|
-
"User-Agent" => @user_agent,
|
150
|
-
"Authorization" => "bearer #{@access.access_token}"
|
151
|
-
}
|
152
|
-
end
|
153
|
-
|
154
|
-
# @return [Faraday::Connection] A new or existing connection.
|
155
|
-
def connection
|
156
|
-
@connection ||= Faraday.new(
|
157
|
-
@api_endpoint,
|
158
|
-
headers: default_headers,
|
159
|
-
builder: middleware
|
160
|
-
)
|
161
|
-
end
|
162
|
-
|
163
|
-
# @return [Hash] A hash of the headers with basic auth.
|
164
|
-
def auth_headers
|
165
|
-
{
|
166
|
-
"User-Agent" => @user_agent,
|
167
|
-
"Authorization" => Faraday.basic_auth(@client_id, @secret)
|
168
|
-
}
|
169
|
-
end
|
170
|
-
|
171
|
-
# @return [Faraday::Connection] A new or existing connection.
|
172
|
-
def auth_connection
|
173
|
-
@auth_connection ||= Faraday.new(
|
174
|
-
@auth_endpoint,
|
175
|
-
headers: auth_headers,
|
176
|
-
builder: middleware
|
177
|
-
)
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
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
|
+
end
|
112
|
+
|
113
|
+
# Dispose of an access or refresh token when you're done with it.
|
114
|
+
# @param [Boolean] remove_refresh_token Whether or not to remove all
|
115
|
+
# tokens associated with the user.
|
116
|
+
def revoke_access!(remove_refresh_token = false)
|
117
|
+
token_type = remove_refresh_token ? :refresh_token : :access_token
|
118
|
+
token = access.send(token_type)
|
119
|
+
@access = nil
|
120
|
+
auth_connection.post(
|
121
|
+
"/api/v1/revoke_token",
|
122
|
+
token: token,
|
123
|
+
token_type_hint: token_type
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
# @return [Faraday::RackBuilder] The middleware to use when creating the
|
130
|
+
# connection.
|
131
|
+
def middleware
|
132
|
+
@middleware ||= Faraday::RackBuilder.new do |builder|
|
133
|
+
builder.use Response::RaiseError
|
134
|
+
builder.use Response::ParseJson
|
135
|
+
builder.use Faraday::Request::Multipart
|
136
|
+
builder.use Faraday::Request::UrlEncoded
|
137
|
+
builder.adapter Faraday.default_adapter
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# @return [Hash] The minimum parameters to send with every request.
|
142
|
+
def default_params
|
143
|
+
@default_params ||= {api_type: "json"}
|
144
|
+
end
|
145
|
+
|
146
|
+
# @return [Hash] A hash of the headers used.
|
147
|
+
def default_headers
|
148
|
+
{
|
149
|
+
"User-Agent" => @user_agent,
|
150
|
+
"Authorization" => "bearer #{@access.access_token}"
|
151
|
+
}
|
152
|
+
end
|
153
|
+
|
154
|
+
# @return [Faraday::Connection] A new or existing connection.
|
155
|
+
def connection
|
156
|
+
@connection ||= Faraday.new(
|
157
|
+
@api_endpoint,
|
158
|
+
headers: default_headers,
|
159
|
+
builder: middleware
|
160
|
+
)
|
161
|
+
end
|
162
|
+
|
163
|
+
# @return [Hash] A hash of the headers with basic auth.
|
164
|
+
def auth_headers
|
165
|
+
{
|
166
|
+
"User-Agent" => @user_agent,
|
167
|
+
"Authorization" => Faraday.basic_auth(@client_id, @secret)
|
168
|
+
}
|
169
|
+
end
|
170
|
+
|
171
|
+
# @return [Faraday::Connection] A new or existing connection.
|
172
|
+
def auth_connection
|
173
|
+
@auth_connection ||= Faraday.new(
|
174
|
+
@auth_endpoint,
|
175
|
+
headers: auth_headers,
|
176
|
+
builder: middleware
|
177
|
+
)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|