topsdk 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +24 -21
- data/init.rb +2 -0
- data/lib/config/taobao.yml +21 -0
- data/lib/topsdk/client.rb +56 -44
- data/lib/topsdk/version.rb +1 -1
- data/lib/topsdk.rb +50 -2
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -8,35 +8,38 @@ The plugin enable easy integration with the [Taobao Open Platform]: http://open.
|
|
8
8
|
|
9
9
|
Create a config/taobao.yml
|
10
10
|
|
11
|
+
default: &default
|
12
|
+
api_key: 12012322
|
13
|
+
secret_key: sandbox2a5bea25d1bc06ccd23123121daf
|
14
|
+
auth_url: https://oauth.tbsandbox.com/authorize
|
15
|
+
token_url: https://oauth.tbsandbox.com/token
|
16
|
+
rest_url: http://gw.api.tbsandbox.com/router/rest
|
17
|
+
callback_url: http://0.0.0.0:3000/auth/callback
|
18
|
+
|
11
19
|
development:
|
12
|
-
|
13
|
-
secret_key: your_secret_key
|
14
|
-
format: json
|
15
|
-
host: sandbox
|
20
|
+
<<: *default
|
16
21
|
|
17
22
|
test:
|
18
|
-
|
19
|
-
secret_key:
|
20
|
-
format: xml
|
21
|
-
host: sandbox
|
23
|
+
<<: *default
|
22
24
|
|
23
25
|
production:
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
secret_key: 42ab2d12a5bea25d1bc06ccd23123121daf
|
27
|
+
auth_url: https://oauth.taobao.com/authorize
|
28
|
+
token_url: https://oauth.taobao.com/token
|
29
|
+
rest_url: http://gw.api.taobao.com/router/rest
|
30
|
+
callback_url: http://yourdomain.com/auth/callback
|
31
|
+
<<: *default
|
29
32
|
|
30
33
|
== Usage
|
31
34
|
|
35
|
+
Topsdk.authorize_url
|
36
|
+
# => "https://oauth.tbsandbox.com/authorize?response_type=code&client_id=12012322&redirect_uri=http://0.0.0.0:3000/auth/callback"
|
37
|
+
|
38
|
+
session = Topsdk.token('lv5W8xgdDc6pf8DzEWIe2Zgt2928')
|
32
39
|
params = {
|
33
|
-
:session =>
|
34
|
-
:method =>'taobao.
|
35
|
-
:fields => '
|
36
|
-
:buyer_nick => 'zhangsan',
|
37
|
-
:start_created => '2010-09-27 00:00:00',
|
38
|
-
:end_date => '2011-09-27 23:00:00',
|
39
|
-
:page_size => 2
|
40
|
+
:session => session,
|
41
|
+
:method =>'taobao.user.get',
|
42
|
+
:fields => 'uid,nick,email',
|
40
43
|
}
|
41
44
|
Topsdk.get_with(params)
|
42
|
-
# => {"
|
45
|
+
# => {"user"=>{"email"=>"null@tbminisandbox.com", "nick"=>"nill", "uid"=>"00000000000"}}
|
data/init.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
default: &default
|
2
|
+
api_key: 12012322
|
3
|
+
secret_key: sandbox2a5bea25d1bc06ccd23123121daf
|
4
|
+
auth_url: https://oauth.tbsandbox.com/authorize
|
5
|
+
token_url: https://oauth.tbsandbox.com/token
|
6
|
+
rest_url: http://gw.api.tbsandbox.com/router/rest
|
7
|
+
callback_url: http://0.0.0.0:3000/auth/callback
|
8
|
+
|
9
|
+
development:
|
10
|
+
<<: *default
|
11
|
+
|
12
|
+
test:
|
13
|
+
<<: *default
|
14
|
+
|
15
|
+
production:
|
16
|
+
secret_key: 42ab2d12a5bea25d1bc06ccd23123121daf
|
17
|
+
auth_url: https://oauth.taobao.com/authorize
|
18
|
+
token_url: https://oauth.taobao.com/token
|
19
|
+
rest_url: http://gw.api.taobao.com/router/rest
|
20
|
+
callback_url: http://yourdomain.com/auth/callback
|
21
|
+
<<: *default
|
data/lib/topsdk/client.rb
CHANGED
@@ -1,51 +1,63 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'nestful'
|
4
|
-
rescue LoadError
|
5
|
-
puts "The Nestful gem is not available.\nIf you ran this command from a git checkout " \
|
6
|
-
"of Rails, please make sure nestful is installed. \n "
|
7
|
-
exit
|
8
|
-
end
|
2
|
+
require 'nestful'
|
9
3
|
require 'digest/md5'
|
4
|
+
require 'pp'
|
10
5
|
|
11
6
|
module Topsdk
|
12
7
|
class Client
|
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
|
-
|
8
|
+
DEBUG = false
|
9
|
+
|
10
|
+
def initialize(params={})
|
11
|
+
@params = { # 淘宝参数
|
12
|
+
:app_key => ENV['TAOBAO_APP_KEY'],
|
13
|
+
:format => :json,
|
14
|
+
:v => '2.0',
|
15
|
+
:timestamp => timestamp
|
16
|
+
}
|
17
|
+
@params.merge!(params.clone) unless params.empty? # 參數合併
|
18
|
+
@params[:sign] = generate_sign(@params)
|
19
|
+
pp @params if DEBUG
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(url=ENV['TAOBAO_REST_URL'])
|
23
|
+
@result = Nestful::Request.new(url, ({:method => :get, :format => :json, :params => @params})).execute
|
24
|
+
parse
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(url=ENV['TAOBAO_REST_URL'])
|
28
|
+
@result = Nestful::Request.new(url, ({:method => :post, :format => :form, :params => @params})).execute
|
29
|
+
@result = JSON.parse(@result)
|
30
|
+
parse
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse # 簡單解析一下
|
34
|
+
return false unless @result.is_a?(Hash)
|
35
|
+
case
|
36
|
+
when @result.has_key?(response_key)
|
37
|
+
result = @result[response_key]
|
38
|
+
result[root_key] if result.has_key?(root_key)
|
39
|
+
when @result.has_key?('error_response')
|
40
|
+
@result['error_response']['msg']
|
41
|
+
else
|
42
|
+
@result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def generate_sign(params={})
|
47
|
+
rand = ENV['TAOBAO_APP_SECRET'] + params.sort.flatten.join
|
48
|
+
Digest::MD5.hexdigest(rand).upcase! # 數字簽名
|
49
|
+
end
|
50
|
+
|
51
|
+
def response_key # 操作結果鍵
|
52
|
+
@params[:method][7..-1].gsub(/\./, '_') + "_response" unless @params[:method].nil?
|
53
|
+
end
|
54
|
+
|
55
|
+
def root_key # 獲取對象的鍵
|
56
|
+
response_key.split('_')[0] unless @params[:method].nil?
|
57
|
+
end
|
58
|
+
|
59
|
+
def timestamp # 时间戳
|
60
|
+
Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
61
|
+
end
|
50
62
|
end
|
51
63
|
end
|
data/lib/topsdk/version.rb
CHANGED
data/lib/topsdk.rb
CHANGED
@@ -1,14 +1,62 @@
|
|
1
1
|
require "topsdk/version"
|
2
2
|
require "topsdk/client"
|
3
3
|
|
4
|
+
RAILS_ENV = PADRINO_ENV if defined?(PADRINO_ENV)
|
5
|
+
|
6
|
+
# 賦值
|
7
|
+
def apply_settings
|
8
|
+
@settings = YAML.load_file(config_file)
|
9
|
+
@settings = defined?(RAILS_ENV) ? @settings[RAILS_ENV] : @settings['development']
|
10
|
+
ENV['TAOBAO_APP_KEY'] = @settings['api_key'].to_s
|
11
|
+
ENV['TAOBAO_APP_SECRET'] = @settings['secret_key']
|
12
|
+
ENV['TAOBAO_AUTH_URL'] = @settings['auth_url']
|
13
|
+
ENV['TAOBAO_TOKEN_URL'] = @settings['token_url']
|
14
|
+
ENV['CALLBACK_URL'] = @settings['callback_url']
|
15
|
+
ENV['TAOBAO_REST_URL'] = @settings['rest_url']
|
16
|
+
end
|
17
|
+
|
18
|
+
def config_file
|
19
|
+
# 解析配置文件
|
20
|
+
yml_file = File.expand_path(File.join('.', 'config', 'taobao.yml'))
|
21
|
+
unless File.exist?(yml_file)
|
22
|
+
File.expand_path(File.join('config', 'taobao.yml'), File.dirname(__FILE__))
|
23
|
+
else
|
24
|
+
yml_file
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
apply_settings
|
29
|
+
|
4
30
|
module Topsdk
|
5
31
|
class << self
|
32
|
+
|
6
33
|
def get_with(joined_params = {})
|
7
|
-
Client.new(joined_params).
|
34
|
+
Client.new(joined_params).get
|
8
35
|
end
|
9
36
|
|
10
37
|
def post_with(joined_params = {})
|
11
|
-
Client
|
38
|
+
Client.new(joined_params).post
|
39
|
+
end
|
40
|
+
|
41
|
+
def authorize_url
|
42
|
+
"#{ENV['TAOBAO_AUTH_URL']}?response_type=code&client_id=#{ENV['TAOBAO_APP_KEY']}&redirect_uri=#{ENV['CALLBACK_URL']}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def token(code)
|
46
|
+
joined_params = token_params(code)
|
47
|
+
res = Client.new(joined_params).post(ENV['TAOBAO_TOKEN_URL'])
|
48
|
+
res['access_token']
|
12
49
|
end
|
50
|
+
|
51
|
+
def token_params(code)
|
52
|
+
{ # 淘宝参数
|
53
|
+
:grant_type => 'authorization_code',
|
54
|
+
:code => code,
|
55
|
+
:client_id => ENV['TAOBAO_APP_KEY'],
|
56
|
+
:client_secret => ENV['TAOBAO_APP_SECRET'],
|
57
|
+
:redirect_uri => ENV['CALLBACK_URL'],
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
13
61
|
end
|
14
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: topsdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-30 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nestful
|
16
|
-
requirement: &
|
16
|
+
requirement: &70339835765280 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70339835765280
|
25
25
|
description: Simple Taobao API client for Ruby
|
26
26
|
email:
|
27
27
|
- howl.wong@gmail.com
|
@@ -34,6 +34,8 @@ files:
|
|
34
34
|
- Gemfile
|
35
35
|
- README.rdoc
|
36
36
|
- Rakefile
|
37
|
+
- init.rb
|
38
|
+
- lib/config/taobao.yml
|
37
39
|
- lib/topsdk.rb
|
38
40
|
- lib/topsdk/client.rb
|
39
41
|
- lib/topsdk/version.rb
|