Class: OmniAuth::Strategies::Douban

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

Overview

Authenticate to Douban via OAuth and retrieve basic user information.

Usage:

use OmniAuth::Strategies::Douban, 'APIKey', 'APIKeySecret'

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Douban.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/omniauth_china/strategies/douban.rb', line 15

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 = {
    :site               => 'http://www.douban.com',
    :request_token_path => '/service/auth/request_token',
    :access_token_path  => '/service/auth/access_token',
    :authorize_path     => '/service/auth/authorize',
    :realm              => 'OmniAuth'
  }

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

Instance Method Details

#auth_hashObject



29
30
31
32
33
34
35
# File 'lib/omniauth_china/strategies/douban.rb', line 29

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_china/strategies/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



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

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