Class: DiandianOAuth::Client

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/diandian_oauth/client.rb,
lib/diandian_oauth/client/callbacks.rb

Defined Under Namespace

Modules: Callbacks

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Constructor Details

#initialize(client_id, client_secret, options = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
# File 'lib/diandian_oauth/client.rb', line 7

def initialize client_id, client_secret, options={}
  @api = options[:api] || DiandianOAuth::API.new
  @redirect_uri = options[:redirect_uri]
  @client = OAuth2::Client.new client_id, client_secret, {
      :authorize_url => @api.authorize_url,
      :token_url => @api.token_url
    }
  self.config_client_middleware(@client)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/diandian_oauth/client.rb', line 68

def method_missing(meth, *args, &block)
    force = meth.to_s =~ /!$/
    meth = $` if force
	interface = @api.interface meth.to_sym
	if interface
      access_token = self.access_token
      raise 'access_token is required' unless access_token
      response = if force
        begin
          token_expired = false
          response = interface.apply access_token, args[0], &block
          response.validate!
        rescue TokenExpiredError => e
          if DiandianOAuth.logger.debug?
            DiandianOAuth.logger.debug("token '#{access_token.inspect}' expired")
          end
          new_access_token = access_token.refresh!
          self.token_refreshed new_access_token
          if DiandianOAuth.logger.debug?
            DiandianOAuth.logger.debug("refreshed '#{access_token.inspect}' with '#{new_access_token}'")
          end
          self.access_token = access_token = new_access_token
          token_expired = true
        end while token_expired
        response
      else
		  interface.apply access_token, args[0], &block
      end # force
	else
		super
	end
end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



6
7
8
# File 'lib/diandian_oauth/client.rb', line 6

def api
  @api
end

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/diandian_oauth/client.rb', line 6

def client
  @client
end

Instance Method Details

#access_token(code_or_hash = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/diandian_oauth/client.rb', line 44

def access_token code_or_hash=nil
  return @access_token if @access_token
			return nil unless code_or_hash
  if code_or_hash.is_a? Hash
    @access_token = OAuth2::AccessToken.from_hash(@client, code_or_hash)
  else
    begin
      DiandianOAuth.logger.warn('redirect_url is required for authorization_code grant type') unless @redirect_uri
      @access_token = @client.auth_code.get_token(code_or_hash, {:redirect_uri => @redirect_uri})
      #@access_token = @client.get_token(
      #    :client_id => @client.id,
      #    :client_secret => @client.secret,
      #    :grant_type => 'authorization_code',
      #    :code => code_or_hash,
      #    :redirect_uri => @redirect_uri
      #)
    rescue OAuth2::Error => e
      DiandianOAuth.logger.error e.to_s
      raise e
    end
  end
  @access_token
end

#access_token=(hash) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/diandian_oauth/client.rb', line 36

def access_token= hash
  @access_token = case hash
    when Hash then OAuth2::AccessToken.from_hash(@client, hash)
    when OAuth2::AccessToken then hash
    else
      raise 'illegal argument hash'
  end
end

#authorize_url(response_type = 'code', scope = []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/diandian_oauth/client.rb', line 25

def authorize_url response_type='code', scope=[]
  if response_type.is_a? Array
    scope = response_type
    response_type = 'code'
  end
  @client.authorize_url(
      :client_id => @client.id,
      :response_type => response_type,
      :scope => scope.join(',')
  )
end

#config_client_middleware(client) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/diandian_oauth/client.rb', line 17

def config_client_middleware client
  client.options[:connection_build] = Proc.new do |builder|
    builder.request :multipart
    builder.request :url_encoded
    builder.adapter :net_http
  end
end