forked from brucehsu/s2sync_web
/
plurk.rb
82 lines (62 loc) · 1.76 KB
/
plurk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'rubygems'
require 'json/pure'
require 'rest-core'
Plurk = RestCore::Builder.client(:data) do
s = self.class
use s::DefaultSite, 'http://www.plurk.com/APP'
use s::Oauth1Header ,
'http://www.plurk.com/OAuth/request_token',
'http://www.plurk.com/OAuth/access_token',
'http://www.plurk.com/OAuth/authorize'
use s::CommonLogger , method(:puts)
use s::ErrorHandler , lambda { |env|
raise Plurk::Error.call(env)
}
use s::JsonDecode , true
use s::Cache , {}, 3600
use s::Defaults , :data => lambda{{}}
run s::RestClient
end
class Plurk::Error < RuntimeError
attr_reader :error
def initialize (error)
@error = error
end
def self.call(env)
@error = env["RESPONSE_BODY"]["error_text"]
end
end
module Plurk::Client
include RestCore
def oauth_token
data['oauth_token'] if data.kind_of?(Hash)
end
def oauth_token= token
data['oauth_token'] = token if data.kind_of?(Hash)
end
def oauth_token_secret
data['oauth_token_secret'] if data.kind_of?(Hash)
end
def oauth_token_secret= secret
data['oauth_token_secret'] = secret if data.kind_of?(Hash)
end
def oauth_signature
data['oauth_signature'] if data.kind_of?(Hash)
end
def oauth_signature= sign
data['oauth_signature'] if data.kind_of?(Hash)
end
def add_plurk content, qualifier='says'
post('/Timeline/plurkAdd', {"content"=>content, "qualifier" => qualifier}, nil)
end
def add_response plurk_id, content, qualifier='says'
post('/Responses/responseAdd', {'plurk_id' => plurk_id,
'content' => content, 'qualifier' => qualifier}, nil)
end
private
def set_token query
self.data = query
end
end
Plurk.send(:include, RestCore::ClientOauth1)
Plurk.send(:include, Plurk::Client)