taobao_fu_reload 1.1

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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.markdown ADDED
@@ -0,0 +1,73 @@
1
+ ### About TaobaoFu
2
+
3
+ **TaobaoFu** is a Rails plugin (supports Rails 3.0.0 or above) as an unofficial Ruby SDK for the Taobao Open Platform(http://open.taobao.com/).
4
+
5
+
6
+ ### Compatibility
7
+
8
+ TaobaoFu is developed against Ruby 1.8.6, 1.8.7, 1.9.1, 1.9.2
9
+
10
+
11
+ ### Installation
12
+
13
+ For Rails applications add the gem dependencies to your Gemfile:
14
+
15
+ gem "taobao_fu_reload" # Required
16
+
17
+ Setup of access the TOP API is handled by using automatic configuration with a config/taobao.yml. You can generate a taobao.yml via the following command:
18
+
19
+ > rails generate taobao_fu:install
20
+
21
+ An example configuration is as follows, with the default values showing:
22
+ config/taobao.yml:
23
+
24
+ defaults: &defaults
25
+ app_key: # YOUR_APP_KEY
26
+ secret_key: # YOUR_APP_SECRET_TOKEN
27
+ session: # TOP-Session
28
+ taobaoke_pid: # YOUR_TAOBAOKE_PID
29
+ taobaoke_nick: # YOUR_TAOBAOKE_NICKNAME
30
+
31
+ development:
32
+ <<: *defaults
33
+ is_sandbox: false # If true, it will work under the sandbox environment(tbsandbox.com, not taobao.com).
34
+ use_curl: false # If true, it will use gem "patron" as the REST client.
35
+
36
+ test:
37
+ <<: *defaults
38
+ is_sandbox: ture
39
+ use_curl: false
40
+
41
+ production:
42
+ <<: *defaults
43
+ is_sandbox: false
44
+ use_curl: true
45
+
46
+
47
+ ### Example
48
+
49
+ Here is an example shows you how to get some information of a commodity from taobao.com by using the TaobaoFu.get method.
50
+
51
+ > cd your_rails_app
52
+ > rails console
53
+ > TaobaoFu.get(:method => 'taobao.item.get',
54
+ :nick => 'simul官方旗舰店',
55
+ :iid => '3b54462a34e2ed84c330dc3e5fb4a94f',
56
+ :fields => 'title, price')
57
+
58
+ There are four methods mapping HTTP verbs(GET/POST/PUT/DELETE).
59
+
60
+ TaobaoFu.get #(Available)
61
+ TaobaoFu.post
62
+ TaobaoFu.update
63
+ TaobaoFu.delete
64
+
65
+ There is also an additional method is using for switching between sandbox and production environment of TOP. For instance:
66
+
67
+ TaobaoFu.switch_to(TaobaoFu::SANDBOX) # switching to sandbox(tbsandbox.com)
68
+ TaobaoFu.switch_to(TaobaoFu::PRODBOX) # back to production environment(taobao.com)
69
+
70
+
71
+ ### Copyright
72
+
73
+ Copyright (c) 2010 why404(why404#gmail), reloaded 2012 NIOTEAM, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+
4
+ PKG_FILES = FileList[
5
+ '[a-zA-Z-]*',
6
+ 'generators/**/*',
7
+ 'lib/**/*'
8
+ ]
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = "taobao_fu"
12
+ s.version = "1.0.0.beta5"
13
+ s.author = "why404"
14
+ s.email = "why404@gmail.com"
15
+ s.homepage = "http://rubygems.org/gems/taobao_fu"
16
+ s.platform = Gem::Platform::RUBY
17
+ s.summary = "Ruby SDK for the Taobao Open Platform"
18
+ s.description = "TaobaoFu is a Ruby gem (also can be a Rails plugin, supports Rails 3.0.0 or above) as an unofficial Ruby SDK for the Taobao Open Platform(http://open.taobao.com/)."
19
+ s.files = PKG_FILES.to_a
20
+ s.require_path = "lib"
21
+ s.has_rdoc = false
22
+ s.extra_rdoc_files = ["README.markdown"]
23
+ s.add_dependency("crack", ">= 0.1.7")
24
+ end
25
+
26
+ Rake::GemPackageTask.new(spec) do |pkg|
27
+ pkg.gem_spec = spec
28
+ end
@@ -0,0 +1,3 @@
1
+ To copy a TaobaoFu initializer to your Rails App, with some configuration values, just do:
2
+
3
+ rails generate taobao_fu:install
@@ -0,0 +1,13 @@
1
+ module TaobaoFu
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy TaobaoFu default files"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ class_option :template_engine
7
+
8
+ def copy_config
9
+ directory 'config'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'taobao_fu'
2
+
3
+ require 'yaml'
4
+ YAML::ENGINE.yamler = 'syck'
5
+
6
+ config_file = File.join(Rails.root, "config", "taobao.yml")
7
+ TaobaoFu.load(config_file) if FileTest::exists?(config_file)
@@ -0,0 +1,21 @@
1
+ defaults: &defaults
2
+ app_key: # YOUR_APP_KEY
3
+ secret_key: # YOUR_APP_SECRET_TOKEN
4
+ session: # TOP-Session
5
+ taobaoke_pid: # YOUR_TAOBAOKE_PID
6
+ taobaoke_nick: # YOUR_TAOBAOKE_NICKNAME
7
+
8
+ development:
9
+ <<: *defaults
10
+ is_sandbox: false # If true, it will work under the sandbox environment(tbsandbox.com, not taobao.com).
11
+ use_curl: false # If true, it will use gem "patron" as the REST client.
12
+
13
+ test:
14
+ <<: *defaults
15
+ is_sandbox: ture
16
+ use_curl: false
17
+
18
+ production:
19
+ <<: *defaults
20
+ is_sandbox: false
21
+ use_curl: true
data/lib/rest.rb ADDED
@@ -0,0 +1,95 @@
1
+ require "uri"
2
+ require "net/http"
3
+
4
+ module TaobaoFu
5
+ module Rest
6
+ class << self
7
+ def get(url, hashed_vars)
8
+ res = request(url, 'GET', hashed_vars)
9
+ process_result(res, url)
10
+ end
11
+
12
+ def post(url, hashed_vars)
13
+ res = request(url, 'POST', hashed_vars)
14
+ process_result(res, url)
15
+ end
16
+
17
+ def put(url, hashed_vars)
18
+ res = request(url, 'PUT', hashed_vars)
19
+ process_result(res, url)
20
+ end
21
+
22
+ def delete(url, hashed_vars)
23
+ res = request(url, 'DELETE', hashed_vars)
24
+ process_result(res, url)
25
+ end
26
+
27
+ protected
28
+
29
+ def request(url, method=nil, params = {})
30
+ if !url || url.length < 1
31
+ raise ArgumentError, 'Invalid url parameter'
32
+ end
33
+ if method && !['GET', 'POST', 'DELETE', 'PUT'].include?(method)
34
+ raise NotImplementedError, 'HTTP %s not implemented' % method
35
+ end
36
+
37
+ if method && method == 'GET'
38
+ url = build_get_uri(url, params)
39
+ end
40
+ uri = URI.parse(url)
41
+
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+
44
+ if method && method == 'GET'
45
+ req = Net::HTTP::Get.new(uri.request_uri)
46
+ elsif method && method == 'DELETE'
47
+ req = Net::HTTP::Delete.new(uri.request_uri)
48
+ elsif method && method == 'PUT'
49
+ req = Net::HTTP::Put.new(uri.request_uri)
50
+ req.set_form_data(params)
51
+ else
52
+ req = Net::HTTP::Post.new(uri.request_uri)
53
+ req.set_form_data(params)
54
+ end
55
+
56
+ http.request(req)
57
+ end
58
+
59
+ def build_get_uri(uri, params)
60
+ if params && params.length > 0
61
+ uri += '?' unless uri.include?('?')
62
+ uri += urlencode(params)
63
+ end
64
+ URI.escape(uri)
65
+ end
66
+
67
+ def urlencode(params)
68
+ params.to_a.collect! { |k, v| "#{k.to_s}=#{v.to_s}" }.join("&")
69
+ end
70
+
71
+ def process_result(res, raw_url)
72
+ if res.code =~ /\A2\d{2}\z/
73
+ res.body
74
+ elsif %w(301 302 303).include? res.code
75
+ url = res.header['Location']
76
+ if url !~ /^http/
77
+ uri = URI.parse(raw_url)
78
+ uri.path = "/#{url}".squeeze('/')
79
+ url = uri.to_s
80
+ end
81
+ raise RuntimeError, "Redirect #{url}"
82
+ elsif res.code == "304"
83
+ raise RuntimeError, "NotModified #{res}"
84
+ elsif res.code == "401"
85
+ raise RuntimeError, "Unauthorized #{res}"
86
+ elsif res.code == "404"
87
+ raise RuntimeError, "Resource not found #{res}"
88
+ else
89
+ raise RuntimeError, "Maybe request timed out #{res}. HTTP status code #{res.code}"
90
+ end
91
+ end
92
+
93
+ end
94
+ end
95
+ end
data/lib/taobao_fu.rb ADDED
@@ -0,0 +1,104 @@
1
+ begin
2
+ require "crack"
3
+ rescue LoadError
4
+ puts "The Crack gem is not available.\nIf you ran this command from a git checkout " \
5
+ "of Rails, please make sure crack is installed. \n "
6
+ exit
7
+ end
8
+ # require "patron"
9
+ require "digest/md5"
10
+ require "yaml"
11
+ require "uri"
12
+ require "rest"
13
+
14
+ module TaobaoFu
15
+
16
+ SANDBOX = 'http://gw.api.tbsandbox.com/router/rest?'
17
+ PRODBOX = 'http://gw.api.taobao.com/router/rest?'
18
+ USER_AGENT = 'why404-taobao_fu/1.0.0.beta5'
19
+ REQUEST_TIMEOUT = 10
20
+ API_VERSION = 2.0
21
+ SIGN_ALGORITHM = 'md5'
22
+ OUTPUT_FORMAT = 'json'
23
+
24
+ class << self
25
+ def load(config_file)
26
+ @settings = YAML.load_file(config_file)
27
+ @settings = @settings[Rails.env] if defined? Rails.env
28
+ apply_settings
29
+ end
30
+
31
+ def apply_settings
32
+ ENV['TAOBAO_API_KEY'] = @settings['app_key'].to_s
33
+ ENV['TAOBAO_SECRET_KEY'] = @settings['secret_key']
34
+ ENV['TAOBAOKE_PID'] = @settings['taobaoke_pid']
35
+ @base_url = @settings['is_sandbox'] ? SANDBOX : PRODBOX
36
+
37
+ initialize_session if @settings['use_curl']
38
+ end
39
+
40
+ def initialize_session
41
+ @sess = Patron::Session.new
42
+ @sess.base_url = @base_url
43
+ @sess.headers['User-Agent'] = USER_AGENT
44
+ @sess.timeout = REQUEST_TIMEOUT
45
+ end
46
+
47
+ def switch_to(sandbox_or_prodbox)
48
+ @base_url = sandbox_or_prodbox
49
+ @sess.base_url = @base_url if @sess
50
+ end
51
+
52
+ def get(options = {})
53
+ if @sess
54
+ @response = @sess.get(generate_query_string(sorted_params(options))).body
55
+ else
56
+ @response = TaobaoFu::Rest.get(@base_url, generate_query_vars(sorted_params(options)))
57
+ end
58
+ parse_result @response
59
+ end
60
+
61
+ # http://toland.github.com/patron/
62
+ def post(options = {})
63
+ end
64
+
65
+ def update(options = {})
66
+ end
67
+
68
+ def delete(options = {})
69
+ end
70
+
71
+ def sorted_params(options)
72
+ {
73
+ :app_key => @settings['app_key'],
74
+ :session => @settings['session'],
75
+ :format => OUTPUT_FORMAT,
76
+ :v => API_VERSION,
77
+ :sign_method => SIGN_ALGORITHM,
78
+ :timestamp => Time.now.strftime("%Y-%m-%d %H:%M:%S")
79
+ }.merge!(options)
80
+ end
81
+
82
+ def generate_query_vars(params)
83
+ params[:sign] = generate_sign(params.sort_by { |k,v| k.to_s }.flatten.join)
84
+ params
85
+ end
86
+
87
+ def generate_query_string(params)
88
+ params_array = params.sort_by { |k,v| k.to_s }
89
+ sign_token = generate_sign(params_array.flatten.join)
90
+ total_param = params_array.map { |key, value| key.to_s+"="+value.to_s } + ["sign=#{sign_token}"]
91
+ URI.escape(total_param.join("&"))
92
+ end
93
+
94
+ def generate_sign(param_string)
95
+ Digest::MD5.hexdigest(@settings['secret_key'] + param_string + @settings['secret_key']).upcase
96
+ end
97
+
98
+ def parse_result(data)
99
+ Crack::JSON.parse(data)
100
+ end
101
+
102
+ end
103
+
104
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taobao_fu_reload
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nioteam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: crack
16
+ requirement: &70361236937180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70361236937180
25
+ description: long live the taobao_fu!
26
+ email: info@networking.io
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - MIT-LICENSE
32
+ - README.markdown
33
+ - Rakefile
34
+ - lib/generators/taobao_fu/install_generator.rb
35
+ - lib/generators/taobao_fu/templates/config/initializers/taobao_fu.rb
36
+ - lib/generators/taobao_fu/templates/config/taobao.yml
37
+ - lib/generators/taobao_fu/USAGE
38
+ - lib/rest.rb
39
+ - lib/taobao_fu.rb
40
+ homepage: http://www.networking.io
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.15
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: taobao_fu reloaded!
64
+ test_files: []