qq-pengyou 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +8 -0
  2. data/lib/qq-pengyou.rb +230 -0
  3. data/test.rb +27 -0
  4. metadata +69 -0
@@ -0,0 +1,8 @@
1
+ 腾讯社区开放平台作为社交型应用的开发接入平台,提供了一系列的开放接口(Open API)和开发工具包(SDK),广大开发者只要登录平台网站并创建应用,就可以通过Open API获取登录用户信息、用户签名信息以及好友关系链信息等。已上线的应用还可以调用支付和监控接口,实现支付功能,查看应用实时运维数据。
2
+
3
+ 此项目是 腾讯官方SDK的 Ruby 实现。
4
+
5
+ http://opensns.qq.com/apps/wiki
6
+
7
+ Install
8
+
@@ -0,0 +1,230 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ # 错误代码定义
8
+ # 参数为空
9
+ PYO_ERROR_REQUIRED_PARAMETER_EMPTY = 2001
10
+ # 参数格式果物
11
+ PYO_ERROR_REQUIRED_PARAMETER_INVALID = 2002
12
+ # 返回包格式错误
13
+ PYO_ERROR_RESPONSE_DATA_INVALID = 2003
14
+ # 网络错误, 偏移量3000, 详见 http://curl.haxx.se/libcurl/c/libcurl-errors.html
15
+ PYO_ERROR_CURL = 3000
16
+
17
+
18
+ class QQPengyou
19
+ VERSION = '1.0.0'
20
+
21
+ # Pengyou OpenAPI 服务器的域名
22
+ # 默认:http://openapi.pengyou.qq.com
23
+ attr :server_name
24
+
25
+ # 构造函数
26
+ # 参数:
27
+ # - app_id : 应用的唯一ID
28
+ # - app_key : 应用的密钥,用于验证应用的合法性
29
+ # - app_name : 应用的英文名(唯一)
30
+ def initialize(app_id,app_key,app_name)
31
+ @server_name = 'openapi.pengyou.qq.com'
32
+ @app_id = app_id
33
+ @app_key = app_key
34
+ @app_name = app_name
35
+ end
36
+
37
+ class << self
38
+ def valid_open_id?(open_id)
39
+ return true if /^[0-9a-fA-F]{32}$/.match(open_id)
40
+ false
41
+ end
42
+ end
43
+
44
+
45
+ def api(method,options = {})
46
+ # 验证信息
47
+ if options[:openid] == nil or options[:openid] == ''
48
+ return {:ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'openid is empty'}
49
+ end
50
+ if options[:openkey] == nil or options[:openkey] == ''
51
+ return {:ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'openkey is empty'}
52
+ end
53
+ if not QQPengyou.valid_open_id?(options[:openid])
54
+ return {:ret => PYO_ERROR_REQUIRED_PARAMETER_INVALID, :msg => 'openid is invalid'}
55
+ end
56
+
57
+ # 添加一些参数
58
+ options[:appid] = @app_id
59
+ options[:appkey] = @app_key
60
+ options[:ref] = @app_name
61
+
62
+ # 得到 OpenAPI 的地址
63
+ path = get_api_path(method)
64
+
65
+ return make_request(path,options)
66
+ end
67
+
68
+
69
+ private
70
+ # 执行一个 HTTP POST 请求, 返回结果数组。可能发生cURL错误
71
+ # 参数:
72
+ # - path : 执行请求的路径
73
+ # - options : 表单参数
74
+ def make_request(path,options = {})
75
+ http = Net::HTTP.new("#{@server_name}")
76
+ query_params = options.collect { |v| v.join("=") }.join("&")
77
+ req = http.request_post(path,query_params,{'User Agent' => 'pengyou-php-1.0'})
78
+ begin
79
+ #
80
+ rescue Exception => e
81
+ # cURL 网络错误, 返回错误码为 cURL 错误码加偏移量
82
+ # 详见 http://curl.haxx.se/libcurl/c/libcurl-errors.html
83
+ return {:ret => PYO_ERROR_CURL, :msg => e.inspect }
84
+ end
85
+
86
+ # 远程返回的不是 json 格式, 说明返回包有问题
87
+ result_json = JSON.parse(req.body)
88
+ if result_json == {}
89
+ return {:ret => PYO_ERROR_RESPONSE_DATA_INVALID, :msg => result}
90
+ end
91
+
92
+ return result_json
93
+ end
94
+
95
+
96
+ # 根据要调用的方法返回 API URL
97
+ def get_api_path(method)
98
+ "/cgi-bin/xyoapp/#{method}.cgi"
99
+ end
100
+
101
+ public
102
+ # 返回当前登录用户信息
103
+ # return hash
104
+ # - ret : 返回码 (0:正确返回, [1000,~]错误)
105
+ # - nickname : 昵称
106
+ # - gender : 性别
107
+ # - province : 省
108
+ # - city : 市
109
+ # - figureurl : 头像url
110
+ # - is_vip : 是否黄钻用户 (true|false)
111
+ # - is_year_vip : 是否年费黄钻(如果is_vip为false, 那is_year_vip一定是false)
112
+ # - vip_level : 黄钻等级(如果是黄钻用户才返回此字段)
113
+ def get_user_info(openid,openkey)
114
+ return api('xyoapp_get_userinfo',:openid => openid, :openkey => openkey)
115
+ end
116
+
117
+ # 验证是否好友(验证 fopenid 是否是 openid 的好友)
118
+ # 参数:
119
+ # openid openid
120
+ # openkey openkey
121
+ # options Hash 值
122
+ # fopenid 待验证用户的openid
123
+ # 返回值(Hash):
124
+ # - ret : 返回码 (0:正确返回, [1000,~]错误)
125
+ # - isFriend : 是否为好友(0:不是好友; 1:是好友; 2:是同班同学)
126
+ def is_friend(openid,openkey, options = {})
127
+ if not QQPengyou.valid_open_id?(options[:fopenid])
128
+ return {:ret => PYO_ERROR_REQUIRED_PARAMETER_INVALID, :msg => 'fopenid is invalid' }
129
+ end
130
+
131
+ return api('xyoapp_get_isrelation',
132
+ :openid => openid,
133
+ :openkey => openkey,
134
+ :fopenid => options[:fopenid])
135
+ end
136
+
137
+ # 获取好友列表
138
+ # 参数 options 说明:
139
+ # :infoed 是否需要好友的详细信息(0:不需要;1:需要)
140
+ # :apped 对此应用的安装情况(-1:没有安装;1:安装了的;0:所有好友)
141
+ # :page 获取对应页码的好友列表,从1开始算起,每页是100个好友。(不传或者0:返回所有好友;>=1,返回对应页码的好友信息)
142
+ # 返回值:
143
+ # @return array 好友关系链的数组
144
+ # - ret : 返回码 (0:正确返回; (0,1000):部分数据获取错误,相当于容错的返回; [1000,~]错误)
145
+ # - items : array 用户信息
146
+ # - openid : 好友QQ号码转化得到的id
147
+ # - nickname : 昵称(infoed==1时返回)
148
+ # - gender : 性别(infoed==1时返回)
149
+ # - figureurl : 头像url(infoed==1时返回)
150
+ def get_friend_list(openid,openkey, options = {})
151
+ infoed = options[:infoed] || 0
152
+ apped = options[:apped] || 1
153
+ page = options[:page] || 0
154
+ return api('xyoapp_get_relationinfo',
155
+ :openid => openid,
156
+ :openkey => openkey,
157
+ :infoed => infoed,
158
+ :apped => apped,
159
+ :page => page)
160
+ end
161
+
162
+ # 批量获取用户详细信息
163
+ # 参数:
164
+ # - :fopenids : Array 需要获取数据的openid列表
165
+ # 返回 HASH 好友详细信息数组:
166
+ # - ret : 返回码 (0:正确返回; (0,1000):部分数据获取错误,相当于容错的返回; [1000,~]错误)
167
+ # - items : array 用户信息
168
+ # - openid : 好友的 OPENID
169
+ # - nickname :昵称
170
+ # - gender : 性别
171
+ # - figureurl : 头像url
172
+ # - is_vip : 是否黄钻 (true:黄钻; false:普通用户)
173
+ # - is_year_vip : 是否年费黄钻 (is_vip为true才显示)
174
+ # - vip_level : 黄钻等级 (is_vip为true才显示)
175
+ def get_multi_info(openid, openkey, options = {})
176
+ fopenids = options[:fopenids]
177
+ if fopenids == nil or fopenids.class != [].class
178
+ return { :ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'fopenids is empty or not a Array type.' }
179
+ end
180
+
181
+ return api('xyoapp_multi_info',
182
+ :openid => openid,
183
+ :openkey => openkey,
184
+ :fopenids => fopenids.join("_"))
185
+ end
186
+
187
+ # 验证登录用户是否安装了应用
188
+ # 返回值 hash:
189
+ # - ret : 返回码 (0:正确返回, [1000,~]错误)
190
+ # - setuped : 是否安装(0:没有安装;1:安装)
191
+ def setuped?(openid, openkey)
192
+ return api('xyoapp_get_issetuped',
193
+ :openid => openid,
194
+ :openkey => openkey)
195
+ end
196
+
197
+ # 判断用户是否为黄钻
198
+ # 返回值 hash:
199
+ # - ret : 返回码 (0:正确返回, [1000,~]错误)
200
+ # - is_vip : 是否黄钻 (true:黄钻; false:普通用户)
201
+ def vip?(openid, openkey)
202
+ return api('xyoapp_pay_showvip',
203
+ :openid => openid,
204
+ :openkey => openkey)
205
+ end
206
+
207
+ # 获取好友的签名信息
208
+ # 参数:
209
+ # - openid
210
+ # - openkey
211
+ # - :fopenids : Array 需要获取数据的openid列表(一次最多20个)
212
+ # 返回值 HASH:
213
+ # - ret : 返回码 (0:正确返回; (0,1000):部分数据获取错误,相当于容错的返回; [1000,~]错误)
214
+ # - items : Array 用户信息
215
+ # - openid: 好友QQ号码转化得到的id
216
+ # - content: 好友的校友心情内容
217
+ def get_emotion(openid, openkey, options = {})
218
+ fopenids = options[:fopenids]
219
+ if fopenids == nil or fopenids.class != [].class
220
+ return { :ret => PYO_ERROR_REQUIRED_PARAMETER_EMPTY, :msg => 'fopenids is empty or not a Array type.' }
221
+ end
222
+
223
+
224
+ return api('xyoapp_get_emotion',
225
+ :openid => openid,
226
+ :openkey => openkey,
227
+ :fopenids => fopenids.join("_"))
228
+ end
229
+
230
+ end
data/test.rb ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require 'lib/qq-pengyou'
3
+
4
+ q = QQPengyou.new(10158,'d3ef8c1c6770480685e8af870ec31b0b','app10158')
5
+
6
+ # 测试的时候需要更新 openid 和 openkey 每次登录后都会有变化
7
+ # 可以在 http://opensns.qq.com/apps/tools 这里得到你的 QQ 的 openid 和 openkey
8
+ openid = '00000000000000000000000001B2DB35'
9
+ openkey = '018CD1F8174C1D5355849097CF3E26F70DDD1725F95BACF4'
10
+ puts q.get_user_info(openid,openkey).inspect
11
+ puts ""
12
+
13
+ friends = q.get_friend_list(openid,openkey)
14
+ puts friends.inspect
15
+ fopenids = friends['items'].collect { |item| item['openid'] }
16
+
17
+
18
+ puts q.get_multi_info(openid, openkey, :fopenids => fopenids).inspect
19
+ puts ""
20
+
21
+ puts q.setuped?(openid, openkey).inspect
22
+ puts ""
23
+
24
+ puts q.vip?(openid, openkey).inspect
25
+ puts ""
26
+
27
+ puts q.get_emotion(openid, openkey,:fopenids => fopenids).inspect
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qq-pengyou
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Lee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-17 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: QQ opensns SDK api for Ruby, http://opensns.qq.com/
23
+ email: huacnlee@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.rdoc
32
+ - test.rb
33
+ - lib/qq-pengyou.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/huacnlee/qq-pengyou
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: QQ opensns SDK api for Ruby
68
+ test_files: []
69
+