Class: Qihu::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/qihu/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, token = {}, redirect_uri = 'oob', site = 'https://openapi.360.cn') ⇒ Auth

Returns a new instance of Auth.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/qihu/auth.rb', line 8

def initialize(client_id, client_secret, token={}, redirect_uri='oob', site='https://openapi.360.cn')
  @redirect_uri = redirect_uri
  @oauth2 = OAuth2::Client.new(client_id, client_secret,
    :site => site,
    :authorize_url => '/oauth2/authorize',
    :token_url => '/oauth2/access_token',
    :ssl => {:verify => false},
  )

  if token or !token.empty?
    @token = OAuth2::AccessToken.from_hash(@oauth2, token)
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/qihu/auth.rb', line 5

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



5
6
7
# File 'lib/qihu/auth.rb', line 5

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



5
6
7
# File 'lib/qihu/auth.rb', line 5

def expires_in
  @expires_in
end

#oauth2Object (readonly)

Returns the value of attribute oauth2.



5
6
7
# File 'lib/qihu/auth.rb', line 5

def oauth2
  @oauth2
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



6
7
8
# File 'lib/qihu/auth.rb', line 6

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



5
6
7
# File 'lib/qihu/auth.rb', line 5

def refresh_token
  @refresh_token
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/qihu/auth.rb', line 6

def token
  @token
end

Instance Method Details

#authorize_url(options = {}) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/qihu/auth.rb', line 22

def authorize_url(options={})
  @redirect_uri = options[:redirect_uri] if options[:redirect_uri]
  scope = options[:scope] ? options[:scope] : 'basic'
  display = options[:display] ? options[:display] : 'default'

  @oauth2.auth_code.authorize_url(:redirect_uri => @redirect_uri, :scope => scope, :display => display)
end

#get_token(code, redirect_uri = '') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/qihu/auth.rb', line 30

def get_token(code, redirect_uri='')
  @redirect_uri = redirect_uri unless redirect_uri.empty?
  @token = @oauth2.auth_code.get_token(code, :redirect_uri => @redirect_uri)

  @refresh_token = @token.refresh_token
  @expires_at = @token.expires_at
  @expires_in = @token.expires_in
  @access_token = @token.token

  return self
end

#get_token_from_hash(token = {}) ⇒ Object



42
43
44
45
# File 'lib/qihu/auth.rb', line 42

def get_token_from_hash(token={})
  @token = OAuth2::AccessToken.from_hash(@oauth2, token)
  return self
end