topsdk 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in topsdk.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'topsdk'
@@ -0,0 +1,21 @@
1
+ prod: &prod
2
+ api_key: 12012322
3
+ secret_key: 42ab2d12a5bea25d1bc06ccd23123121daf
4
+ auth_url: http://container.api.taobao.com/container
5
+ rest_url: http://gw.api.taobao.com/router/rest
6
+ black_list: taobao.wangwang.eservice.receivenum.get, taobao.wangwang.eservice.noreplynum.get, taobao.wangwang.eservice.onlinetime.get, taobao.wangwang.eservice.loginlogs.get, taobao.wangwang.eservice.avgwaittime.get, taobao.wangwang.eservice.chatpeers.get,
7
+
8
+ sandbox: &sandbox
9
+ <<: *prod
10
+ secret_key: sandbox9ffd79873a7ae0e01fefed97c
11
+ auth_url: http://container.api.tbsandbox.com/container
12
+ rest_url: http://gw.api.tbsandbox.com/router/rest
13
+
14
+ development:
15
+ <<: *sandbox
16
+
17
+ test:
18
+ <<: *sandbox
19
+
20
+ production:
21
+ <<: *prod
@@ -0,0 +1,70 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'nestful'
3
+ require 'digest/md5'
4
+ require 'pp'
5
+
6
+ module Topsdk
7
+ class Client
8
+ DEBUG = false
9
+
10
+ def initialize(params={})
11
+ @params = { # 淘宝参数
12
+ :app_key => ENV['TAOBAO_APP_KEY'],
13
+ :format => :json,
14
+ :v => '2.0',
15
+ :timestamp => timestamp
16
+ }
17
+ @params.merge!(params.clone) unless params.empty? # 參數合併
18
+ @params[:sign] = generate_sign(@params)
19
+ pp @params if DEBUG
20
+ end
21
+
22
+ def get(url=ENV['TAOBAO_REST_URL'])
23
+ format = if @params.has_key?(:method) && ENV['TAOBAO_XML_ONLY'].include?(@params[:method]) # 淘寶接口格式限制
24
+ :xml
25
+ else
26
+ :json
27
+ end
28
+ request = Nestful::Request.new(url, ({:method => :get, :format => format, :params => @params}))
29
+ @result = request.execute
30
+ parse
31
+ end
32
+
33
+ def post(url=ENV['TAOBAO_REST_URL'])
34
+ request = Nestful::Request.new(url, ({:method => :post, :format => :form, :params => @params}))
35
+ @result = request.execute
36
+ @result = JSON.parse(@result)
37
+ parse
38
+ end
39
+
40
+ def parse # 簡單解析一下
41
+ return false unless @result.is_a?(Hash)
42
+ case
43
+ when @result.has_key?(response_key)
44
+ pp response_key if DEBUG
45
+ result = @result[response_key]
46
+ when @result.has_key?('error_response')
47
+ @result['error_response']['msg']
48
+ else
49
+ @result
50
+ end
51
+ end
52
+
53
+ def generate_sign(params={})
54
+ rand = ENV['TAOBAO_APP_SECRET'] + params.sort.flatten.join
55
+ Digest::MD5.hexdigest(rand).upcase! # 數字簽名
56
+ end
57
+
58
+ def response_key # 操作結果鍵
59
+ @params[:method][7..-1].gsub(/\./, '_') + "_response" unless @params[:method].nil?
60
+ end
61
+
62
+ def root_key # 獲取對象的鍵
63
+ response_key.split('_')[0] unless @params[:method].nil?
64
+ end
65
+
66
+ def timestamp # 时间戳
67
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Topsdk
2
+ VERSION = '0.0.5'
3
+ end
data/lib/topsdk.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'topsdk/version'
2
+ require 'topsdk/client'
3
+
4
+ RAILS_ENV = PADRINO_ENV if defined?(PADRINO_ENV)
5
+
6
+ # 賦值
7
+ def apply_settings
8
+ @settings = YAML.load_file(config_file)
9
+ @settings = defined?(RAILS_ENV) ? @settings[RAILS_ENV] : @settings['development']
10
+ ENV['TAOBAO_APP_KEY'] = @settings['api_key'].to_s
11
+ ENV['TAOBAO_APP_SECRET'] = @settings['secret_key']
12
+ ENV['TAOBAO_AUTH_URL'] = @settings['auth_url']
13
+ ENV['TAOBAO_REST_URL'] = @settings['rest_url']
14
+ ENV['TAOBAO_XML_ONLY'] = @settings['black_list']
15
+ end
16
+
17
+ def config_file
18
+ # 解析配置文件
19
+ yml_file = File.expand_path(File.join('.', 'config', 'taobao.yml'))
20
+ unless File.exist?(yml_file)
21
+ File.expand_path(File.join('config', 'taobao.yml'), File.dirname(__FILE__))
22
+ else
23
+ yml_file
24
+ end
25
+ end
26
+
27
+ apply_settings
28
+
29
+ module Topsdk
30
+ class << self
31
+
32
+ def get_with(joined_params = {})
33
+ Client.new(joined_params).get)
34
+ end
35
+
36
+ def post_with(joined_params = {})
37
+ Client.new(joined_params).post
38
+ end
39
+
40
+ def authorize_url
41
+ "#{ENV['TAOBAO_AUTH_URL']}?appkey=#{ENV['TAOBAO_APP_KEY']}&encode=utf-8"
42
+ end
43
+
44
+ end
45
+ end
data/topsdk.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "topsdk/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "topsdk"
7
+ s.version = Topsdk::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Howl王"]
10
+ s.email = ["howl.wong@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = "Taobao API client"
13
+ s.description = %q{Simple Taobao API client for Ruby}
14
+ s.extra_rdoc_files = [
15
+ "README.rdoc"
16
+ ]
17
+
18
+ s.rubyforge_project = "topsdk"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ s.add_runtime_dependency "nestful"
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: topsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nestful
16
- requirement: &70238391476300 !ruby/object:Gem::Requirement
16
+ requirement: &70105361799620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70238391476300
24
+ version_requirements: *70105361799620
25
25
  description: Simple Taobao API client for Ruby
26
26
  email:
27
27
  - howl.wong@gmail.com
@@ -30,7 +30,17 @@ extensions: []
30
30
  extra_rdoc_files:
31
31
  - README.rdoc
32
32
  files:
33
+ - .DS_Store
34
+ - .gitignore
35
+ - Gemfile
33
36
  - README.rdoc
37
+ - Rakefile
38
+ - init.rb
39
+ - lib/config/taobao.yml
40
+ - lib/topsdk.rb
41
+ - lib/topsdk/client.rb
42
+ - lib/topsdk/version.rb
43
+ - topsdk.gemspec
34
44
  homepage: ''
35
45
  licenses: []
36
46
  post_install_message: