open_taobao 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +9 -0
- data/LICENSE +2 -2
- data/README.md +24 -1
- data/lib/open_taobao/open_taobao.rb +90 -17
- data/lib/open_taobao/version.rb +1 -1
- data/lib/open_taobao.rb +7 -0
- data/open_taobao.gemspec +4 -1
- data/spec/open_taobao_spec.rb +13 -1
- metadata +59 -5
data/.travis.yml
ADDED
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2012
|
1
|
+
Copyright (c) 2012 Wang Yongzhi
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
OpenTaobao [![Build Status](https://secure.travis-ci.org/wongyouth/open_taobao.png?branch=master)](https://travis-ci.org/wongyouth/open_taobao)
|
2
|
+
==========
|
2
3
|
|
3
4
|
Taobao Open Platform client for ruby. Rails3 is supported.
|
4
5
|
|
@@ -7,6 +8,7 @@ Taobao Open Platform client for ruby. Rails3 is supported.
|
|
7
8
|
Add this line to your application's Gemfile:
|
8
9
|
|
9
10
|
gem 'open_taobao'
|
11
|
+
gem 'patron' # add this line if you want to use [patron][] instead of Net::HTTP
|
10
12
|
|
11
13
|
And then execute:
|
12
14
|
|
@@ -25,6 +27,8 @@ Run generate to complete install:
|
|
25
27
|
|
26
28
|
## Usage
|
27
29
|
|
30
|
+
### Rails with yaml file configured
|
31
|
+
|
28
32
|
调用`OpenTaobao.get`方法,传入相应参数:
|
29
33
|
|
30
34
|
hash = OpenTaobao.get(
|
@@ -35,6 +39,23 @@ Run generate to complete install:
|
|
35
39
|
|
36
40
|
返回内容将自动转化为hash格式。
|
37
41
|
|
42
|
+
### plain ruby
|
43
|
+
|
44
|
+
OpenTaobao.config = {
|
45
|
+
'app_key' => 'test',
|
46
|
+
'secret_key' => 'test',
|
47
|
+
'pid' => 'test',
|
48
|
+
'endpoint' => "http://gw.api.tbsandbox.com/router/rest"
|
49
|
+
}
|
50
|
+
|
51
|
+
OpenTaobao.initialize_session
|
52
|
+
|
53
|
+
hash = OpenTaobao.get(
|
54
|
+
:method => "taobao.itemcats.get",
|
55
|
+
:fields => "cid,parent_id,name,is_parent",
|
56
|
+
:parent_cid => 0
|
57
|
+
)
|
58
|
+
|
38
59
|
## Contributing
|
39
60
|
|
40
61
|
1. Fork it
|
@@ -42,3 +63,5 @@ Run generate to complete install:
|
|
42
63
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
43
64
|
4. Push to the branch (`git push origin my-new-feature`)
|
44
65
|
5. Create new Pull Request
|
66
|
+
|
67
|
+
[patron]: https://github.com/toland/patron
|
@@ -1,9 +1,25 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2012 Wang Yongzhi
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
7
23
|
|
8
24
|
module OpenTaobao
|
9
25
|
REQUEST_TIMEOUT = 10
|
@@ -13,36 +29,89 @@ module OpenTaobao
|
|
13
29
|
class << self
|
14
30
|
attr_accessor :config, :session
|
15
31
|
|
32
|
+
# Load a yml config, and initialize http session
|
33
|
+
# yml config file content should be:
|
34
|
+
#
|
35
|
+
# app_key: "YOUR APP KEY"
|
36
|
+
# secret_key: "YOUR SECRET KEY"
|
37
|
+
# pid: "YOUR TAOBAOKE pid"
|
38
|
+
# endpoint: "TAOBAO GATEWAY API URL"
|
39
|
+
#
|
16
40
|
def load(config_file)
|
17
41
|
@config = YAML.load_file(config_file)
|
18
42
|
@config = config[Rails.env] if defined? Rails
|
19
|
-
|
43
|
+
check_config_and_export_to_env
|
44
|
+
initialize_session
|
45
|
+
end
|
46
|
+
|
47
|
+
# check config and export all setting to ENV
|
48
|
+
def check_config_and_export_to_env
|
49
|
+
check_config
|
50
|
+
export_config_to_env
|
51
|
+
end
|
52
|
+
|
53
|
+
# check config
|
54
|
+
#
|
55
|
+
# raise exception if config key missed in YAML file
|
56
|
+
def check_config
|
57
|
+
list = []
|
58
|
+
%w(app_key secret_key pid endpoint).map do |k|
|
59
|
+
list << k unless config.has_key? k
|
60
|
+
end
|
61
|
+
|
62
|
+
raise "[#{list.join(', ')}] not included in your yaml file." unless list.empty?
|
20
63
|
end
|
21
64
|
|
22
|
-
|
65
|
+
# setting ENV variables from config
|
66
|
+
#
|
67
|
+
# ENV variables:
|
68
|
+
#
|
69
|
+
# TAOBAO_API_KEY -> config['app_key']
|
70
|
+
# TAOBAO_SECRET_KEY -> config['secret_key']
|
71
|
+
# TAOBAO_ENDPOINT -> config['endpoint']
|
72
|
+
# TAOBAOKE_PID -> config['pid']
|
73
|
+
def export_config_to_env
|
23
74
|
ENV['TAOBAO_API_KEY'] = config['app_key']
|
24
75
|
ENV['TAOBAO_SECRET_KEY'] = config['secret_key']
|
25
76
|
ENV['TAOBAO_ENDPOINT'] = config['endpoint']
|
26
77
|
ENV['TAOBAOKE_PID'] = config['pid']
|
27
|
-
|
28
|
-
initialize_session
|
29
78
|
end
|
30
79
|
|
80
|
+
# Initialize http sesison
|
31
81
|
def initialize_session
|
32
|
-
@session =
|
33
|
-
|
34
|
-
|
35
|
-
|
82
|
+
@session = Faraday.new :url => config['endpoint'] do |builder|
|
83
|
+
begin
|
84
|
+
require 'patron'
|
85
|
+
builder.adapter :patron
|
86
|
+
rescue LoadError
|
87
|
+
builder.adapter :net_http
|
88
|
+
end
|
89
|
+
end
|
36
90
|
end
|
37
91
|
|
92
|
+
# Return request signature with MD5 signature method
|
38
93
|
def sign(params)
|
39
94
|
Digest::MD5::hexdigest("#{config['secret_key']}#{sorted_option_string params}#{config['secret_key']}").upcase
|
40
95
|
end
|
41
96
|
|
97
|
+
# Return sorted request parameter by request key
|
42
98
|
def sorted_option_string(options)
|
43
99
|
options.map {|k, v| "#{k}#{v}" }.sort.join
|
44
100
|
end
|
45
101
|
|
102
|
+
# Merge custom parameters with TAOBAO system parameters.
|
103
|
+
#
|
104
|
+
# System paramters below will be merged.
|
105
|
+
#
|
106
|
+
# timestamp
|
107
|
+
# v
|
108
|
+
# format
|
109
|
+
# sign_method
|
110
|
+
# app_key
|
111
|
+
#
|
112
|
+
# Current Taobao API Version is '2.0'.
|
113
|
+
# <tt>format</tt> should be json.
|
114
|
+
# Only <tt>sign_method</tt> MD5 is supported so far.
|
46
115
|
def full_options(params)
|
47
116
|
{
|
48
117
|
:timestamp => Time.now.strftime("%F %T"),
|
@@ -53,22 +122,26 @@ module OpenTaobao
|
|
53
122
|
}.merge params
|
54
123
|
end
|
55
124
|
|
125
|
+
# Retrun query string with signature.
|
56
126
|
def query_string(params)
|
57
127
|
params = full_options params
|
58
128
|
params[:sign] = sign params
|
59
|
-
params.to_query
|
129
|
+
"?" + params.to_query
|
60
130
|
end
|
61
131
|
|
132
|
+
# Return full url with signature.
|
62
133
|
def url(params)
|
63
|
-
"%s
|
134
|
+
"%s%s" % [config['endpoint'], query_string(params)]
|
64
135
|
end
|
65
136
|
|
137
|
+
# Return a parsed JSON object.
|
66
138
|
def parse_result(data)
|
67
139
|
JSON.parse(data)
|
68
140
|
end
|
69
141
|
|
142
|
+
# Return response in JSON format
|
70
143
|
def get(params)
|
71
|
-
path =
|
144
|
+
path = query_string(params)
|
72
145
|
parse_result session.get(path).body
|
73
146
|
end
|
74
147
|
end
|
data/lib/open_taobao/version.rb
CHANGED
data/lib/open_taobao.rb
CHANGED
data/open_taobao.gemspec
CHANGED
@@ -15,7 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = OpenTaobao::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency "
|
18
|
+
gem.add_dependency "activesupport"
|
19
|
+
gem.add_dependency "json"
|
20
|
+
gem.add_dependency "faraday"
|
19
21
|
gem.add_development_dependency "rspec"
|
20
22
|
gem.add_development_dependency "autotest"
|
23
|
+
gem.add_development_dependency "rake"
|
21
24
|
end
|
data/spec/open_taobao_spec.rb
CHANGED
@@ -11,7 +11,7 @@ describe OpenTaobao do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
let(:config_file) do
|
14
|
-
file = Tempfile.new 'taobao'
|
14
|
+
file = Tempfile.new 'taobao.yml'
|
15
15
|
File.open file.path, 'w+' do |f|
|
16
16
|
f.write <<-EOS.gsub(/^ +/, '')
|
17
17
|
app_key: 'test'
|
@@ -34,6 +34,18 @@ describe OpenTaobao do
|
|
34
34
|
}
|
35
35
|
end
|
36
36
|
|
37
|
+
it "should raise exception if config yaml does not include correct keys" do
|
38
|
+
OpenTaobao.config = {
|
39
|
+
'app_key' => 'test',
|
40
|
+
'secret_key' => 'test',
|
41
|
+
'endpoint' => "http://gw.api.tbsandbox.com/router/rest"
|
42
|
+
}
|
43
|
+
|
44
|
+
expect {
|
45
|
+
OpenTaobao.check_config
|
46
|
+
}.to raise_error('[pid] not included in your yaml file.')
|
47
|
+
end
|
48
|
+
|
37
49
|
it "should merge with default options" do
|
38
50
|
options = {:foo => 'foo', :bar => 'bar'}
|
39
51
|
OpenTaobao.full_options(options).should ==
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_taobao
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,42 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
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: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
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: faraday
|
16
48
|
requirement: !ruby/object:Gem::Requirement
|
17
49
|
none: false
|
18
50
|
requirements:
|
@@ -59,6 +91,22 @@ dependencies:
|
|
59
91
|
- - ! '>='
|
60
92
|
- !ruby/object:Gem::Version
|
61
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rake
|
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'
|
62
110
|
description: 淘宝开放平台ruby版,支持Rails3
|
63
111
|
email:
|
64
112
|
- wongyouth@gmail.com
|
@@ -68,6 +116,7 @@ extra_rdoc_files: []
|
|
68
116
|
files:
|
69
117
|
- .gitignore
|
70
118
|
- .rspec
|
119
|
+
- .travis.yml
|
71
120
|
- Gemfile
|
72
121
|
- LICENSE
|
73
122
|
- README.md
|
@@ -93,19 +142,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
142
|
- - ! '>='
|
94
143
|
- !ruby/object:Gem::Version
|
95
144
|
version: '0'
|
145
|
+
segments:
|
146
|
+
- 0
|
147
|
+
hash: -1746376069650641682
|
96
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
149
|
none: false
|
98
150
|
requirements:
|
99
151
|
- - ! '>='
|
100
152
|
- !ruby/object:Gem::Version
|
101
153
|
version: '0'
|
154
|
+
segments:
|
155
|
+
- 0
|
156
|
+
hash: -1746376069650641682
|
102
157
|
requirements: []
|
103
158
|
rubyforge_project:
|
104
|
-
rubygems_version: 1.8.
|
159
|
+
rubygems_version: 1.8.23
|
105
160
|
signing_key:
|
106
161
|
specification_version: 3
|
107
162
|
summary: Open Taobao API for ruby
|
108
163
|
test_files:
|
109
164
|
- spec/open_taobao_spec.rb
|
110
165
|
- spec/spec_helper.rb
|
111
|
-
has_rdoc:
|