Class: OmniAuth::Strategies::Douban

Inherits:
OAuth
  • Object
show all
Defined in:
lib/omniauth/strategies/oauth/douban.rb

Overview

Authenticate to Douban via OAuth and retrieve basic user information. Usage: use OmniAuth::Strategies::Douban, 'APIKey', 'APIKeySecret'

Instance Attribute Summary

Attributes inherited from OAuth

#consumer_key, #consumer_options, #consumer_secret, #name

Instance Method Summary collapse

Methods inherited from OAuth

#callback_phase, #consumer, #request_phase, #unique_id

Constructor Details

#initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block) ⇒ Douban

Returns a new instance of Douban.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/omniauth/strategies/oauth/douban.rb', line 12

def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
  # Although in OAuth spec the :realm parameter is optional,
  # it is required for Douban.
  client_options = {
    :access_token_path => '/service/auth/access_token',
    :authorize_path => '/service/auth/authorize',
    :realm => 'OmniAuth',
    :request_token_path => '/service/auth/request_token',
    :site => 'http://www.douban.com',
  }

  super(app, :douban, consumer_key, consumer_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/omniauth/strategies/oauth/douban.rb', line 26

def auth_hash
  OmniAuth::Utils.deep_merge(
    super, {
      'uid' => @access_token.params[:douban_user_id],
      'user_info' => ,
      'extra' => {
        'user_hash' => user_hash,
      },
    }
  )
end

#user_hashObject



55
56
57
# File 'lib/omniauth/strategies/oauth/douban.rb', line 55

def user_hash
  @user_hash ||= MultiJson.decode(@access_token.get('http://api.douban.com/people/%40me?alt=json').body)
end

#user_infoObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omniauth/strategies/oauth/douban.rb', line 38

def 
  user_hash = self.user_hash
  location = user_hash['location'] ? user_hash['location']['$t'] : nil
  image = user_hash['link'].find{|l| l['@rel'] == 'icon'}['@href']
  douban_url = user_hash['link'].find{|l| l['@rel'] == 'alternate'}['@href']
  {
    'username' => user_hash['db:uid']['$t'],
    'name' => user_hash['title']['$t'],
    'location' => location,
    'image' => image,
    'description' => user_hash['content']['$t'],
    'urls' => {
      'Douban' => douban_url,
    },
  }
end