open_qq 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 zires
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ open_qq
2
+ =======
3
+
4
+ 腾讯开放平台ruby版SDK(v3版本)
5
+
6
+ ## 安装
7
+
8
+ ```ruby
9
+ gem install open_qq
10
+ ```
11
+
12
+ ## 使用
13
+
14
+ 使用非常简单,传入应用的appid, appkey和环境地址env _http://openapi.tencentyun.com_ 或者 _http://119.147.19.43_ 即可
15
+
16
+ ```ruby
17
+ require 'rubygems'
18
+ require 'open_qq'
19
+
20
+ OpenQq.setup(:appid => '123', :appkey => '456', :env => 'http://119.147.19.43')
21
+
22
+ # get请求
23
+ user_info = OpenQq.get('/v3/user/get_info', :openid => '111',:openkey => '222')
24
+
25
+ # 或者post请求
26
+ user_info = OpenQq.post('/v3/user/get_info',:openid => '111',:openkey => '222')
27
+
28
+ user_info.ret # => 0
29
+ user_info.nickname # => 'foo'
30
+ ```
31
+
32
+ 如果你只想原样返回未加工的数据,使用`raw => true`
33
+
34
+ ```ruby
35
+ user_info = OpenQq.post('/v3/user/get_info', {:openid => '111', :openkey => '222'}, :raw => true)
36
+ puts user_info
37
+ # => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }'
38
+ ```
39
+
40
+ 如果你不想使用全局的配置
41
+
42
+ ```ruby
43
+ options = {:appid => 'newappid', :appkey => 'newappkey', :env => 'http://newenv'}
44
+
45
+ user_info = OpenQq.start('/v3/user/get_info', options) do |request|
46
+
47
+ request.get {:openid => '111',:openkey => '222'}
48
+
49
+ #或者
50
+ request.post {:openid => '111',:openkey => '222'}
51
+
52
+ end
53
+
54
+ user_info.nickname
55
+ # => 'foo'
56
+ ```
57
+
58
+ ## 在rails中使用
59
+
60
+ 首先在Gemfile中添加
61
+ ```
62
+ gem 'open_qq'
63
+ ```
64
+
65
+ 执行`bundle install`
66
+
67
+ 在config目录下生成配置文件config/open_qq.yml
68
+ ```
69
+ rails g open_qq:install
70
+ ```
71
+
72
+ 在配置文件中填入appid, appkey和env的值,启动服务后全局都可以使用,例如:
73
+
74
+ ```ruby
75
+ class OpenQqController < ApplicationController
76
+
77
+ # 假设这里是应用的入口
78
+ def index
79
+ user_info = OpenQq.post('/v3/user/get_info', params.slice!(:action, :controller))
80
+ if user_info.ret == 0
81
+ # do something
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ ```
88
+
89
+ ## 注意和说明
90
+
91
+ * 当传入的format为xml时,不会对返回的结果做处理,直接字符串返回
92
+ * 当传入的format不为xml时,会使用`JSON#parse`转换成hash,并且使用[OpenStruct](http://www.ruby-doc.org/stdlib-1.8.7/libdoc/ostruct/rdoc/OpenStruct.html, 'OpenStruct')封装
93
+ * 当ret返回`2001`时,是由本api抛出
94
+ * 关于*signature verification failed*,先仔细对照[文档](http://wiki.open.qq.com/wiki/%E8%85%BE%E8%AE%AF%E5%BC%80%E6%94%BE%E5%B9%B3%E5%8F%B0%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E7%AD%BE%E5%90%8D%E5%8F%82%E6%95%B0sig%E7%9A%84%E8%AF%B4%E6%98%8E#.E4.B8.BA.E4.BB.80.E4.B9.88.E6.80.BB.E6.98.AF.E8.BF.94.E5.9B.9E.E2.80.9C-5.EF.BC.9Asignature_verification_failed.E2.80.9D.EF.BC.9F)
95
+
96
+ ```
97
+ 可以通过联调工具看下签名是否一致
98
+
99
+ opts = {:openid => '1111',:openkey => '2222',:pf => 'pengyou'}
100
+ sig = OpenQq.wrap(:post, '/v3/user/get_info', opts)['sig']
101
+
102
+ puts sig # 与联调结果比对
103
+
104
+ ```
105
+
106
+ * 如果不想使用open_qq.yml,只要在使用前全局配置好`OpenQq`即可
107
+ * 测试基本覆盖,可以下载下来执行`rake`
108
+ * bug反馈[Issue](https://github.com/zires/open_qq/issues)
109
+
110
+ #### This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'yard'
9
+ YARD::Rake::YardocTask.new
10
+
11
+ require 'rake/testtask'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ module OpenQq
3
+ module Generators
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ # Create open_qq.yml under config/initializers/
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a ThemePark initializer file."
9
+ def copy_initializer
10
+ template "open_qq.yml", "config/open_qq.yml"
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # open_qq configruation.
2
+ # see http://open.qq.com/
3
+ development:
4
+ appid:
5
+ appkey:
6
+ env: http://119.147.19.43
7
+
8
+ test:
9
+ appid:
10
+ appkey:
11
+ env: http://119.147.19.43
12
+
13
+ production:
14
+ appid:
15
+ appkey:
16
+ env: http://openapi.tencentyun.com
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ module OpenQq
8
+ # behavior response to body method
9
+ class Error
10
+
11
+ def initialize(ret, msg, format = 'json')
12
+ @ret, @msg, @format = ret, msg, format.to_s
13
+ end
14
+
15
+ def body
16
+ if @format == 'xml'
17
+ %Q(<?xml version="1.0" encoding="UTF-8"?><data><ret>#{@ret}</ret><msg>#{@msg}</msg></data>)
18
+ else
19
+ {:ret => @ret, :msg => @msg}.to_json
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ require 'open_qq/signature'
8
+ require 'open_qq/error'
9
+ require 'ostruct'
10
+ require 'net/http'
11
+
12
+ module OpenQq
13
+ class Gateway
14
+
15
+ OPEN_HTTP_TRANSLATE_ERROR = 2001
16
+
17
+ attr_accessor :appid, :appkey, :env
18
+
19
+ # @param [String] appid
20
+ # @param [String] appkey
21
+ # @param [String] env 调用的环境地址.生产环境: <http://openapi.tencentyun.com> 测试环境: <http://119.147.19.43>
22
+ def initialize(appid, appkey, env)
23
+ @appid = appid
24
+ @appkey = appkey
25
+ self.env = env
26
+ end
27
+
28
+ # override
29
+ def env=(env)
30
+ @env = env
31
+ uri = URI.parse(env)
32
+ @http = Net::HTTP.new(uri.host, uri.port)
33
+ end
34
+
35
+ # @param [String] url which api you want to call
36
+ # @param [Hash] params extra params attach to the url
37
+ # @param [options] options
38
+ #
39
+ # @example
40
+ # gateway = OpenQq::Gateway.new('1111', '2222', 'http://119.147.19.43')
41
+ # user_info = gateway.get('/v3/user/get_info', {:openid => '11'} )
42
+ # user_info.nickname # => foo
43
+ #
44
+ # @example available option
45
+ # :raw => true will raw the output
46
+ # user_info = gateway.get('/v3/user/get_info', {:openid => '11'}, {:raw => true} )
47
+ # user_info.nickname # => '{"nickname":"foo"}'
48
+ #
49
+ # @return (@see #call)
50
+ def get(url, params = {}, options = {})
51
+ parsed_params = each_pair_escape( wrap(:get, url, params) ).map{|k,v| "#{k}=#{v}"}.join('&')
52
+ get_request = Net::HTTP::Get.new("#{url}?#{parsed_params}")
53
+ self.call( get_request, options.merge(:format => params[:format]) )
54
+ end
55
+
56
+ # @param (see #get)
57
+ #
58
+ # @return (@see #call)
59
+ def post(url, params = {}, options = {})
60
+ post_request = Net::HTTP::Post.new(url)
61
+ post_request.set_form_data wrap(:post, url, params)
62
+ self.call( post_request, options.merge(:format => params[:format]) )
63
+ end
64
+
65
+ def wrap(http_method, url, params)
66
+ params = params.merge(:appid => @appid)
67
+ params[:sig] = signature( "#{@appkey}&", make_source(http_method.to_s.upcase, url, params) )
68
+ params
69
+ end
70
+
71
+ protected
72
+
73
+ # Call the request and format the output
74
+ # @param [Net::HTTP::Request] request be called
75
+ # @param [Hash] options some option used to format output
76
+ #
77
+ # @return [String, Object] unformatted string result or parsed OpenStruct instance
78
+ def call(request, options)
79
+ response = begin
80
+ @http.request(request)
81
+ rescue Exception => e
82
+ Error.new(OPEN_HTTP_TRANSLATE_ERROR, "#{e.message}\n#{e.backtrace.inspect}", options[:format])
83
+ end
84
+
85
+ return response.body if options[:raw] || options[:format] == 'xml'
86
+
87
+ OpenStruct.new( JSON.parse(response.body) )
88
+ end
89
+
90
+ private
91
+
92
+ include Signature
93
+
94
+ end
95
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module OpenQq
3
+ class Railtie < ::Rails::Railtie
4
+
5
+ generators do
6
+ require "generators/open_qq/install_generator"
7
+ end
8
+
9
+ initializer "open_qq.setup" do
10
+ config_file = Rails.root.join("config", "open_qq.yml")
11
+ if config_file.file?
12
+ begin
13
+ options = YAML.load_file(config_file)[Rails.env]
14
+ OpenQq.setup(options)
15
+ rescue Exception => e
16
+ puts "There is a configuration error with the current open_qq.yml"
17
+ puts e.message
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ require 'open_qq/gateway'
8
+
9
+ module OpenQq
10
+ class Request < Gateway
11
+
12
+ attr_accessor :url, :options
13
+
14
+ def initialize(options)
15
+ options = options.dup
16
+ appid = options.delete(:appid)
17
+ appkey = options.delete(:appkey)
18
+ env = options.delete(:env)
19
+ @url = options.delete(:url)
20
+ @options = options
21
+ super(appid, appkey, env)
22
+ end
23
+
24
+ # @see Gateway#get
25
+ def get(params = {}, options = @options)
26
+ super(@url, params, options)
27
+ end
28
+
29
+ # @see Gateway#get
30
+ def post(params = {}, options = @options)
31
+ super(@url, params, options)
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ require 'base64'
8
+ require 'openssl'
9
+ require 'uri'
10
+
11
+ module OpenQq
12
+ module Signature
13
+
14
+ def signature(key, source)
15
+ Base64.encode64( OpenSSL::HMAC.digest('sha1', key, source) ).rstrip
16
+ end
17
+
18
+ def url_escape(url)
19
+ URI.escape(url, /[^\.\-_\da-zA-Z]/)
20
+ end
21
+
22
+ def each_pair_escape(options)
23
+ options.inject({}){|h,(k,v)| h[url_escape(k.to_s)] = url_escape(v.to_s);h}
24
+ end
25
+
26
+ def make_source(http_method, url, options)
27
+ escape_opt = url_escape( options.sort_by{|k,v| k.to_s}.map{|kv| "#{kv.first}=#{kv.last}" }.join('&') )
28
+ "#{http_method}&#{url_escape(url)}&#{escape_opt}"
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ module OpenQq
8
+ # 3.0表示OpenAPI版本, 小数点第二位表示SDK版本
9
+ VERSION = "3.0.1"
10
+ end
data/lib/open_qq.rb ADDED
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ #
3
+ # --
4
+ # @author zires
5
+ # @email zshuaibin@gmail.com
6
+ #
7
+ require 'rubygems' if RUBY_VERSION < '1.9'
8
+ require 'json'
9
+ require 'open_qq/request'
10
+ require 'active_support/core_ext/module/delegation'
11
+ require 'active_support/core_ext/hash/indifferent_access'
12
+
13
+ module OpenQq
14
+ class << self
15
+
16
+ def setup(options = {})
17
+ options = options.with_indifferent_access
18
+ @gateway ||= OpenQq::Gateway.new(options[:appid], options[:appkey], options[:env])
19
+ yield @gateway if block_given?
20
+ end
21
+
22
+ # @see Gateway#get
23
+ delegate :appid, :appkey, :env, :get, :post, :wrap, :to => :@gateway
24
+
25
+ # @example
26
+ # OpenQq.start('/v3/user/get_info', { :appid => 123 }) do |request|
27
+ # request.get({:openid => '111'})
28
+ # end
29
+ def start(url, options)
30
+ request = OpenQq::Request.new(options.with_indifferent_access)
31
+ if block_given?
32
+ yield request
33
+ else
34
+ request
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+
41
+ require 'open_qq/railtie' if defined?(Rails)
data/test/benchmark.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'benchmark'
3
+ require 'ostruct'
4
+ require 'json'
5
+
6
+ n = 500000
7
+ @os = { :ret => 0,
8
+ :is_lost => 0,
9
+ :nickname => 'Peter',
10
+ :gender => 'boy',
11
+ :country => 'country',
12
+ :province => 'province',
13
+ :city => 'city',
14
+ :figureurl => 'http://imgcache.qq.com/qzone_v4/client/userinfo_icon/1236153759.gif',
15
+ :is_yellow_vip => 1,
16
+ :is_yellow_year_vip => 1,
17
+ :yellow_vip_level => 7
18
+ }
19
+
20
+ @os_obj = OpenStruct.new(@os)
21
+
22
+ Benchmark.bm do |x|
23
+
24
+ x.report { for i in 1..n; @os_obj.nickname; end }
25
+ x.report { for i in 1..n; @os['nickname']; end }
26
+
27
+ end
28
+
29
+ @os_json = @os.to_json
30
+
31
+ m = 10
32
+
33
+ Benchmark.bm do |x|
34
+
35
+ x.report { for i in 1..m; JSON.parse(@os_json); end }
36
+ x.report { for i in 1..m; OpenStruct.new( JSON.parse(@os_json) ); end }
37
+
38
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class OpenQq::ErrorTest < MiniTest::Unit::TestCase
4
+
5
+ def test_symbol_format
6
+ error = OpenQq::Error.new(100, 'error!', :json)
7
+ assert_equal '{"ret":100,"msg":"error!"}', error.body
8
+ end
9
+
10
+ def test_json_format
11
+ error = OpenQq::Error.new(100, 'error!', 'json')
12
+ assert_equal '{"ret":100,"msg":"error!"}', error.body
13
+ end
14
+
15
+ def test_xml_format
16
+ error = OpenQq::Error.new(100, 'error!', 'xml')
17
+ assert_equal '<?xml version="1.0" encoding="UTF-8"?><data><ret>100</ret><msg>error!</msg></data>', error.body
18
+ end
19
+
20
+ def test_no_format
21
+ error = OpenQq::Error.new(100, 'error!')
22
+ assert_equal '{"ret":100,"msg":"error!"}', error.body
23
+ end
24
+
25
+ def test_unsupported_format
26
+ error = OpenQq::Error.new(100, 'error!', :ruby)
27
+ assert_equal '{"ret":100,"msg":"error!"}', error.body
28
+ end
29
+
30
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class OpenQq::GatewayTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ # 测试用例的签名通过腾讯联调工具产生 http://open.qq.com/tools
7
+ @appid = 12345
8
+ @appkey = '228bf094169a40a3bd188ba37ebe8723'
9
+ @env = 'http://119.147.19.43'
10
+ @gateway = OpenQq::Gateway.new(@appid, @appkey, @env)
11
+ end
12
+
13
+ def test_call_get_request_without_params
14
+ assert_equal 'foo', @gateway.get('/v3/user/get_info').nickname
15
+ end
16
+
17
+ def test_call_get_request_with_params
18
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'json' }
19
+ assert_equal 'foo', @gateway.get('/v3/user/get_info', params).nickname
20
+ end
21
+
22
+ def test_call_get_request_with_xml_format
23
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'xml' }
24
+ assert_equal '<?xml version="1.0" encoding="UTF-8"?>', @gateway.get('/v3/user/get_info', params)
25
+ end
26
+
27
+ def test_call_get_request_without_format_param
28
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou }
29
+ assert_equal 'foo', @gateway.get('/v3/user/get_info', params).nickname
30
+ end
31
+
32
+ def test_call_get_request_with_unknow_format_param
33
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'ruby'}
34
+ assert_equal 'foo', @gateway.get('/v3/user/get_info', params).nickname
35
+ end
36
+
37
+ def test_call_post_request
38
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
39
+ assert_equal 'foo', @gateway.post('/v3/user/get_info', params).nickname
40
+ end
41
+
42
+ def test_call_post_request_with_raw_output
43
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
44
+ assert_equal '{ "ret": 0, "is_lost": 0, "nickname": "foo" }', @gateway.post('/v3/user/get_info', params, :raw => true)
45
+ end
46
+
47
+ def test_open_http_translate_error
48
+ gateway = OpenQq::Gateway.new('123456', @appkey, @env)
49
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
50
+ assert_equal 2001, gateway.get('/v3/user/get_info', params).ret
51
+ end
52
+
53
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ class OpenQq::RequestTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ # 测试用例的签名通过腾讯联调工具产生 http://open.qq.com/tools
7
+ options = { :appid => 12345,
8
+ :appkey => '228bf094169a40a3bd188ba37ebe8723',
9
+ :env => 'http://119.147.19.43',
10
+ :url => '/v3/user/get_info',
11
+ }
12
+ @request = OpenQq::Request.new(options)
13
+ end
14
+
15
+ def test_call_get_request_without_params
16
+ assert_equal 'foo', @request.get.nickname
17
+ end
18
+
19
+ def test_call_get_request_with_params
20
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'json' }
21
+ assert_equal 'foo', @request.get(params).nickname
22
+ end
23
+
24
+ def test_call_post_request
25
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
26
+ assert_equal 'foo', @request.post(params).nickname
27
+ end
28
+
29
+ def test_call_post_request_with_raw_output
30
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
31
+ assert_equal '{ "ret": 0, "is_lost": 0, "nickname": "foo" }', @request.post(params, :raw => true)
32
+ end
33
+
34
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class Foo
4
+ include OpenQq::Signature
5
+ end
6
+
7
+ class OpenQq::SignatureTest < MiniTest::Unit::TestCase
8
+
9
+ def setup
10
+ @foo = Foo.new
11
+ # 测试数据是通过腾讯开放平台联调工具集生成
12
+ # 详见:http://open.qq.com/tools
13
+ @url = '/v3/user/get_info'
14
+ @http_method = :GET
15
+ @appkey = '228bf094169a40a3bd188ba37ebe8723'
16
+
17
+ #openid=11111111111111111&openkey=2222222222222222&pf=qzone&appid=123456&format=json&userip=10.0.0.1
18
+ @options = { :openid => '11111111111111111', :openkey => '2222222222222222',
19
+ 'appid' => '123456', 'pf' => 'qzone',
20
+ :userip => '10.0.0.1', :format => :json
21
+ }
22
+ end
23
+
24
+ def test_signature_is_a_module
25
+ assert_equal Module, ::OpenQq::Signature.class
26
+ end
27
+
28
+ def test_make_source
29
+ expect_source = 'GET&%2Fv3%2Fuser%2Fget_info&appid%3D123456%26format%3Djson%26openid%3D11111111111111111%26openkey%3D2222222222222222%26pf%3Dqzone%26userip%3D10.0.0.1'
30
+ assert_equal expect_source, @foo.make_source(@http_method, @url, @options)
31
+ end
32
+
33
+ def test_each_pair_escape
34
+ opt = { '/ +=&' => '123@*!' }
35
+ assert_equal( {'%2F%20%2B%3D%26' => '123%40%2A%21'}, @foo.each_pair_escape(opt) )
36
+ end
37
+
38
+ def test_url_escape
39
+ assert_equal 'o%2FnGo1QkVEiiN6Bqn%2FfRJtEJwLc%3D', @foo.url_escape('o/nGo1QkVEiiN6Bqn/fRJtEJwLc=')
40
+ end
41
+
42
+ def test_signature
43
+ source = @foo.make_source(@http_method, @url, @options)
44
+ assert_equal( 'o/nGo1QkVEiiN6Bqn/fRJtEJwLc=', @foo.signature("#{@appkey}&", source) )
45
+ end
46
+
47
+ end
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ class OpenQqTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @options = { :appid => 12345,
7
+ :appkey => '228bf094169a40a3bd188ba37ebe8723',
8
+ :env => 'http://119.147.19.43',
9
+ :url => '/v3/user/get_info',
10
+ }
11
+ OpenQq.setup(@options)
12
+ end
13
+
14
+ def teardown
15
+ # Need setup back for the rest test
16
+ OpenQq.setup{|c| c.appid = 12345}
17
+ end
18
+
19
+ def test_OpenQq_is_a_module
20
+ assert_kind_of Module, OpenQq
21
+ end
22
+
23
+ def test_setup
24
+ assert_equal 12345, OpenQq.appid
25
+ assert_equal '228bf094169a40a3bd188ba37ebe8723', OpenQq.appkey
26
+ assert_equal 'http://119.147.19.43', OpenQq.env
27
+ OpenQq.setup do |config|
28
+ config.appid = 'foo'
29
+ end
30
+ assert_equal 'foo', OpenQq.appid
31
+ end
32
+
33
+ # Copy from gateway_test
34
+
35
+ def test_call_get_request_without_params
36
+ assert_equal 'foo', OpenQq.get('/v3/user/get_info').nickname
37
+ end
38
+
39
+ def test_call_get_request_with_params
40
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'json' }
41
+ assert_equal 'foo', OpenQq.get('/v3/user/get_info', params).nickname
42
+ end
43
+
44
+ def test_call_get_request_with_xml_format
45
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'xml' }
46
+ assert_equal '<?xml version="1.0" encoding="UTF-8"?>', OpenQq.get('/v3/user/get_info', params)
47
+ end
48
+
49
+ def test_call_get_request_without_format_param
50
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou }
51
+ assert_equal 'foo', OpenQq.get('/v3/user/get_info', params).nickname
52
+ end
53
+
54
+ def test_call_get_request_with_unknow_format_param
55
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'ruby'}
56
+ assert_equal 'foo', OpenQq.get('/v3/user/get_info', params).nickname
57
+ end
58
+
59
+ def test_call_post_request
60
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
61
+ assert_equal 'foo', OpenQq.post('/v3/user/get_info', params).nickname
62
+ end
63
+
64
+ def test_call_post_request_with_raw_output
65
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
66
+ assert_equal '{ "ret": 0, "is_lost": 0, "nickname": "foo" }', OpenQq.post('/v3/user/get_info', params, :raw => true)
67
+ end
68
+
69
+ def test_open_http_translate_error
70
+ OpenQq.setup do |config|
71
+ config.appid = '123456'
72
+ end
73
+ params = {:openid => '1111', :openkey => '2222', :pf => 'pengyou'}
74
+ assert_equal 2001, OpenQq.get('/v3/user/get_info', params).ret
75
+ end
76
+
77
+ def test_start_wont_change_get_and_post_method
78
+ params = { :openid => 1111, 'openkey' => '2222', :pf => :pengyou, :format => 'json' }
79
+ respond = OpenQq.start('/v3/user/get_info', @options){|r| r.get(params) }
80
+ assert_equal 'foo', OpenQq.get('/v3/user/get_info', params).nickname
81
+ assert_equal 'foo', respond.nickname
82
+ end
83
+
84
+ end
@@ -0,0 +1,57 @@
1
+ begin
2
+ Bundler.setup(:default, :development)
3
+ rescue Bundler::BundlerError => e
4
+ $stderr.puts e.message
5
+ $stderr.puts "Run `bundle install` to install missing gems"
6
+ exit e.status_code
7
+ end
8
+
9
+ require "test/unit"
10
+ require 'minitest/autorun'
11
+
12
+ # Enable turn if it is available
13
+ begin
14
+ require 'turn'
15
+ rescue LoadError
16
+ end
17
+
18
+ require 'open_qq'
19
+
20
+ # fake qq response
21
+ require 'fakeweb'
22
+
23
+ # 测试用例的签名通过腾讯联调工具产生 http://open.qq.com/tools
24
+ # get
25
+ FakeWeb.register_uri(:get, "http://119.147.19.43/v3/user/get_info?appid=12345&sig=CRkzTEcrnfRkNMt9LnVtHjGTocI%3D",
26
+ :body => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }')
27
+
28
+ # get with json format
29
+ FakeWeb.register_uri(:get,
30
+ "http://119.147.19.43/v3/user/get_info?openid=1111&openkey=2222&pf=pengyou&appid=12345&format=json&sig=qjReS%2Fg%2FGM9qStD9gmYNI%2B65nWo%3D",
31
+ :body => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }')
32
+
33
+ # get without format
34
+ FakeWeb.register_uri(:get,
35
+ "http://119.147.19.43/v3/user/get_info?openid=1111&openkey=2222&pf=pengyou&appid=12345&sig=CtBQwNTqB24SnqMnWrSJyh4atg8%3D",
36
+ :body => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }')
37
+
38
+ # get with xml format
39
+ FakeWeb.register_uri(:get,
40
+ "http://119.147.19.43/v3/user/get_info?openid=1111&openkey=2222&pf=pengyou&appid=12345&format=xml&sig=t4WtmJ2pG6RDgGAOfiLWMf8E4%2FM%3D",
41
+ :body => '<?xml version="1.0" encoding="UTF-8"?>')
42
+
43
+ # get with unsupported format
44
+ FakeWeb.register_uri(:get,
45
+ "http://119.147.19.43/v3/user/get_info?openid=1111&openkey=2222&pf=pengyou&appid=12345&format=ruby&sig=egeboQvMq5HbfntN5jdwbQaB6go%3D",
46
+ :body => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }')
47
+
48
+ # post without set format
49
+ FakeWeb.register_uri(:post,
50
+ "http://119.147.19.43/v3/user/get_info",
51
+ :parameters => {:openid => '1111', :openkey => '2222', :pf => 'pengyou', :appid => '12345', :sig => 'CtBQwNTqB24SnqMnWrSJyh4atg8%3D'},
52
+ :body => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }')
53
+
54
+ # http translate error
55
+ FakeWeb.register_uri(:get,
56
+ "http://119.147.19.43/v3/user/get_info?openid=1111&openkey=2222&pf=pengyou&appid=123456&sig=b67OwPW%2BfHFsKwgpu1b82k4dHZU%3D",
57
+ :exception => Net::HTTPError)
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_qq
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - zires
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: turn
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: fakeweb
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: see http://open.qq.com/
111
+ email:
112
+ - zshuaibin@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - lib/generators/open_qq/install_generator.rb
118
+ - lib/generators/templates/open_qq.yml
119
+ - lib/open_qq/error.rb
120
+ - lib/open_qq/gateway.rb
121
+ - lib/open_qq/railtie.rb
122
+ - lib/open_qq/request.rb
123
+ - lib/open_qq/signature.rb
124
+ - lib/open_qq/version.rb
125
+ - lib/open_qq.rb
126
+ - MIT-LICENSE
127
+ - Rakefile
128
+ - README.md
129
+ - test/benchmark.rb
130
+ - test/open_qq/error_test.rb
131
+ - test/open_qq/gateway_test.rb
132
+ - test/open_qq/request_test.rb
133
+ - test/open_qq/signature_test.rb
134
+ - test/open_qq_test.rb
135
+ - test/test_helper.rb
136
+ homepage: https://github.com/zires/open_qq
137
+ licenses: []
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 1.8.19
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: open qq ruby sdk. v3 version
160
+ test_files:
161
+ - test/benchmark.rb
162
+ - test/open_qq/error_test.rb
163
+ - test/open_qq/gateway_test.rb
164
+ - test/open_qq/request_test.rb
165
+ - test/open_qq/signature_test.rb
166
+ - test/open_qq_test.rb
167
+ - test/test_helper.rb
168
+ has_rdoc: