top4r 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.2.3 / 2013-01-09
2
+
3
+ Add: taobao.items.inventory.get 获取当前用户的库存商品列表
4
+ Mod: 与master merge,现在开始不再维护bm分支
5
+
1
6
  === 0.2.2 / 2013-01-09
2
7
 
3
8
  Add: taobao.items.list.get 的支持
@@ -0,0 +1,26 @@
1
+ h1. Top4R
2
+
3
+ Top4R 封装了 "淘宝开放平台":http://open.taobao.com 的 Ruby 接口,帮助你快速构建基于TOP的应用程序。
4
+
5
+ h2. 功能
6
+
7
+ * [09/25/10] TOP API 2.0支持
8
+ * 商品
9
+ * 物流
10
+ * 店铺
11
+ * 收费
12
+ * 淘宝客
13
+ * 交易
14
+ * 用户
15
+
16
+ h2. 安装
17
+
18
+ $ gem install top4r
19
+
20
+ h2. Authors
21
+
22
+ * "Nowa Zhu":http://nowa.me <nowazhu@gmail.com>
23
+
24
+ h3. Thanks
25
+
26
+ * Twitter4R <"http://twitter4r.rubyforge.org":http://twitter4r.rubyforge.org>.
@@ -16,6 +16,7 @@ require 'json'
16
16
  require 'yaml'
17
17
  require 'iconv'
18
18
  require 'timeout'
19
+ require 'base64'
19
20
 
20
21
  require_local('top4r/ext')
21
22
  require_local('top4r/version')
@@ -6,7 +6,7 @@ class Top4R::Client
6
6
 
7
7
  def inspect
8
8
  s = old_inspect
9
- s.gsub!(/@app_secret=".*?"/, '@app_secret="XXXX"')
9
+ s.gsub!(/@app_secret=".*?"/, '@app_secret="******"')
10
10
  end
11
11
 
12
12
  def init
@@ -70,27 +70,35 @@ class Top4R::Client
70
70
  end
71
71
 
72
72
  def handle_rest_response(response, uri = nil)
73
- unless response.is_a?(Net::HTTPSuccess)
73
+ if !response.is_a?(Net::HTTPSuccess)
74
74
  raise_rest_error(response, uri)
75
75
  end
76
-
76
+
77
77
  map = JSON.parse(response.body)
78
+
78
79
  # API 1.0
79
- if map["error_rsp"].is_a?(Hash) and map["error_rsp"]["code"].to_s == "630"
80
+ if !map["error_response"].blank? and map["error_response"]["code"].to_s == "27"
81
+ puts "[Top4R]--#{map.inspect}"
82
+ @@logger.info "Login session expired."
83
+ raise Top4R::LoginRequiredError.new
84
+ elsif !map["error_response"].blank? and map["error_response"]["code"].to_s == "560"
85
+ @@logger.info "Shop not exist."
86
+ raise Top4R::ShopNotExistError.new(:code => map["error_response"]["code"])
87
+ elsif map["error_rsp"].is_a?(Hash) and map["error_rsp"]["code"].to_s == "630"
80
88
  @@logger.info "Raising SuiteNotOrderedError..."
81
89
  raise Top4R::SuiteNotOrderedError.new(:code => map["error_rsp"]["code"],
82
90
  :message => map["error_rsp"]["msg"],
83
91
  :error => map["error_rsp"],
84
92
  :uri => uri)
85
93
  elsif map["error_rsp"].is_a?(Hash)
86
- @@logger.info "Raising RESTError..."
94
+ @@logger.info "Raising RESTError [error_rsp not hash]..."
87
95
  raise Top4R::RESTError.new(:code => map["error_rsp"]["code"],
88
96
  :message => map["error_rsp"]["msg"],
89
97
  :error => map["error_rsp"],
90
98
  :uri => uri)
91
99
  # API 2.0
92
100
  elsif map["error_response"].is_a?(Hash)
93
- @@logger.info "Raising RESTError..."
101
+ @@logger.info "Raising RESTError [error_response not hash]..."
94
102
  raise Top4R::RESTError.new(:code => map["error_response"]["code"],
95
103
  :message => map["error_response"]["msg"],
96
104
  :sub_code => map["error_response"]["sub_code"],
@@ -129,13 +137,12 @@ class Top4R::Client
129
137
 
130
138
  def append_top_params(params)
131
139
  params = params.merge({
132
- # :session => @session,
133
140
  :timestamp => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
134
141
  :format => "#{@@config.format}",
135
142
  :app_key => @app_key
136
143
  })
137
144
  if !@session.nil? and !@session.blank?
138
- params[:session] = @session
145
+ params = params.merge({ :session => @session })
139
146
  end
140
147
  params[:v] = "2.0" unless params[:v]
141
148
  params = params.merge({
@@ -1,6 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  class Top4R::Client
3
3
  @@ITEM_METHODS = {
4
+ :inventory_list => 'taobao.items.inventory.get',
4
5
  :onsale_list => 'taobao.items.onsale.get',
5
6
  :item_info => 'taobao.item.get',
6
7
  :items_info => 'taobao.items.list.get',
@@ -25,21 +26,47 @@ class Top4R::Client
25
26
  items
26
27
  end
27
28
 
29
+ # 得到当前会话用户库存中的商品列表
30
+ def items_inventory(q = nil, options = {}, &block)
31
+ method = :inventory_list
32
+ valid_method(method, @@ITEM_METHODS, :item)
33
+ options = {:q => q}.merge(options) if q
34
+ params = {:fields => Top4R::Item.fields}.merge(options)
35
+ response = http_connect {|conn| create_http_get_request(@@ITEM_METHODS[method], params)}
36
+ result = JSON.parse(response.body)[rsp(@@ITEM_METHODS[method])]
37
+ if result.is_a?(Hash) and result['items']
38
+ items = Top4R::Item.unmarshal(result["items"]["item"])
39
+ items.each {|item| bless_model(item); yield item if block_given?}
40
+ @total_results = result["total_results"].to_i
41
+ else
42
+ @total_results = 0
43
+ items = []
44
+ end
45
+ items
46
+ end
47
+
28
48
  def item_info(iid = nil, options = {}, &block)
29
49
  method = :item_info
30
50
  valid_method(method, @@ITEM_METHODS, :item)
31
51
  options = {:num_iid => iid}.merge(options) if iid
32
52
  params = {:fields => Top4R::Item.fields}.merge(options)
33
53
  response = http_connect {|conn| create_http_get_request(@@ITEM_METHODS[method], params)}
34
- item = Top4R::Item.unmarshal(JSON.parse(response.body)[rsp(@@ITEM_METHODS[method])]["item"])
35
- bless_model(item)
54
+ result = JSON.parse(response.body)[rsp(@@ITEM_METHODS[method])]
55
+ if result.is_a?(Hash) and result['item']
56
+ item = Top4R::Item.unmarshal(result["item"])
57
+ item = bless_model(item)
58
+ else
59
+ item = nil
60
+ end
61
+ item
36
62
  end
37
63
 
38
64
  def items_info(iid = nil, options = {}, &block)
39
65
  method = :items_info
40
66
  valid_method(method, @@ITEM_METHODS, :item)
41
- iid = [iid] if !iid.is_a?(Array)
42
- options = {:num_iids => iid}.merge(options) if iid
67
+ # 为了兼容老的items_list接口,先split,后面再join
68
+ iid = iid.split(',') if !iid.is_a?(Array)
69
+ options = {:num_iids => iid.join(',')}.merge(options) if iid
43
70
  params = {:fields => Top4R::Item.fields}.merge(options)
44
71
  response = http_connect {|conn| create_http_get_request(@@ITEM_METHODS[method], params)}
45
72
  result = JSON.parse(response.body)[rsp(@@ITEM_METHODS[method])]
@@ -53,4 +80,7 @@ class Top4R::Client
53
80
  end
54
81
  items
55
82
  end
83
+
84
+ alias_method :item, :item_info
85
+ alias_method :items_list, :items_info
56
86
  end
@@ -56,10 +56,6 @@ module Top4R
56
56
  :test_rest_uri => '/router/rest',
57
57
  :test_port => 80,
58
58
  :test_protocol => :http,
59
- # :staging_host => '192.168.208.110',
60
- # :staging_rest_uri => '/top/private/services/rest',
61
- # :staging_port => '8080',
62
- # :staging_protocol => :http,
63
59
  :proxy_host => nil,
64
60
  :proxy_port => nil,
65
61
  :format => :json,
@@ -7,6 +7,8 @@ module Top4R
7
7
  end
8
8
 
9
9
  module InstanceMethods
10
+ attr_accessor :raw
11
+
10
12
  def initialize(params = {})
11
13
  others = {}
12
14
  params.each do |key,val|
@@ -16,6 +18,7 @@ module Top4R
16
18
  others[key] = val
17
19
  end
18
20
  end
21
+ self.send("raw=",params)
19
22
  self.send("#{:other_attrs}=", others) if self.respond_to? :other_attrs and others.size > 0
20
23
  self.send(:init) if self.respond_to? :init
21
24
  end
@@ -61,4 +64,14 @@ module Top4R
61
64
  "#{@method} method at model #{@model} requires you to be logged in first"
62
65
  end
63
66
  end # LoginRequiredError
67
+
68
+ class ShopNotExistError < RESTError
69
+ include ClassUtilMixin
70
+ @@ATTRIBUTES = [:model, :method]
71
+ attr_accessor *@@ATTRIBUTES
72
+
73
+ def to_s
74
+ "#{@method} method at model #{@model} 错误,错误代号#{@code},用户没有开通店铺!"
75
+ end
76
+ end # ShopNotExistError
64
77
  end
@@ -76,7 +76,7 @@ module Top4R
76
76
  end
77
77
 
78
78
  def unmarshal_other_attrs
79
- @id = @iid
79
+ @id = @num_iid
80
80
  if @location && @location.size > 0
81
81
  @location = Location.new(@location)
82
82
  else
@@ -77,7 +77,7 @@ module Top4R
77
77
 
78
78
  def default_public_fields
79
79
  ["user_id", "nick", "sex", "buyer_credit", "seller_credit",
80
- "location.city", "location.state", "location.country", "created", "last_visit", "type"]
80
+ "location.city", "location.state", "email", "location.country", "created", "last_visit", "type"]
81
81
  end
82
82
 
83
83
  def default_private_fields
@@ -2,7 +2,7 @@
2
2
  module Top4R::Version
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- REVISION = 2
5
+ REVISION = 3
6
6
 
7
7
  class << self
8
8
  # Returns X.Y.Z formatted version string
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: top4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -33,7 +33,7 @@ executables:
33
33
  - top4rsh
34
34
  extensions: []
35
35
  extra_rdoc_files:
36
- - README
36
+ - README.textile
37
37
  - CHANGES
38
38
  - TODO
39
39
  - MIT-LICENSE
@@ -66,12 +66,12 @@ files:
66
66
  - lib/top4r/model.rb
67
67
  - lib/top4r/version.rb
68
68
  - lib/top4r.rb
69
- - README
69
+ - README.textile
70
70
  - CHANGES
71
71
  - TODO
72
72
  - MIT-LICENSE
73
73
  - bin/top4rsh
74
- homepage: http://top4r.labs.nowa.me
74
+ homepage: https://github.com/nowa/top4r
75
75
  licenses: []
76
76
  post_install_message:
77
77
  rdoc_options: []
data/README DELETED
@@ -1,28 +0,0 @@
1
- = TOP4R
2
-
3
- * http://top4r.labs.nowa.me
4
- * Thanks Twitter4R <http://twitter4r.rubyforge.org>.
5
-
6
- == DESCRIPTION:
7
-
8
- TOP4R封装了淘宝开发平台的接口,帮助你快速构建基于TOP的应用程序。
9
-
10
- == FEATURES/PROBLEMS:
11
-
12
- * [09/25/10] TOP API 2.0支持
13
- * 商品
14
- * 物流
15
- * 店铺
16
- * 收费
17
- * 淘宝客
18
- * 交易
19
- * 用户
20
-
21
- == REQUIREMENTS:
22
-
23
- * rubygems
24
- * json
25
-
26
- == INSTALL:
27
-
28
- sudo gem install top4r