open_taobao 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ require 'autotest/bundler'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in open_taobao.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 ryan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
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.
@@ -0,0 +1,44 @@
1
+ # OpenTaobao
2
+
3
+ Taobao Open Platform client for ruby. Rails3 is supported.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'open_taobao'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install open_taobao
18
+
19
+ Run generate to complete install:
20
+
21
+ $ rails g open_taobao:install
22
+
23
+ this will create taobao.yml in your config/ directory.
24
+ 打开taobao.yml文件,设置你自己的`app_key`, `secret_key`, 淘宝客的`pid`
25
+
26
+ ## Usage
27
+
28
+ 调用`OpenTaobao.get`方法,传入相应参数:
29
+
30
+ hash = OpenTaobao.get(
31
+ :method => "taobao.itemcats.get",
32
+ :fields => "cid,parent_id,name,is_parent",
33
+ :parent_cid => 0
34
+ )
35
+
36
+ 返回内容将自动转化微hash格式。
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ module OpenTaobao
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ def copy_yml_file
7
+ copy_file 'taobao.yml', "config/taobao.yml"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ development:
2
+ app_key: 'test'
3
+ secret_key: 'test'
4
+ pid: 'test'
5
+ endpoint: "http://gw.api.tbsandbox.com/router/rest"
6
+
7
+ test:
8
+ app_key: 'test'
9
+ secret_key: 'test'
10
+ pid: 'test'
11
+ endpoint: "http://gw.api.tbsandbox.com/router/rest"
12
+
13
+ production:
14
+ app_key: 'YOUR-KEY'
15
+ secret_key: 'YOUR-KEY'
16
+ pid: 'YOUR-PID'
17
+ endpoint: "http://gw.api.taobao.com/router/rest"
18
+
@@ -0,0 +1,3 @@
1
+ require 'open_taobao/open_taobao'
2
+
3
+ require 'open_taobao/railtie' if defined? Rails
@@ -0,0 +1,76 @@
1
+ require 'active_support/core_ext'
2
+ require 'digest'
3
+ require 'yaml'
4
+ require 'json'
5
+ require 'patron'
6
+ require 'open_taobao/version'
7
+
8
+ module OpenTaobao
9
+ REQUEST_TIMEOUT = 10
10
+ API_VERSION = '2.0'
11
+ USER_AGENT = "open_taobao-v#{VERSION}"
12
+
13
+ class << self
14
+ attr_accessor :config, :session
15
+
16
+ def load(config_file)
17
+ @config = YAML.load_file(config_file)
18
+ @config = config[Rails.env] if defined? Rails
19
+ apply_settings
20
+ end
21
+
22
+ def apply_settings
23
+ ENV['TAOBAO_API_KEY'] = config['app_key']
24
+ ENV['TAOBAO_SECRET_KEY'] = config['secret_key']
25
+ ENV['TAOBAO_ENDPOINT'] = config['endpoint']
26
+ ENV['TAOBAOKE_PID'] = config['pid']
27
+
28
+ initialize_session
29
+ end
30
+
31
+ def initialize_session
32
+ @session = Patron::Session.new
33
+ #@session.base_url = config['endpoint']
34
+ @session.headers['User-Agent'] = USER_AGENT
35
+ @session.timeout = REQUEST_TIMEOUT
36
+ end
37
+
38
+ def sign(params)
39
+ Digest::MD5::hexdigest("#{config['secret_key']}#{sorted_option_string params}#{config['secret_key']}").upcase
40
+ end
41
+
42
+ def sorted_option_string(options)
43
+ options.map {|k, v| "#{k}#{v}" }.sort.join
44
+ end
45
+
46
+ def full_options(params)
47
+ {
48
+ :timestamp => Time.now.strftime("%F %T"),
49
+ :v => API_VERSION,
50
+ :format => :json,
51
+ :sign_method => :md5,
52
+ :app_key => config['app_key']
53
+ }.merge params
54
+ end
55
+
56
+ def query_string(params)
57
+ params = full_options params
58
+ params[:sign] = sign params
59
+ params.to_query
60
+ end
61
+
62
+ def url(params)
63
+ "%s?%s" % [config['endpoint'], query_string(params)]
64
+ end
65
+
66
+ def parse_result(data)
67
+ JSON.parse(data)
68
+ end
69
+
70
+ def get(params)
71
+ path = url(params)
72
+ parse_result session.get(path).body
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module OpenTaobao
4
+ class Railtie < Rails::Railtie
5
+ generators do
6
+ require 'generators/open_taobao/install_generator'
7
+ end
8
+
9
+ initializer 'load taobao.yml' do
10
+ config_file = Rails.root + 'config/taobao.yml'
11
+ OpenTaobao.load(config_file) if config_file.file?
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module OpenTaobao
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/open_taobao/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["wongyouth"]
6
+ gem.email = ["wongyouth@gmail.com"]
7
+ gem.description = %q{淘宝开放平台ruby版,支持Rails3}
8
+ gem.summary = %q{Open Taobao API for ruby}
9
+ gem.homepage = "http://github.com/wongyouth/open_taobao"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "open_taobao"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = OpenTaobao::VERSION
17
+
18
+ gem.add_dependency "patron"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "autotest"
21
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'tempfile'
4
+
5
+ describe OpenTaobao do
6
+ ENDPOINT = "http://gw.api.tbsandbox.com/router/rest"
7
+
8
+ before(:each) do
9
+ @now = Time.parse('2012-10-29 23:30')
10
+ Time.stub(:now) { @now }
11
+ end
12
+
13
+ let(:config_file) do
14
+ file = Tempfile.new 'taobao'
15
+ File.open file.path, 'w+' do |f|
16
+ f.write <<-EOS.gsub(/^ +/, '')
17
+ app_key: 'test'
18
+ secret_key: 'test'
19
+ pid: 'test'
20
+ endpoint: '#{ENDPOINT}'
21
+ EOS
22
+ end
23
+ file
24
+ end
25
+
26
+ # we only need to load config file here once for all test
27
+ it "should load config file" do
28
+ OpenTaobao.load(config_file.path)
29
+ OpenTaobao.config.should == {
30
+ 'app_key' => 'test',
31
+ 'secret_key' => 'test',
32
+ 'pid' => 'test',
33
+ 'endpoint' => "http://gw.api.tbsandbox.com/router/rest"
34
+ }
35
+ end
36
+
37
+ it "should merge with default options" do
38
+ options = {:foo => 'foo', :bar => 'bar'}
39
+ OpenTaobao.full_options(options).should ==
40
+ {
41
+ :foo => 'foo',
42
+ :bar => 'bar',
43
+ :timestamp => @now.strftime("%F %T"),
44
+ :v => '2.0',
45
+ :format => :json,
46
+ :sign_method => :md5,
47
+ :app_key => 'test'
48
+ }
49
+ end
50
+
51
+ # ref: http://open.taobao.com/doc/detail.htm?spm=0.0.0.30.iamImZ&id=111
52
+ it "should return sorted options string" do
53
+ options = {
54
+ 'timestamp' => '2011-07-01 13:52:03',
55
+ 'v' => '2.0',
56
+ 'app_key' => 'test',
57
+ 'method' => 'taobao.user.get',
58
+ 'sign_method' => 'md5',
59
+ 'format' => 'xml',
60
+ 'nick' => '商家测试帐号17',
61
+ 'fields' => 'nick,location.state,location.city'
62
+ }
63
+ OpenTaobao.sorted_option_string(options).should == "app_keytestfieldsnick,location.state,location.cityformatxmlmethodtaobao.user.getnick商家测试帐号17sign_methodmd5timestamp2011-07-01 13:52:03v2.0"
64
+ end
65
+
66
+ # ref: http://open.taobao.com/doc/detail.htm?spm=0.0.0.30.iamImZ&id=111
67
+ it "should return signature" do
68
+ options = {
69
+ 'timestamp' => '2011-07-01 13:52:03',
70
+ 'v' => '2.0',
71
+ 'app_key' => 'test',
72
+ 'method' => 'taobao.user.get',
73
+ 'sign_method' => 'md5',
74
+ 'format' => 'xml',
75
+ 'nick' => '商家测试帐号17',
76
+ 'fields' => 'nick,location.state,location.city'
77
+ }
78
+ OpenTaobao.sign(options).should == '5029C3055D51555112B60B33000122D5'
79
+ end
80
+
81
+ it "should return query string for url" do
82
+ options = {
83
+ :timestamp => '2011-07-01 13:52:03',
84
+ :method => 'taobao.user.get',
85
+ :format => 'xml',
86
+ :partner_id => 'top-apitools',
87
+ 'nick' => '商家测试帐号17',
88
+ 'fields' => 'nick,location.state,location.city'
89
+ }
90
+ OpenTaobao.query_string(options).should include("sign=")
91
+ OpenTaobao.query_string(options).should include("timestamp=")
92
+ OpenTaobao.query_string(options).should include("method=taobao.user.get")
93
+ OpenTaobao.query_string(options).should include("format=xml")
94
+ OpenTaobao.query_string(options).should include("partner_id=top-apitools")
95
+ end
96
+
97
+ it "should return url with endpoint" do
98
+ options = {
99
+ :timestamp => '2011-07-01 13:52:03',
100
+ :method => 'taobao.user.get',
101
+ :format => 'xml',
102
+ :partner_id => 'top-apitools',
103
+ 'nick' => '商家测试帐号17',
104
+ 'fields' => 'nick,location.state,location.city'
105
+ }
106
+ OpenTaobao.url(options).should start_with(ENDPOINT)
107
+ end
108
+
109
+ it "should parse result data" do
110
+ data = '
111
+ "item_get_response": {
112
+ "item": {
113
+ "item_imgs": {
114
+ "item_img": [
115
+ ]
116
+ }
117
+ }
118
+ }
119
+ '
120
+
121
+ OpenTaobao.parse_result(data).should == {
122
+ "item_get_response" => {
123
+ 'item' => {
124
+ "item_imgs" => { "item_img" => []}
125
+ }
126
+ }
127
+ }
128
+ end
129
+
130
+ it "should support get method" do
131
+ OpenTaobao.initialize_session
132
+ params = {
133
+ :method => "taobao.itemcats.get",
134
+ :fields => "cid,parent_id,name,is_parent",
135
+ :parent_cid => 0
136
+ }
137
+
138
+ OpenTaobao.get(params)['itemcats_get_response']['item_cats']['item_cat'].should be_a(Array)
139
+ end
140
+ end
141
+
142
+ # OpenTaobao.load(File.expand_path('../taobao.yml',__FILE__))
143
+ #
144
+ # def pretty(json)
145
+ # puts JSON.pretty_generate(json)
146
+ # end
147
+ #
148
+ # params = {
149
+ # :method => "taobao.itemcats.get",
150
+ # :fields => "cid,parent_id,name,is_parent",
151
+ # :parent_cid => 0
152
+ # }
153
+ #
154
+ # pretty(OpenTaobao.get(params))
155
+ #
156
+ # params = {
157
+ # :method => "taobao.taobaoke.items.get",
158
+ # :fields => "num_iid,title,nick,pic_url,price,click_url, commission,commission_num,volume",
159
+ # :cid => 30, # 男装
160
+ # :pid => OpenTaobao::PID
161
+ # }
162
+ #
163
+ # pretty(OpenTaobao.get(params))
164
+ #
165
+ # params = {
166
+ # :method => "taobao.item.get",
167
+ # :fields => "prop_img.url,item_img.url,nick",
168
+ # :num_iid => 19276752117
169
+ # }
170
+ #
171
+ # pretty(OpenTaobao.get(params))
172
+ #
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require File.expand_path('../../lib/open_taobao.rb', __FILE__)
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec
6
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_taobao
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wongyouth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: patron
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: rspec
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: autotest
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
+ description: 淘宝开放平台ruby版,支持Rails3
63
+ email:
64
+ - wongyouth@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/generators/open_taobao/install_generator.rb
76
+ - lib/generators/templates/taobao.yml
77
+ - lib/open_taobao.rb
78
+ - lib/open_taobao/open_taobao.rb
79
+ - lib/open_taobao/railtie.rb
80
+ - lib/open_taobao/version.rb
81
+ - open_taobao.gemspec
82
+ - spec/open_taobao_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: http://github.com/wongyouth/open_taobao
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.24
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Open Taobao API for ruby
108
+ test_files:
109
+ - spec/open_taobao_spec.rb
110
+ - spec/spec_helper.rb
111
+ has_rdoc: