redd 0.6.5 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +34 -33
- data/.rspec +3 -4
- data/.rubocop.yml +5 -5
- data/.travis.yml +9 -7
- data/{LICENSE.md → LICENSE.txt} +22 -22
- data/README.md +126 -241
- data/Rakefile +5 -6
- data/{RedditKit.LICENSE.md → RedditKit.LICENSE.txt} +13 -13
- data/lib/redd.rb +50 -4
- data/lib/redd/access.rb +76 -0
- data/lib/redd/clients/base.rb +178 -0
- data/lib/redd/clients/base/account.rb +20 -0
- data/lib/redd/clients/base/identity.rb +22 -0
- data/lib/redd/clients/base/none.rb +27 -0
- data/lib/redd/clients/base/privatemessages.rb +28 -0
- data/lib/redd/clients/base/read.rb +66 -0
- data/lib/redd/clients/base/stream.rb +74 -0
- data/lib/redd/clients/base/submit.rb +54 -0
- data/lib/redd/clients/base/utilities.rb +80 -0
- data/lib/redd/clients/base/wikiread.rb +33 -0
- data/lib/redd/clients/installed.rb +55 -0
- data/lib/redd/clients/script.rb +37 -0
- data/lib/redd/clients/userless.rb +31 -0
- data/lib/redd/clients/web.rb +57 -0
- data/lib/redd/error.rb +138 -153
- data/lib/redd/objects/base.rb +36 -0
- data/lib/redd/objects/comment.rb +22 -0
- data/lib/redd/objects/listing.rb +29 -0
- data/lib/redd/objects/more_comments.rb +10 -0
- data/lib/redd/objects/private_message.rb +18 -0
- data/lib/redd/objects/submission.rb +72 -0
- data/lib/redd/objects/subreddit.rb +152 -0
- data/lib/redd/objects/thing.rb +33 -0
- data/lib/redd/objects/thing/editable.rb +22 -0
- data/lib/redd/objects/thing/hideable.rb +18 -0
- data/lib/redd/objects/thing/inboxable.rb +25 -0
- data/lib/redd/objects/thing/messageable.rb +34 -0
- data/lib/redd/objects/thing/moderatable.rb +43 -0
- data/lib/redd/objects/thing/refreshable.rb +14 -0
- data/lib/redd/objects/thing/saveable.rb +21 -0
- data/lib/redd/objects/thing/votable.rb +33 -0
- data/lib/redd/objects/user.rb +52 -0
- data/lib/redd/objects/wiki_page.rb +15 -0
- data/lib/redd/rate_limit.rb +50 -49
- data/lib/redd/response/parse_json.rb +17 -33
- data/lib/redd/response/raise_error.rb +16 -25
- data/lib/redd/version.rb +4 -5
- data/redd.gemspec +30 -31
- data/spec/redd/objects/base_spec.rb +1 -0
- data/spec/redd/rate_limit_spec.rb +29 -29
- data/spec/redd/response/parse_json_spec.rb +12 -0
- data/spec/redd/response/raise_error_spec.rb +11 -0
- data/spec/redd_spec.rb +7 -5
- data/spec/spec_helper.rb +69 -50
- metadata +73 -136
- data/.yardopts +0 -1
- data/lib/redd/base.rb +0 -56
- data/lib/redd/client/authenticated.rb +0 -83
- data/lib/redd/client/authenticated/account.rb +0 -13
- data/lib/redd/client/authenticated/apps.rb +0 -13
- data/lib/redd/client/authenticated/flair.rb +0 -71
- data/lib/redd/client/authenticated/gold.rb +0 -13
- data/lib/redd/client/authenticated/links_comments.rb +0 -189
- data/lib/redd/client/authenticated/live.rb +0 -13
- data/lib/redd/client/authenticated/moderation.rb +0 -126
- data/lib/redd/client/authenticated/multis.rb +0 -9
- data/lib/redd/client/authenticated/private_messages.rb +0 -73
- data/lib/redd/client/authenticated/subreddits.rb +0 -172
- data/lib/redd/client/authenticated/users.rb +0 -9
- data/lib/redd/client/authenticated/wiki.rb +0 -9
- data/lib/redd/client/oauth2.rb +0 -71
- data/lib/redd/client/oauth2/authorization.rb +0 -108
- data/lib/redd/client/oauth2/identity.rb +0 -16
- data/lib/redd/client/oauth2_script.rb +0 -24
- data/lib/redd/client/oauth2_script/authorization.rb +0 -21
- data/lib/redd/client/unauthenticated.rb +0 -118
- data/lib/redd/client/unauthenticated/account.rb +0 -30
- data/lib/redd/client/unauthenticated/captcha.rb +0 -27
- data/lib/redd/client/unauthenticated/links_comments.rb +0 -60
- data/lib/redd/client/unauthenticated/listing.rb +0 -65
- data/lib/redd/client/unauthenticated/live.rb +0 -9
- data/lib/redd/client/unauthenticated/moderation.rb +0 -26
- data/lib/redd/client/unauthenticated/subreddits.rb +0 -49
- data/lib/redd/client/unauthenticated/users.rb +0 -67
- data/lib/redd/client/unauthenticated/utilities.rb +0 -109
- data/lib/redd/client/unauthenticated/wiki.rb +0 -33
- data/lib/redd/oauth2_access.rb +0 -70
- data/lib/redd/object/comment.rb +0 -74
- data/lib/redd/object/listing.rb +0 -29
- data/lib/redd/object/more_comments.rb +0 -14
- data/lib/redd/object/private_message.rb +0 -35
- data/lib/redd/object/submission.rb +0 -94
- data/lib/redd/object/subreddit.rb +0 -74
- data/lib/redd/object/user.rb +0 -34
- data/lib/redd/object/wiki_page.rb +0 -27
- data/lib/redd/thing.rb +0 -27
- data/lib/redd/thing/commentable.rb +0 -27
- data/lib/redd/thing/editable.rb +0 -16
- data/lib/redd/thing/hideable.rb +0 -16
- data/lib/redd/thing/inboxable.rb +0 -20
- data/lib/redd/thing/messageable.rb +0 -12
- data/lib/redd/thing/moderatable.rb +0 -32
- data/lib/redd/thing/reportable.rb +0 -12
- data/lib/redd/thing/saveable.rb +0 -16
- data/lib/redd/thing/voteable.rb +0 -22
- data/spec/README.md +0 -18
- data/spec/redd/base_spec.rb +0 -36
- data/spec/redd/client/authenticated/account_spec.rb +0 -5
- data/spec/redd/client/authenticated/apps_spec.rb +0 -2
- data/spec/redd/client/authenticated/flair_spec.rb +0 -26
- data/spec/redd/client/authenticated/gold_spec.rb +0 -2
- data/spec/redd/client/authenticated/links_comments_spec.rb +0 -231
- data/spec/redd/client/authenticated/live_spec.rb +0 -2
- data/spec/redd/client/unauthenticated/account_spec.rb +0 -15
- data/spec/redd/client/unauthenticated/captcha_spec.rb +0 -23
- data/spec/redd/client/unauthenticated/links_comments_spec.rb +0 -28
- data/spec/redd/client/unauthenticated/listing_spec.rb +0 -23
- data/spec/redd/client/unauthenticated/live_spec.rb +0 -2
- data/spec/redd/client/unauthenticated/moderation_spec.rb +0 -14
- data/spec/redd/client/unauthenticated/subreddits_spec.rb +0 -35
- data/spec/redd/client/unauthenticated/users_spec.rb +0 -34
- data/spec/redd/client/unauthenticated/wiki_spec.rb +0 -18
- data/spec/redd/oauth2_access_spec.rb +0 -83
- data/spec/redd/thing_spec.rb +0 -22
data/Rakefile
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
require "rspec/core/rake_task"
|
3
|
-
|
4
|
-
RSpec::Core::RakeTask.new
|
5
|
-
|
6
|
-
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
|
@@ -1,13 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
---
|
4
|
-
|
5
|
-
Copyright (c) 2013 Sam Symons
|
6
|
-
|
7
|
-
MIT License
|
8
|
-
|
9
|
-
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:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
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
|
+
error.rb would have been impossible if it weren't for @samsymons :)
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
Copyright (c) 2013 Sam Symons
|
6
|
+
|
7
|
+
MIT License
|
8
|
+
|
9
|
+
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:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
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,4 +1,50 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
+
@client = types[type].new(*args, **kwargs)
|
49
|
+
end
|
50
|
+
end
|
data/lib/redd/access.rb
ADDED
@@ -0,0 +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
|
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
|
@@ -0,0 +1,178 @@
|
|
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] access The access to use.
|
93
|
+
# @yield The client with the given access.
|
94
|
+
def with(access)
|
95
|
+
new_instance = dup
|
96
|
+
new_instance.access = access
|
97
|
+
yield new_instance
|
98
|
+
end
|
99
|
+
|
100
|
+
# Obtain a new access token using a refresh token.
|
101
|
+
# @return [Access] The refreshed information.
|
102
|
+
def refresh_access!
|
103
|
+
response = auth_connection.post(
|
104
|
+
"/api/v1/access_token",
|
105
|
+
grant_type: "refresh_token",
|
106
|
+
refresh_token: access.refresh_token
|
107
|
+
)
|
108
|
+
access.refreshed!(response.body)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Dispose of an access or refresh token when you're done with it.
|
112
|
+
# @param [Boolean] remove_refresh_token Whether or not to remove all
|
113
|
+
# tokens associated with the user.
|
114
|
+
def revoke_access!(remove_refresh_token = false)
|
115
|
+
token_type = remove_refresh_token ? :refresh_token : :access_token
|
116
|
+
token = access.send(token_type)
|
117
|
+
@access = nil
|
118
|
+
auth_connection.post(
|
119
|
+
"/api/v1/revoke_token",
|
120
|
+
token: token,
|
121
|
+
token_type_hint: token_type
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
# @return [Faraday::RackBuilder] The middleware to use when creating the
|
128
|
+
# connection.
|
129
|
+
def middleware
|
130
|
+
@middleware ||= Faraday::RackBuilder.new do |builder|
|
131
|
+
builder.use Response::RaiseError
|
132
|
+
builder.use Response::ParseJson
|
133
|
+
builder.use Faraday::Request::UrlEncoded
|
134
|
+
builder.adapter Faraday.default_adapter
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# @return [Hash] The minimum parameters to send with every request.
|
139
|
+
def default_params
|
140
|
+
@default_params ||= {api_type: "json"}
|
141
|
+
end
|
142
|
+
|
143
|
+
# @return [Hash] A hash of the headers used.
|
144
|
+
def default_headers
|
145
|
+
{
|
146
|
+
"User-Agent" => @user_agent,
|
147
|
+
"Authorization" => "bearer #{@access.access_token}"
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
# @return [Faraday::Connection] A new or existing connection.
|
152
|
+
def connection
|
153
|
+
@connection ||= Faraday.new(
|
154
|
+
@api_endpoint,
|
155
|
+
headers: default_headers,
|
156
|
+
builder: middleware
|
157
|
+
)
|
158
|
+
end
|
159
|
+
|
160
|
+
# @return [Hash] A hash of the headers with basic auth.
|
161
|
+
def auth_headers
|
162
|
+
{
|
163
|
+
"User-Agent" => @user_agent,
|
164
|
+
"Authorization" => Faraday.basic_auth(@client_id, @secret)
|
165
|
+
}
|
166
|
+
end
|
167
|
+
|
168
|
+
# @return [Faraday::Connection] A new or existing connection.
|
169
|
+
def auth_connection
|
170
|
+
@auth_connection ||= Faraday.new(
|
171
|
+
@auth_endpoint,
|
172
|
+
headers: auth_headers,
|
173
|
+
builder: middleware
|
174
|
+
)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,20 @@
|
|
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
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -0,0 +1,27 @@
|
|
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/#{identifier}.png"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
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
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|