Class: Douban

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_token = nil, request_token_secret = nil) ⇒ Douban



53
54
55
56
57
58
59
# File 'lib/douban.rb', line 53

def initialize(request_token = nil, request_token_secret = nil)
  if request_token && request_token_secret
    self.request_token = OAuth::RequestToken.new(self.class.auth_consumer, request_token, request_token_secret)
  else
    self.request_token = self.class.auth_consumer.get_request_token()
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



4
5
6
# File 'lib/douban.rb', line 4

def access_token
  @access_token
end

#callbackObject

Returns the value of attribute callback.



4
5
6
# File 'lib/douban.rb', line 4

def callback
  @callback
end

#request_tokenObject

Returns the value of attribute request_token.



4
5
6
# File 'lib/douban.rb', line 4

def request_token
  @request_token
end

Class Method Details

.api_consumerObject



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

def api_consumer
  @@api_consumer ||= OAuth::Consumer.new(key, secret,
    {
      :site             => "http://api.douban.com",
      :scheme           => :header,
      :signature_method => "HMAC-SHA1",
      :realm            => url
    })
end

.auth_consumerObject



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

def auth_consumer
  @@auth_consumer ||= OAuth::Consumer.new(key, secret, {
      :signature_method   => "HMAC-SHA1",
      :site               => "http://www.douban.com",
      :scheme             => :header,
      :request_token_path => '/service/auth/request_token',
      :access_token_path  => '/service/auth/access_token',
      :authorize_path     => '/service/auth/authorize',
      :realm              => url
     })
end

.configObject



42
43
44
45
46
47
48
49
50
# File 'lib/douban.rb', line 42

def config
  @@config ||= lambda do
    require 'yaml'
    filename = "#{Rails.root}/config/douban.yml"
    file     = File.open(filename)
    yaml     = YAML.load(file)
    return yaml[Rails.env]
  end.call
end

.keyObject



38
# File 'lib/douban.rb', line 38

def key; config['key'];  end

.load(data) ⇒ Object



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

def load(data)
  access_token        = data[:access_token]
  access_token_secret = data[:access_token_secret]

  douban               = Douban.new(data[:request_token], data[:request_token_secret])
  douban.access_token  = OAuth::AccessToken.new(api_consumer, access_token, access_token_secret) if access_token
  douban
end

.secretObject



39
# File 'lib/douban.rb', line 39

def secret; config['secret']; end

.urlObject



40
# File 'lib/douban.rb', line 40

def url; config['url']; end

Instance Method Details

#authorizeObject



65
66
67
68
69
70
# File 'lib/douban.rb', line 65

def authorize
  return unless self.access_token.nil?
  
  access_token = self.request_token.get_access_token
  self.access_token ||= OAuth::AccessToken.new(self.class.api_consumer, access_token.token, access_token.secret)
end

#authorize_urlObject



61
62
63
# File 'lib/douban.rb', line 61

def authorize_url
  @authorize_url ||= request_token.authorize_url(:oauth_callback => self.callback)
end

#authorized?Boolean



72
73
74
75
76
# File 'lib/douban.rb', line 72

def authorized?
  return false if access_token.nil?
  response = self.get("/access_token/#{access_token.token}")
  response.code == '200'
end

#delete(path, headers = {}) ⇒ Object



104
105
106
# File 'lib/douban.rb', line 104

def delete(path, headers = {})
  request(:delete, path, headers)
end

#destroyObject



78
79
80
81
# File 'lib/douban.rb', line 78

def destroy
  destroy_access_key if !access_token.nil?
  request_token = access_token = nil
end

#dumpObject



83
84
85
86
87
88
89
90
# File 'lib/douban.rb', line 83

def dump
  {
    :request_token        => request_token.token, 
    :request_token_secret => request_token.secret,
    :access_token         => access_token.nil? ? nil : access_token.token,
    :access_token_secret  => access_token.nil? ? nil : access_token.secret
  }
end

#get(path, headers = {}) ⇒ Object



96
97
98
# File 'lib/douban.rb', line 96

def get(path, headers = {})
  request(:get, path, headers)
end

#post(path, headers = {}) ⇒ Object



100
101
102
# File 'lib/douban.rb', line 100

def post(path, headers = {})
  request(:post, path, headers)
end

#put(path, headers = {}) ⇒ Object



108
109
110
# File 'lib/douban.rb', line 108

def put(path, headers = {})
  request(:put, path, headers)
end

#request(http_method, path, headers = {}) ⇒ Object



92
93
94
# File 'lib/douban.rb', line 92

def request(http_method, path, headers = {})
  access_token.request(http_method, path, headers)
end