redd 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea1e887784c57e6c3bef623557618c0b2cf85b6f
4
- data.tar.gz: a0c3e6e653857d9f10d5915d433866b166d436da
3
+ metadata.gz: 4800088050748f4a32d520ba72815bab9e2d8f1b
4
+ data.tar.gz: 55e9d64b88930bbf42480efa232f7fd65d585f79
5
5
  SHA512:
6
- metadata.gz: 7cd3dc27fe73b93382f5a2cb651d391fc6dd6fc43e2257871419c77703d1b070c7fd7e561e29724f54c7736d7340860866ee0b05b0c10f39f666cb32a7ed2f3e
7
- data.tar.gz: 9ae7e3b594a3f4db301623737e63f9771a71f3c9663ccd2a9ccbc9e1965f95a2dcf0c5c5d989432ac7614284b08d74f4ea74cb96068db39a0d28f45161be4915
6
+ metadata.gz: e3b67ad7805af912e63c42754312a3cf7f1d9a7cfcc6199d74077b182ce2b9d750052773535b99e3752b422f741d445cc696d212fbdd7b39f66730102877d421
7
+ data.tar.gz: 0b100bc3f6986435e10160c14c72e3cf8b0df8c8c8222a2c24902ee3a97ec90d4c050532a4cb19c3b8c69b03dfbfbe2c7ebf0a3ebf5e323a7dd47ec23ac80ab7
@@ -22,13 +22,9 @@ module Redd
22
22
  # @return [String] The exact redirect_uri of the oauth application.
23
23
  attr_reader :redirect_uri
24
24
 
25
- # @!attribute [rw] access_token
26
- # @return [String] The access token used to make requests.
27
- attr_accessor :access_token
28
-
29
- # @!attribute [rw] refresh_token
30
- # @return [String] The token used to refresh the access token.
31
- attr_accessor :refresh_token
25
+ # @!attribute [rw] access
26
+ # @return [String] The access info used to make requests.
27
+ attr_accessor :access
32
28
 
33
29
  def initialize(client_id, secret, redirect_uri, options = {})
34
30
  @client_id = client_id
@@ -42,7 +38,7 @@ module Redd
42
38
 
43
39
  private
44
40
 
45
- def connection(access_token = @access_token)
41
+ def connection(access_token = @access.access_token)
46
42
  @connection ||= Faraday.new(url: api_endpoint) do |faraday|
47
43
  faraday.use Faraday::Request::UrlEncoded
48
44
  faraday.use Redd::Response::RaiseError
@@ -1,3 +1,5 @@
1
+ require "redd/oauth2_access"
2
+
1
3
  module Redd
2
4
  module Client
3
5
  class OAuth2
@@ -17,26 +19,29 @@ module Redd
17
19
  "#{path}?#{string_query}"
18
20
  end
19
21
 
20
- def request_access_token(code, set_token = true)
22
+ def request_access_token(code, set_access = true)
21
23
  response = auth_connection.post "/api/v1/access_token",
22
24
  grant_type: "authorization_code", code: code,
23
25
  redirect_uri: @redirect_uri
24
26
 
25
- body = response.body
26
- @access_token = body[:access_token] if set_token
27
- @refresh_token = body[:refresh_token] if body[:refresh_token]
28
- body
27
+ access = Redd::OAuth2Access.new(response.body)
28
+ @access = access if set_access
29
+ access
29
30
  end
30
31
 
31
- def refresh_access_token(
32
- refresh_token = @refresh_token, set_token = true
33
- )
32
+ def refresh_access_token(access = nil, set_access = true)
33
+ refresh_token = extract_attribute(access, :refresh_token)
34
34
  response = auth_connection.post "/api/v1/access_token",
35
35
  grant_type: "refresh_token", refresh_token: refresh_token
36
36
 
37
- body = response.body
38
- @access_token = body[:access_token] if set_token
39
- body
37
+ case access
38
+ when Redd::OAuth2Access
39
+ access.refresh(response.body)
40
+ when ::String
41
+ new_access = Redd::OAuth2Access.new(response.body)
42
+ @access = new_access if set_access
43
+ new_access
44
+ end
40
45
  end
41
46
  end
42
47
  end
@@ -0,0 +1,47 @@
1
+ require "multi_json"
2
+
3
+ module Redd
4
+ class OAuth2Access
5
+ attr_reader :access_token
6
+
7
+ attr_reader :refresh_token
8
+
9
+ attr_reader :scope
10
+
11
+ attr_reader :duration
12
+
13
+ attr_reader :expires_at
14
+
15
+ def initialize(response)
16
+ @access_token = response[:access_token]
17
+ @refresh_token = response[:refresh_token]
18
+ @scope = response[:scope].split(",").map { |s| s.to_sym }
19
+ @duration = @refresh_token ? :permanent : :temporary
20
+ @expires_at = Time.now + response[:expires_in]
21
+ end
22
+
23
+ def refresh(response)
24
+ @access_token = response[:access_token]
25
+ @expires_at = Time.now + response[:expires_in]
26
+ self
27
+ end
28
+
29
+ def expired?
30
+ Time.now > @expires_at
31
+ end
32
+
33
+ def to_json
34
+ MultiJson.dump(
35
+ access_token: @access_token,
36
+ refresh_token: @refresh_token,
37
+ scope: @scope.join(","),
38
+ expires_in: (@expires_at - Time.now).to_i
39
+ )
40
+ end
41
+
42
+ def self.from_json(json)
43
+ hash = MultiJson.load(json, symbolize_keys: true)
44
+ new(hash)
45
+ end
46
+ end
47
+ end
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.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-10 00:00:00.000000000 Z
11
+ date: 2014-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -197,6 +197,7 @@ files:
197
197
  - lib/redd/client/unauthenticated/utilities.rb
198
198
  - lib/redd/client/unauthenticated/wiki.rb
199
199
  - lib/redd/error.rb
200
+ - lib/redd/oauth2_access.rb
200
201
  - lib/redd/object/comment.rb
201
202
  - lib/redd/object/listing.rb
202
203
  - lib/redd/object/more_comments.rb