Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit b96bdd1b4c216734356100bffb98a4ec47a88ac1 @wear committed Jul 12, 2009
Showing with 209 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +33 −0 README
  3. +23 −0 Rakefile
  4. +6 −0 init.rb
  5. +10 −0 install.rb
  6. +63 −0 lib/open_taobao.rb
  7. +19 −0 lib/open_taobao/shop.rb
  8. +4 −0 tasks/open_taobao_tasks.rake
  9. +19 −0 temlpate/config/taobao.yml
  10. +8 −0 test/open_taobao_test.rb
  11. +3 −0 test/test_helper.rb
  12. +1 −0 uninstall.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 README
@@ -0,0 +1,33 @@
+OpenTaobao
+==========
+
+open.taobao.com 插件,更方便的调用taobao open api,目前是实现两个方法
+
+1: 目前只能作为测试用户,跑在测试环境下
+2: 目前实现两个方法,以后会更多
+
+安装
+======
+先安装rest client,
+ruby script/plugin install git://github.com/adamwiggins/rest-client.git
+
+
+使用
+=======
+#fields和nick是taobao的taobao.shop.get参数,文档见http://wiki.open.taobao.com/index.php/Taobao.shop.get
+OpenTaobao::Shop.get(:fields =>'sid,cid,nick,title,desc,bulletin,pic_path,created,modified',
+ :nick => 'alipublic05')
+
+
+#这里要用倒taobao的回掉session, http://open.taobao.com/isv/appTest.php?你的app_key
+OpenTaobao::Shop::Cart.get_list(:session => params[:top_session])
+
+
+To-do
+=====
+1: 更多方法
+2: 生产环境使用
+3: 添加测试
+
+
+Copyright (c) 2009 [stephen - wear6365022#gmail.com], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the open_taobao plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the open_taobao plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'OpenTaobao'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,6 @@
+# Include hook code here
+taobao_config_file = "#{RAILS_ROOT}/config/taobao.yml"
+
+require 'open_taobao'
+
+TAOBAO = OpenTaobao.load_configuration(taobao_config_file)
@@ -0,0 +1,10 @@
+# Install hook code here
+require 'fileutils'
+require 'rubygems'
+
+dir = File.dirname(__FILE__)
+config = File.join('config', 'taobao.yml')
+
+FileUtils.cp File.join(templates, config), File.join(RAILS_ROOT, config) unless File.exist?(File.join(RAILS_ROOT, config))
+
+puts IO.read(File.join(dir, 'README'))
@@ -0,0 +1,63 @@
+# OpenTaobao
+module OpenTaobao
+
+ @raw_taobao_configuration = {}
+ @taobao_configuration = {}
+
+ class << self
+ def load_configuration(taobao_yaml_file)
+ if File.exist?(taobao_yaml_file)
+ @raw_taobao_configuration = YAML.load(ERB.new(File.read(taobao_yaml_file)).result)
+ if defined? RAILS_ENV
+ @raw_taobao_configuration = @raw_taobao_configuration[RAILS_ENV]
+ end
+ # Thread.current[:fb_api_config] = @raw_taobao_configuration unless Thread.current[:fb_api_config]
+ apply_configuration(@raw_taobao_configuration)
+ end
+ end
+
+ def apply_configuration(config)
+ ENV['TAOBAO_API_KEY'] = config['api_key'].to_s
+ ENV['TAOBAO_SECRET_KEY'] = config['secret_key']
+
+ @taobao_configuration = config
+ end
+
+ def generate_url(pasted)
+ "http://gw.sandbox.taobao.com/router/rest?" + url_params(pasted)
+ end
+
+
+ def url_encode(str)
+ return str.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x[0])}
+ end
+
+ def url_params(pasted)
+ total_param = pasted.to_a.collect{|key,value| key.to_s+"="+value} + ["sign=#{generate_sign(pasted)}"]
+ url_encode total_param.join("&")
+ end
+
+ def pasted_params
+ {
+ :app_key => @taobao_configuration['api_key'].to_s ,
+ # :method =>'taobao.shop.get',
+ :format=> @taobao_configuration['format'],
+ :v => @taobao_configuration['v'].to_s,
+ :timestamp => timestamp,
+ # :fields =>'sid,cid,nick,title,desc,bulletin,pic_path,created,modified',
+ # :nick => 'alipublic05'
+ }
+ end
+
+ def generate_sign(pasted)
+ # Digest::MD5.hexdigest("716be49fa01c839b302abc2ef40e3f56" + generate_params.to_s).upcase
+ Digest::MD5.hexdigest(@taobao_configuration['secret_key'] + pasted.to_s).upcase
+ end
+
+ def timestamp
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
+ end
+
+ end
+
+end
@@ -0,0 +1,19 @@
+module OpenTaobao
+ module Shop
+ class << self
+ def get(fields_and_nick = {})
+ pasted = OpenTaobao.pasted_params.merge({:method =>'taobao.shop.get'}).merge(fields_and_nick)
+ RestClient.get OpenTaobao.generate_url(pasted)
+ end
+ end
+
+ module Cart
+ class << self
+ def get_list(session = {})
+ pasted = OpenTaobao.pasted_params.merge({:method =>'taobao.shopcats.list.get'}).merge(session)
+ RestClient.get OpenTaobao.generate_url(pasted)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :open_taobao do
+# # Task goes here
+# end
@@ -0,0 +1,19 @@
+development:
+ api_key:
+ secret_key:
+ v:
+ format:
+ host:
+ callback_url:
+
+test:
+ api_key:
+ secret_key:
+ v:
+ format:
+
+production:
+ api_key:
+ secret_key:
+ v:
+ format:
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class OpenTaobaoTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit b96bdd1

Please sign in to comment.