open_taobao 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  require 'autotest/bundler'
2
+ --debug
@@ -1,9 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - "1.8.7"
4
3
  - "1.9.2"
5
4
  - "1.9.3"
6
- - rbx-18mode
7
- - rbx-19mode
5
+ - "2.0.0"
8
6
  # uncomment this line if your project needs to run something other than `rake`:
9
7
  script: bundle exec rspec spec
@@ -1,3 +1,10 @@
1
+ ## 2013-08-25 - v0.2.0 released.
2
+
3
+ * Besides HTTP `get`, `post` is supported.
4
+ * `get!`, `post!` added, these methods will raise OpenTaobao::Error when get an `error_response`.
5
+ * Remove `json` dependency in favor of `multi_json`.
6
+ * Remove `active_support` dependency, add a ActiveSupport compatible Hash hack to converted to http parameters
7
+
1
8
  ## 2013-05-12
2
9
 
3
10
  pid is not required in config file, but will be loaded to config for back compatibility.
data/README.md CHANGED
@@ -38,7 +38,7 @@ Open the file and configure it with your taobao info.
38
38
 
39
39
  Notice: `app_key`, `secret_key`, `endpoint` must be included in your YAML file, otherwise OpenTaobao.load will fail.
40
40
 
41
- call `OpenTaobao.get`,with taobao parameters:
41
+ call `OpenTaobao.get` or `OpenTaoboa.post`,with taobao parameters:
42
42
 
43
43
  hash = OpenTaobao.get(
44
44
  :method => "taobao.itemcats.get",
@@ -48,6 +48,8 @@ call `OpenTaobao.get`,with taobao parameters:
48
48
 
49
49
  The return data will be converted to a Hash automatically.
50
50
 
51
+ Also `OpenTaobao.get!` and `OpenTaobao.post!` are avaliable, which will raise an `OpenTaobao::Error` if a `error_response` receieved.
52
+
51
53
  ### plain ruby
52
54
 
53
55
  OpenTaobao.config = {
@@ -1,10 +1,10 @@
1
- require 'active_support/core_ext/object'
2
1
  require 'digest'
3
2
  require 'faraday'
4
- require 'json'
5
- require 'open_taobao/version'
3
+ require 'multi_json'
6
4
  require 'yaml'
7
5
 
6
+ require 'open_taobao/version'
7
+ require 'open_taobao/hash_ext'
8
8
  require 'open_taobao/open_taobao'
9
9
 
10
10
  require 'open_taobao/railtie' if defined? Rails
@@ -0,0 +1,7 @@
1
+ require 'cgi'
2
+
3
+ class Hash
4
+ def to_query
5
+ self.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
6
+ end
7
+ end
@@ -26,6 +26,8 @@ module OpenTaobao
26
26
  API_VERSION = '2.0'
27
27
  USER_AGENT = "open_taobao-v#{VERSION}"
28
28
 
29
+ class Error < StandardError; end
30
+
29
31
  class << self
30
32
  attr_accessor :config, :session
31
33
 
@@ -93,8 +95,9 @@ module OpenTaobao
93
95
  Digest::MD5::hexdigest(wrap_with_secret sorted_option_string(params)).upcase
94
96
  end
95
97
 
98
+ # wrapped with secret_key
96
99
  def wrap_with_secret(s)
97
- config['secret_key'] + s + config['secret_key']
100
+ "#{config['secret_key']}#{s}#{config['secret_key']}"
98
101
  end
99
102
 
100
103
  # Return sorted request parameter by request key
@@ -125,11 +128,15 @@ module OpenTaobao
125
128
  }.merge params
126
129
  end
127
130
 
128
- # Retrun query string with signature.
129
- def query_string(params)
131
+ def query_hash(params)
130
132
  params = full_options params
131
133
  params[:sign] = sign params
132
- "?" + params.to_query
134
+ params
135
+ end
136
+
137
+ # Retrun query string with signature.
138
+ def query_string(params)
139
+ "?" + query_hash(params).to_query
133
140
  end
134
141
 
135
142
  # Return full url with signature.
@@ -139,14 +146,35 @@ module OpenTaobao
139
146
 
140
147
  # Return a parsed JSON object.
141
148
  def parse_result(data)
142
- JSON.parse(data)
149
+ MultiJson.decode(data)
143
150
  end
144
151
 
145
- # Return response in JSON format
152
+ # Request by get method and return result in JSON format
146
153
  def get(params)
147
154
  path = query_string(params)
148
155
  parse_result session.get(path).body
149
156
  end
157
+
158
+ # Request by get method and return result in JSON format
159
+ # Raise OpenTaobao::Error if returned with error_response
160
+ def get!(params)
161
+ response = get params
162
+ raise Error.new(response['error_response']) if response.has_key?('error_response')
163
+ response
164
+ end
165
+
166
+ # Request by post method and return result in JSON format
167
+ def post(params)
168
+ parse_result session.post('', query_hash(params).to_query).body
169
+ end
170
+
171
+ # Request by post method and return result in JSON format
172
+ # Raise OpenTaobao::Error if returned with error_response
173
+ def post!(params)
174
+ response = post params
175
+ raise Error.new(response['error_response']) if response.has_key?('error_response')
176
+ response
177
+ end
150
178
  end
151
179
  end
152
180
 
@@ -1,3 +1,3 @@
1
1
  module OpenTaobao
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -15,10 +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 "activesupport"
19
- gem.add_dependency "json"
18
+ gem.add_dependency "multi_json"
20
19
  gem.add_dependency "faraday"
21
20
  gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "debugger"
22
22
  gem.add_development_dependency "autotest"
23
23
  gem.add_development_dependency "rake"
24
24
  end
@@ -146,6 +146,65 @@ describe OpenTaobao do
146
146
 
147
147
  OpenTaobao.get(params)['itemcats_get_response']['item_cats']['item_cat'].should be_a(Array)
148
148
  end
149
+
150
+ it "should support get! method" do
151
+ OpenTaobao.initialize_session
152
+ params = {
153
+ :method => "taobao.itemcats.get",
154
+ :fields => "cid,parent_id,name,is_parent",
155
+ :parent_cid => 0
156
+ }
157
+
158
+ OpenTaobao.get!(params)['itemcats_get_response']['item_cats']['item_cat'].should be_a(Array)
159
+ end
160
+
161
+ it "should raise error by get! method" do
162
+ OpenTaobao.initialize_session
163
+ params = {
164
+ :method => "taobao.itemcats.no-method",
165
+ :fields => "cid,parent_id,name,is_parent",
166
+ :parent_cid => 0
167
+ }
168
+
169
+ expect {
170
+ OpenTaobao.get!(params)
171
+ }.to raise_error OpenTaobao::Error
172
+ end
173
+
174
+ it "should support post method" do
175
+ OpenTaobao.initialize_session
176
+ params = {
177
+ :method => "taobao.itemcats.get",
178
+ :fields => "cid,parent_id,name,is_parent",
179
+ :parent_cid => 0
180
+ }
181
+
182
+ OpenTaobao.post(params)['itemcats_get_response']['item_cats']['item_cat'].should be_a(Array)
183
+ end
184
+
185
+ it "should support post! method" do
186
+ OpenTaobao.initialize_session
187
+ params = {
188
+ :method => "taobao.itemcats.get",
189
+ :fields => "cid,parent_id,name,is_parent",
190
+ :parent_cid => 0
191
+ }
192
+
193
+ OpenTaobao.post!(params)['itemcats_get_response']['item_cats']['item_cat'].should be_a(Array)
194
+ end
195
+
196
+ it "should raise error by post! method" do
197
+ OpenTaobao.initialize_session
198
+ params = {
199
+ :method => "taobao.itemcats.no-method",
200
+ :fields => "cid,parent_id,name,is_parent",
201
+ :parent_cid => 0
202
+ }
203
+
204
+ expect {
205
+ OpenTaobao.post!(params)
206
+ }.to raise_error OpenTaobao::Error
207
+ end
149
208
  end
150
209
 
151
210
  # OpenTaobao.load(File.expand_path('../taobao.yml',__FILE__))
metadata CHANGED
@@ -1,97 +1,110 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_taobao
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - wongyouth
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-05-12 00:00:00.000000000 Z
12
+ date: 2013-08-24 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- name: activesupport
15
+ name: multi_json
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
- name: json
31
+ name: faraday
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :runtime
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
- name: faraday
47
+ name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
- type: :runtime
54
+ type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
- name: rspec
63
+ name: debugger
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  - !ruby/object:Gem::Dependency
70
79
  name: autotest
71
80
  requirement: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
- - - '>='
83
+ - - ! '>='
74
84
  - !ruby/object:Gem::Version
75
85
  version: '0'
76
86
  type: :development
77
87
  prerelease: false
78
88
  version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
79
90
  requirements:
80
- - - '>='
91
+ - - ! '>='
81
92
  - !ruby/object:Gem::Version
82
93
  version: '0'
83
94
  - !ruby/object:Gem::Dependency
84
95
  name: rake
85
96
  requirement: !ruby/object:Gem::Requirement
97
+ none: false
86
98
  requirements:
87
- - - '>='
99
+ - - ! '>='
88
100
  - !ruby/object:Gem::Version
89
101
  version: '0'
90
102
  type: :development
91
103
  prerelease: false
92
104
  version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
93
106
  requirements:
94
- - - '>='
107
+ - - ! '>='
95
108
  - !ruby/object:Gem::Version
96
109
  version: '0'
97
110
  description: 淘宝开放平台ruby版,支持Rails3~
@@ -112,6 +125,7 @@ files:
112
125
  - lib/generators/open_taobao/install_generator.rb
113
126
  - lib/generators/templates/taobao.yml
114
127
  - lib/open_taobao.rb
128
+ - lib/open_taobao/hash_ext.rb
115
129
  - lib/open_taobao/open_taobao.rb
116
130
  - lib/open_taobao/railtie.rb
117
131
  - lib/open_taobao/version.rb
@@ -120,27 +134,29 @@ files:
120
134
  - spec/spec_helper.rb
121
135
  homepage: http://github.com/wongyouth/open_taobao
122
136
  licenses: []
123
- metadata: {}
124
137
  post_install_message:
125
138
  rdoc_options: []
126
139
  require_paths:
127
140
  - lib
128
141
  required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
129
143
  requirements:
130
- - - '>='
144
+ - - ! '>='
131
145
  - !ruby/object:Gem::Version
132
146
  version: '0'
133
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
134
149
  requirements:
135
- - - '>='
150
+ - - ! '>='
136
151
  - !ruby/object:Gem::Version
137
152
  version: '0'
138
153
  requirements: []
139
154
  rubyforge_project:
140
- rubygems_version: 2.0.0
155
+ rubygems_version: 1.8.25
141
156
  signing_key:
142
- specification_version: 4
157
+ specification_version: 3
143
158
  summary: Open Taobao API for ruby
144
159
  test_files:
145
160
  - spec/open_taobao_spec.rb
146
161
  - spec/spec_helper.rb
162
+ has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: a0d2cd1f2224327805d2a53d744fa6a2e48bde17
4
- data.tar.gz: 902d65b7f744defc6380de2ac0f66037a20d17c4
5
- SHA512:
6
- metadata.gz: 281732d45349bbaf20424eb940c272d7494571d19f74605328e95dc37342040b4107c10e7d8868d30cb1136002f53aef10706d7c09f645502f2fedea8cc7873f
7
- data.tar.gz: aefdde07064cc319103cf3203e371c233c8e142a3db835f800c6846a28aa3d0aaf0b137566531b1a67174000c61bbc4a96fc9d7a41f7416f8bf072226cd9303d