redd 0.1.6 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d85e07d35f21832e40d2a17c6063d758b2051171
4
- data.tar.gz: d91adb52b18dd74a382369e5ff57384f27bc9cae
3
+ metadata.gz: cabe1bab129d4622fc26a19bea40fcbf27a676f4
4
+ data.tar.gz: 00a8d3c241c58c0ed449a1002e5e4ae3fb936093
5
5
  SHA512:
6
- metadata.gz: b1330d62998349feaa50b8a9266ba295145dfe0d633b290f77ecf72a5eadaba36733467d2e9c5138bc89fa9f7043b6616d28408384ce67832ca43db3500750d2
7
- data.tar.gz: 64b1c5d48b6e8c34e4fa6eb0b28465766240955966d4ef5e4235276dce8d791f0a78cbc83109b8565e4a430e082394ee83902063e2ebb3a895af8489348e78db
6
+ metadata.gz: 1275d0cba8f74d11d17b0bdb2644cfb4fcfa982fcc8597e665c505bcfc2252f990a6ecdbdc26d4f4671aef012353997428fac1845319b0ecc703bf673e364354
7
+ data.tar.gz: 5f21e8de382d108263b1fda22fd833e5862d0d214c9e54ab64f0b153a560185685326688571304120aeeb2e0484ca8627ce3d8505d219789dc0de6c7943827a5
@@ -41,14 +41,6 @@ module Redd
41
41
  # @return [String] The returned modhash used when making requests.
42
42
  attr_reader :modhash
43
43
 
44
- # @!attribute [rw] auth_endpoint
45
- # @return [String] The site to connect to authenticate with.
46
- attr_accessor :auth_endpoint
47
-
48
- def self.new_from_credentials(username, password, options = {})
49
- Redd::Client::Unauthenticated.new.login(username, password, options)
50
- end
51
-
52
44
  # Set up an authenticated connection to reddit.
53
45
  #
54
46
  # @param [String] cookie The cookie to use when sending a request.
@@ -66,8 +58,7 @@ module Redd
66
58
 
67
59
  @rate_limit = options[:rate_limit] || Redd::RateLimit.new
68
60
  @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
69
- @api_endpoint = options[:api_endpoint] || "http://www.reddit.com/"
70
- @auth_endpoint = options[:auth_endpoint] || "https://ssl.reddit.com/"
61
+ @api_endpoint = options[:api_endpoint] || "https://www.reddit.com/"
71
62
  end
72
63
 
73
64
  private
@@ -3,10 +3,7 @@ module Redd
3
3
  class Authenticated
4
4
  module Account
5
5
  def me
6
- meth = :get
7
- path = "/api/me.json"
8
-
9
- object_from_response(meth, path)
6
+ object_from_response :get, "/api/me.json"
10
7
  end
11
8
  end
12
9
  end
@@ -4,6 +4,68 @@ module Redd
4
4
  module Client
5
5
  # The client to connect using OAuth2.
6
6
  class OAuth2 < Redd::Client::Authenticated
7
+ require "redd/client/oauth2/authorization"
8
+ require "redd/client/oauth2/identity"
9
+
10
+ include Redd::Client::OAuth2::Authorization
11
+ include Redd::Client::OAuth2::Identity
12
+
13
+ # @!attribute [r] api_endpoint
14
+ # @return [String] The site to make oauth requests with.
15
+ attr_accessor :api_endpoint
16
+
17
+ # @!attribute [r] auth_endpoint
18
+ # @return [String] The site to connect to authenticate with.
19
+ attr_accessor :auth_endpoint
20
+
21
+ # @!attribute [r] client_id
22
+ # @return [String] The client_id of the oauth application.
23
+ attr_reader :client_id
24
+
25
+ # @!attribute [r] redirect_uri
26
+ # @return [String] The exact redirect_uri of the oauth application.
27
+ attr_reader :redirect_uri
28
+
29
+ # @!attribute [rw] access_token
30
+ # @return [String] The access token used to make requests.
31
+ attr_accessor :access_token
32
+
33
+ def initialize(client_id, secret, redirect_uri, options = {})
34
+ @client_id = client_id
35
+ @secret = secret
36
+ @redirect_uri = redirect_uri
37
+
38
+ @rate_limit = options[:rate_limit] || Redd::RateLimit.new(1)
39
+ @api_endpoint = options[:api_endpoint] || "https://oauth.reddit.com/"
40
+ @auth_endpoint = options[:auth_endpoint] || "https://ssl.reddit.com/"
41
+ end
42
+
43
+ private
44
+
45
+ def connection(access_token = @access_token)
46
+ @connection ||= Faraday.new(url: api_endpoint) do |faraday|
47
+ faraday.use Faraday::Request::UrlEncoded
48
+ faraday.use Redd::Response::RaiseError
49
+ faraday.use Redd::Response::ParseJson
50
+ faraday.adapter Faraday.default_adapter
51
+
52
+ faraday.headers["Authorization"] = "bearer #{access_token}"
53
+ end
54
+ end
55
+
56
+ def auth_connection
57
+ @auth_connection ||= Faraday.new(url: auth_endpoint) do |faraday|
58
+ faraday.use Faraday::Request::UrlEncoded
59
+ faraday.use Redd::Response::ParseJson
60
+ faraday.basic_auth(@client_id, @secret)
61
+ faraday.adapter Faraday.default_adapter
62
+ end
63
+ end
64
+
65
+ # @return [Hash] The headers that are sent with every request.
66
+ def headers
67
+ @headers ||= {}
68
+ end
7
69
  end
8
70
  end
9
71
  end
@@ -0,0 +1,31 @@
1
+ module Redd
2
+ module Client
3
+ class OAuth2
4
+ module Authorization
5
+ def auth_url(scope = ["identity"], duration = "temporary", state = "x")
6
+ path = "https://ssl.reddit.com/api/v1/authorize"
7
+ query = {
8
+ client_id: @client_id,
9
+ redirect_uri: @redirect_uri,
10
+ response_type: "code",
11
+ state: state,
12
+ scope: scope.join(","),
13
+ duration: duration
14
+ }
15
+ string_query = query.map { |key, value| "#{key}=#{value}" }.join("&")
16
+
17
+ "#{path}?#{string_query}"
18
+ end
19
+
20
+ def request_access_token(code, set_token = true)
21
+ response = auth_connection.post "/api/v1/access_token",
22
+ grant_type: "authorization_code", code: code,
23
+ redirect_uri: @redirect_uri
24
+
25
+ @access_token = response.body[:access_token] if set_token
26
+ response.body
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Redd
2
+ module Client
3
+ class OAuth2
4
+ module Identity
5
+ def me
6
+ response = get "/api/v1/me.json"
7
+ object_from_body kind: "t2", data: response
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -52,7 +52,7 @@ module Redd
52
52
  def initialize(options = {})
53
53
  @rate_limit = options[:rate_limit] || Redd::RateLimit.new
54
54
  @user_agent = options[:user_agent] || "Redd/Ruby, v#{Redd::VERSION}"
55
- @api_endpoint = options[:api_endpoint] || "http://www.reddit.com/"
55
+ @api_endpoint = options[:api_endpoint] || "https://www.reddit.com/"
56
56
  end
57
57
 
58
58
  private
data/lib/redd/error.rb CHANGED
@@ -20,41 +20,41 @@ module Redd
20
20
  when 200
21
21
  case body
22
22
  when /WRONG_PASSWORD/i
23
- InvalidCredentials
23
+ Redd::Error::InvalidCredentials
24
24
  when /BAD_CAPTCHA/i
25
- InvalidCaptcha
25
+ Redd::Error::InvalidCaptcha
26
26
  when /RATELIMIT/i
27
- RateLimited
27
+ Redd::Error::RateLimited
28
28
  when /BAD_CSS_NAME/i
29
- InvalidClassName
29
+ Redd::Error::InvalidClassName
30
30
  when /TOO_OLD/i
31
- Archived
31
+ Redd::Error::Archived
32
32
  when /TOO_MUCH_FLAIR_CSS/i
33
- TooManyClassNames
33
+ Redd::Error::TooManyClassNames
34
34
  when /USER_REQUIRED/i
35
- AuthenticationRequired
35
+ Redd::Error::AuthenticationRequired
36
36
  end
37
37
  when 400
38
- BadRequest
38
+ Redd::Error::BadRequest
39
39
  when 403
40
40
  case body
41
41
  when /USER_REQUIRED/i
42
- AuthenticationRequired
42
+ Redd::Error::AuthenticationRequired
43
43
  else
44
- PermissionDenied
44
+ Redd::Error::PermissionDenied
45
45
  end
46
46
  when 404
47
- NotFound
47
+ Redd::Error::NotFound
48
48
  when 409
49
- Conflict
49
+ Redd::Error::Conflict
50
50
  when 500
51
- InternalServerError
51
+ Redd::Error::InternalServerError
52
52
  when 502
53
- BadGateway
53
+ Redd::Error::BadGateway
54
54
  when 503
55
- ServiceUnavailable
55
+ Redd::Error::ServiceUnavailable
56
56
  when 504
57
- TimedOut
57
+ Redd::Error::TimedOut
58
58
  end
59
59
  end
60
60
 
data/lib/redd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # The main Redd module.
2
2
  module Redd
3
3
  # The semantic version number for Redd.
4
- VERSION = "0.1.6"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
@@ -184,6 +184,8 @@ files:
184
184
  - lib/redd/client/authenticated/users.rb
185
185
  - lib/redd/client/authenticated/wiki.rb
186
186
  - lib/redd/client/oauth2.rb
187
+ - lib/redd/client/oauth2/authorization.rb
188
+ - lib/redd/client/oauth2/identity.rb
187
189
  - lib/redd/client/unauthenticated.rb
188
190
  - lib/redd/client/unauthenticated/account.rb
189
191
  - lib/redd/client/unauthenticated/captcha.rb