Permalink
Browse files

add user.get, users.get, product.get

  • Loading branch information...
1 parent a61241c commit 99f6483c3fec9f8ea089530ddb048020d5ad982f @taweili taweili committed Nov 30, 2009
View
@@ -16,6 +16,9 @@
require "taobao/models/prop_value"
require "taobao/models/item"
require "taobao/models/trade"
+require "taobao/models/product"
+require "taobao/models/product_prop_img"
+require "taobao/models/product_img"
module Taobao
end
@@ -0,0 +1,37 @@
+require 'taobao/model'
+
+module Taobao
+ class Product < Model
+ def self.elm_name
+ "product"
+ end
+
+ def self.attr_names
+ [
+ :product_id,
+ :outer_id,
+ :tsc,
+ :cid,
+ :cat_name,
+ :props,
+ :props_str,
+ :name,
+ :binds,
+ :binds_str,
+ :sale_props,
+ :sale_props_str,
+ :price,
+ :desc,
+ :pic_path,
+ :created,
+ :modified,
+ :product_imgs
+ ]
+ end
+
+ for a in attr_names
+ attr_accessor a
+ end
+
+ end
+end
@@ -0,0 +1,22 @@
+require 'taobao/model'
+
+module Taobao
+ class ProductImg < Model
+ def self.elm_name
+ "productImg"
+ end
+
+ def self.attr_names
+ [
+ :pic_id,
+ :url,
+ :position
+ ]
+ end
+
+ for a in attr_names
+ attr_accessor a
+ end
+
+ end
+end
@@ -0,0 +1,23 @@
+require 'taobao/model'
+
+module Taobao
+ class ProductPropImg < Model
+ def self.elm_name
+ "productPropImg"
+ end
+
+ def self.attr_names
+ [
+ :pic_id,
+ :props,
+ :url,
+ :position
+ ]
+ end
+
+ for a in attr_names
+ attr_accessor a
+ end
+
+ end
+end
View
@@ -5,26 +5,32 @@ class User < Model
def self.elm_name
"user"
end
-
+
def self.attr_names
[
- :id,
- :nick,
+ :alipay_bind,
+ :auto_repost,
+ :birthday,
+ :buyer_credit,
+ :consumer_protection,
:created,
+ :has_more_pic,
+ :item_img_num,
+ :item_img_size,
+ :last_visit,
:location,
- :sex,
- :buyer_credit,
+ :nick,
+ :promoted_type,
+ :prop_img_num,
+ :prop_img_size,
:seller_credit,
- :last_visit,
- :real_name,
- :id_card,
- :phone,
- :mobile,
- :email,
- :birthday
+ :sex,
+ :status,
+ :type,
+ :user_id
]
end
-
+
for a in attr_names
attr_accessor a
end
View
@@ -5,6 +5,7 @@
module Taobao
class Parse
DEBUG = true
+
class MyListener
include REXML::StreamListener
attr_accessor :result
@@ -30,20 +31,31 @@ def model_classes
Taobao::SellerCat,
Taobao::PropValue,
Taobao::Item,
- Taobao::Trade
+ Taobao::Trade,
+ Taobao::Product,
+ Taobao::ProductImg,
+ Taobao::ProductPropImg
]
end
+ def elm_name_to_class_default
+ {
+ "buyer_credit" => Taobao::UserCredit,
+ "seller_credit" => Taobao::UserCredit
+ }
+ end
+
def array_elements
{
"SimpleUserInfo-array" => TotalArray,
"rsp" => TotalArray,
- "prop_values" => TotalArray
+ "prop_values" => TotalArray,
+ "product_imgs" => TotalArray
}
end
def elm_name_to_class(name)
- @name_to_class ||= (Hash[*(model_classes.collect {|v| [v.elm_name, v]}.flatten)]).merge(array_elements)
+ @name_to_class ||= (Hash[*(model_classes.collect {|v| [v.elm_name, v]}.flatten)]).merge(array_elements).merge(elm_name_to_class_default)
@name_to_class[name]
end
@@ -123,8 +135,10 @@ def process(data)
REXML::Document.parse_stream(data, listener)
listener.result
rescue Exception => e
- pp e
- nil
+ pp e.inspect
+ pp e.backtrace
+ throw e
end
+
end
end
View
@@ -2,6 +2,20 @@ module Taobao
class Service
def initialize(method, options={})
options = options.clone
+
+ @params = {
+ 'app_key'=>'test',
+ 'method'=>'taobao.taobaoke.items.get',
+ 'format'=>'xml',
+ 'v'=>'1.0',
+ 'timestamp'=>t.strftime("%Y-%m-%d %H:%M:%S"),
+ 'fields'=>'iid,title,nick,pic_url,price,click_url',
+ 'pid' => 'mm_5410_0_0',
+ 'cid' => '1512',
+ 'page_no' => '1',
+ 'page_size' => '6'
+ }
+
@params = {}
@params["appId"] = options.delete("app_id")
@params["sip_apiname"] = method
View
@@ -5,7 +5,7 @@
require 'pp'
class ParseTest < Test::Unit::TestCase
- context "A User instance" do
+ context "A Parser instance" do
should "should parse the error xml" do
result = Taobao::Parse.new.process(error_xml)
@@ -85,8 +85,55 @@ class ParseTest < Test::Unit::TestCase
result = Taobao::Parse.new.process(trades_xml)
assert_equal result.size, 2
end
+
+ should "parse taobao.user.get" do
+ result = Taobao::Parse.new.process(taobao_user_get_xml)
+ assert_equal result[0].buyer_credit.level, "0"
+ assert_equal result[0].nick, "nick"
+ assert_equal result[0].location.city, "金华"
+ end
+
+ should "parse taobao.users.get" do
+ result = Taobao::Parse.new.process(taobao_users_get_xml)
+ assert_equal result.size,2
+ assert_equal result[1].buyer_credit.level, "0"
+ assert_equal result[1].nick, "nick"
+ assert_equal result[1].location.city, "金华"
+ assert_equal result[0].buyer_credit.level, "4"
+ assert_equal result[0].nick, "hz0799"
+ assert_equal result[0].location.city, "新界"
+ end
+
+ should "parse taobao.product.get" do
+ result = Taobao::Parse.new.process(taobao_product_get_xml)
+ assert_equal result.size, 1
+ assert_equal result[0].cat_name, "手机"
+ assert_equal result[0].cid, "1512"
+ assert_equal result[0].name, "测试手机"
+ assert_equal result[0].product_id, "1895913"
+ assert_equal result[0].product_imgs.size, 1
+ assert_equal result[0].props, "20000:10552;32222:10555"
+ assert_equal result[0].props_str, "品牌:松下;松下型号:MX7;"
+ end
end
+ def taobao_product_get_xml
+ <<-XML
+ <rsp>
+ <product>
+ <cat_name>手机</cat_name>
+ <cid>1512</cid>
+ <name>测试手机</name>
+ <product_id>1895913</product_id>
+ <product_imgs list="true">
+ <productImg />
+ </product_imgs>
+ <props>20000:10552;32222:10555</props>
+ <props_str>品牌:松下;松下型号:MX7;</props_str>
+ </product>
+ </rsp>
+ XML
+ end
def trades_xml
<<-XML
@@ -300,4 +347,84 @@ def app_subsc_control_xml
</AppSubscControl>
XML
end
+
+ def taobao_user_get_xml
+ <<-XML
+ <rsp>
+ <user>
+ <buyer_credit>
+ <level>0</level>
+ <score>0</score>
+ <total_num>0</total_num>
+ <good_num>0</good_num>
+ </buyer_credit>
+ <created>2003-10-30 15:46:36</created>
+ <last_visit>1970-01-01 00:00:00</last_visit>
+ <location>
+ <city>金华</city>
+ <state>浙江</state>
+ </location>
+ <nick>nick</nick>
+ <seller_credit>
+ <level>0</level>
+ <score>0</score>
+ <total_num>0</total_num>
+ <good_num>0</good_num>
+ </seller_credit>
+ </user>
+
+ </rsp>
+ XML
+ end
+
+ def taobao_users_get_xml
+ <<-XML
+ <rsp>
+ <user>
+ <buyer_credit>
+ <level>4</level>
+ <score>147</score>
+ <total_num>147</total_num>
+ <good_num>147</good_num>
+ </buyer_credit>
+ <created>2005-02-17 13:19:49</created>
+ <last_visit>2009-11-06 14:39:24</last_visit>
+ <location>
+ <city>新界</city>
+ <state>香港</state>
+ </location>
+ <nick>hz0799</nick>
+ <seller_credit>
+ <level>4</level>
+ <score>146</score>
+ <total_num>156</total_num>
+ <good_num>150</good_num>
+ </seller_credit>
+ <sex>m</sex>
+ </user>
+ <user>
+ <buyer_credit>
+ <level>0</level>
+ <score>0</score>
+ <total_num>0</total_num>
+ <good_num>0</good_num>
+ </buyer_credit>
+ <created>2003-10-30 15:46:36</created>
+ <last_visit>1970-01-01 00:00:00</last_visit>
+ <location>
+ <city>金华</city>
+ <state>浙江</state>
+ </location>
+ <nick>nick</nick>
+ <seller_credit>
+ <level>0</level>
+ <score>0</score>
+ <total_num>0</total_num>
+ <good_num>0</good_num>
+ </seller_credit>
+ </user>
+
+ </rsp>
+ XML
+ end
end

0 comments on commit 99f6483

Please sign in to comment.