taobaorb 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,6 @@ module Taobao
7
7
  config = YAML.load_file(Rails.root.join('config', 'taobaorb.yml'))[Rails.env]
8
8
  Taobao.public_key = config['public_key']
9
9
  Taobao.private_key = config['private_key']
10
- p Taobao.public_key
11
10
  rescue Errno::ENOENT
12
11
  puts 'Taobao config not found.'
13
12
  puts 'To generate one run: rails g taobao:config'
@@ -16,7 +16,7 @@ class Taobao::Category
16
16
  def subcategories
17
17
  return @subcategories if @subcategories
18
18
  @subcategories = category_request(parent_cid: @id).map do |cat|
19
- category = self.class.new cat[:id]
19
+ category = self.class.new cat[:cid]
20
20
  category.to_object(cat)
21
21
  category
22
22
  end
@@ -40,7 +40,7 @@ class Taobao::Category
40
40
  begin
41
41
  result[:itemcats_get_response][:item_cats][:item_cat]
42
42
  rescue NoMethodError
43
- raise Taobao::IncorrectCategoryId, 'Incorrect category ID'
43
+ []
44
44
  end
45
45
  end
46
46
 
@@ -26,6 +26,10 @@ class Taobao::Product
26
26
  end
27
27
  end
28
28
 
29
+ def user
30
+ Taobao::User.new @nick
31
+ end
32
+
29
33
  def method_missing(method_name, *args, &block)
30
34
  if @properties.include? method_name
31
35
  fetch_full_data unless @all_properties_fetched
@@ -40,12 +40,10 @@ class Taobao::ProductList < Taobao::AbstractList
40
40
 
41
41
  private
42
42
  def products
43
- begin
44
- products = cached_responce[:items_get_response][:items][:item]
45
- get_products_as_objects(products)
46
- rescue NoMethodError
47
- []
48
- end
43
+ products = cached_responce[:items_get_response][:items][:item]
44
+ get_products_as_objects(products)
45
+ rescue NoMethodError
46
+ []
49
47
  end
50
48
 
51
49
  def get_products_as_objects(products)
@@ -6,12 +6,10 @@ class Taobao::PropertyList < Taobao::AbstractList
6
6
 
7
7
  private
8
8
  def properties
9
- begin
10
- props = cached_responce[:itemprops_get_response][:item_props][:item_prop]
11
- props.map { |prop| Taobao::Property.new(prop) }
12
- rescue NoMethodError
13
- []
14
- end
9
+ props = cached_responce[:itemprops_get_response][:item_props][:item_prop]
10
+ props.map { |prop| Taobao::Property.new(prop) }
11
+ rescue NoMethodError
12
+ []
15
13
  end
16
14
 
17
15
  def retrieve_response
data/lib/taobao/user.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'date'
2
2
 
3
3
  class Taobao::User
4
-
4
+ attr_reader :nick
5
5
  # Retrive user info by nickname
6
6
  # @param nickname [String]
7
7
  def initialize(nickname)
@@ -1,3 +1,3 @@
1
1
  module Taobao
2
- VERSION = '0.9.1'
2
+ VERSION = '0.9.2'
3
3
  end
@@ -0,0 +1 @@
1
+ {"itemcats_get_response":{}}
@@ -1 +1 @@
1
- {"itemcats_get_response":{}}
1
+ {"error_response":{"code":41,"msg":"Invalid arguments:cids"}}
@@ -11,7 +11,7 @@ describe Taobao::Category do
11
11
  describe '#name' do
12
12
  it 'should returns the name of the category' do
13
13
  category = Taobao::Category.new(28)
14
- fixture = 'category.json'.json_fixture
14
+ fixture = 'category'.json_fixture
15
15
  args = {
16
16
  method: 'taobao.itemcats.get',
17
17
  fields: 'cid,parent_cid,name,is_parent',
@@ -21,35 +21,30 @@ describe Taobao::Category do
21
21
  category.name.should == 'ZIPPO/瑞士军刀/眼镜'
22
22
  end
23
23
  it 'should throws an exception if the category ID is incorrect' do
24
- fixture = 'incorrect_category.json'.json_fixture
25
- args = {
26
- method: 'taobao.itemcats.get',
27
- fields: 'cid,parent_cid,name,is_parent',
28
- cids: -20
29
- }
30
- Taobao.stub(:api_request).with(args).and_return(fixture)
24
+ Net::HTTP.stub(:post_form).and_return 'incorrect_category'.to_response
31
25
  category = Taobao::Category.new(-20)
32
26
  expect {
33
27
  category.name
34
- }.to raise_error Taobao::IncorrectCategoryId, 'Incorrect category ID'
28
+ }.to raise_error Taobao::ApiError, 'Invalid arguments:cids'
35
29
  end
36
30
  end
37
31
 
38
32
  describe '#subcategories' do
39
- category = Taobao::Category.new(28)
40
- fixture = 'subcategories.json'.json_fixture
41
- args = {
42
- method: 'taobao.itemcats.get',
43
- fields: 'cid,parent_cid,name,is_parent',
44
- parent_cid: 28
45
- }
33
+ before :each do
34
+ @category = Taobao::Category.new(28)
35
+ end
46
36
  it 'should returns subcategories if they are exists' do
47
- Taobao.stub(:api_request).with(args).and_return(fixture)
48
- category.subcategories.should have_at_least(1).subcategory
37
+ Taobao.stub(:api_request).and_return 'subcategories'.json_fixture
38
+ @category.subcategories.should have_at_least(1).subcategory
39
+ end
40
+ it 'should returns empty array if subcategories do not exist' do
41
+ Taobao.stub(:api_request).and_return 'category_without_subcategories'.json_fixture
42
+ @category.subcategories.should be_empty
49
43
  end
50
44
  it 'each subcategory should be an object of Taobao::Category class' do
51
- Taobao.stub(:api_request).with(args).and_return(fixture)
52
- category.subcategories[0].should be_a_kind_of(Taobao::Category)
45
+ Taobao.stub(:api_request).and_return 'subcategories'.json_fixture
46
+ @category.subcategories[0].should be_a_kind_of(Taobao::Category)
47
+ @category.subcategories[0].id.should be > 0
53
48
  end
54
49
  end
55
50
 
@@ -6,7 +6,7 @@ describe Taobao::ProductList do
6
6
  describe 'init with default options and category id' do
7
7
  pl = Taobao::ProductList.new(cid: 28)
8
8
 
9
- fixture = 'items.json'.json_fixture
9
+ fixture = 'items'.json_fixture
10
10
  args = {
11
11
  method: 'taobao.items.get',
12
12
  fields: 'num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume',
@@ -32,7 +32,7 @@ describe Taobao::ProductList do
32
32
  .per_page(10)
33
33
  .order_by_price
34
34
 
35
- fixture = 'items_page15.json'.json_fixture
35
+ fixture = 'items_page15'.json_fixture
36
36
  args = {
37
37
  method: 'taobao.items.get',
38
38
  fields: 'num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume',
@@ -60,7 +60,7 @@ describe Taobao::ProductList do
60
60
  describe 'category without items' do
61
61
  it 'should return 0 items' do
62
62
  pl = Taobao::ProductList.new(cid: 283333)
63
- fixture = 'no_items.json'.json_fixture
63
+ fixture = 'no_items'.json_fixture
64
64
  args = {
65
65
  method: 'taobao.items.get',
66
66
  fields: 'num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume',
@@ -2,8 +2,7 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Taobao::Product do
5
- describe 'initialize with id' do
6
- fixture = 'product.json'.json_fixture
5
+ before(:each) do
7
6
  args = {
8
7
  method: 'taobao.item.get',
9
8
  fields: 'cid,num_iid,title,nick,price,pic_url,type,score,delist_time,' +
@@ -15,33 +14,38 @@ describe Taobao::Product do
15
14
  'property_alias,item_img,prop_img,sku,video,outer_id,is_virtual',
16
15
  num_iid: '2997802325'
17
16
  }
17
+ fixture = 'product'.json_fixture
18
+ Taobao.stub(:api_request).with(args).and_return(fixture)
19
+ @product = Taobao::Product.new(2997802325)
20
+ end
18
21
 
22
+ describe 'initialize with id' do
19
23
  it 'should returns product' do
20
- Taobao.stub(:api_request).with(args).and_return(fixture)
21
- product = Taobao::Product.new(2997802325)
24
+ @product.cid.should == 1512
25
+ @product.nick.should == '奇迹shouji'
26
+ @product.num_iid.should == 2997802325
27
+ @product.pic_url.should == 'http://img03.taobaocdn.com/bao/uploaded/i3/T1YsfeXbxkXXXpZak._110941.jpg'
28
+ @product.price.should == 4300.00
29
+ @product.title.should == '可完美越狱 Apple/苹果 iPhone 4S 正品 装软件 未拆封 未激活'
22
30
 
23
- product.cid.should == 1512
24
- product.nick.should == '奇迹shouji'
25
- product.num_iid.should == 2997802325
26
- product.pic_url.should == 'http://img03.taobaocdn.com/bao/uploaded/i3/T1YsfeXbxkXXXpZak._110941.jpg'
27
- product.price.should == 4300.00
28
- product.title.should == '可完美越狱 Apple/苹果 iPhone 4S 正品 装软件 未拆封 未激活'
29
-
30
- product.approve_status.should == 'onsale'
31
- product.auction_point.should == 0
32
- product.delist_time.should == '2012-06-01 22:41:44'
33
- product.type.should == 'fixed'
31
+ @product.approve_status.should == 'onsale'
32
+ @product.auction_point.should == 0
33
+ @product.delist_time.should == '2012-06-01 22:41:44'
34
+ @product.type.should == 'fixed'
34
35
  end
35
36
 
36
37
  describe 'get unknown product property' do
37
38
  it 'should raise an exception' do
38
- Taobao.stub(:api_request).with(args).and_return(fixture)
39
- product = Taobao::Product.new(2997802325)
40
39
  expect {
41
- product.unknown_property
40
+ @product.unknown_property
42
41
  }.to raise_error NoMethodError
43
42
  end
44
43
  end
44
+ end
45
45
 
46
+ describe '#user' do
47
+ it 'should returns Taobao::User object' do
48
+ @product.user.should be_a_kind_of Taobao::User
49
+ end
46
50
  end
47
51
  end
@@ -7,7 +7,7 @@ describe Taobao::PropertyList do
7
7
  it 'should returns property' do
8
8
  property_list = Taobao::PropertyList.new(cid: 1512)
9
9
 
10
- fixture = 'category_properties.json'.json_fixture
10
+ fixture = 'category_properties'.json_fixture
11
11
  args = {
12
12
  method: 'taobao.itemprops.get',
13
13
  fields: 'pid,name,prop_values,must,multi,is_color_prop,is_key_prop,is_enum_prop,is_input_prop,is_sale_prop,is_item_prop',
@@ -23,7 +23,7 @@ describe Taobao::PropertyList do
23
23
  it 'should returns 0 properties' do
24
24
  property_list = Taobao::PropertyList.new(cid: 0)
25
25
 
26
- fixture = 'top_category_properties.json'.json_fixture
26
+ fixture = 'top_category_properties'.json_fixture
27
27
  args = {
28
28
  method: 'taobao.itemprops.get',
29
29
  fields: 'pid,name,prop_values,must,multi,is_color_prop,is_key_prop,is_enum_prop,is_input_prop,is_sale_prop,is_item_prop',
@@ -5,7 +5,7 @@ describe Taobao::Property do
5
5
  describe 'initialize' do
6
6
  describe 'with correct data' do
7
7
  it 'should create Property object' do
8
- property_response = 'property.json'.json_fixture
8
+ property_response = 'property'.json_fixture
9
9
  property = Taobao::Property.new(property_response)
10
10
 
11
11
  property.multi.should be_false
@@ -6,7 +6,7 @@ describe Taobao do
6
6
  describe 'search method returns product list' do
7
7
  search = Taobao::search('iPhone')
8
8
 
9
- fixture = 'search.json'.json_fixture
9
+ fixture = 'search'.json_fixture
10
10
  args = {
11
11
  method: 'taobao.items.get',
12
12
  fields: 'num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee,score,volume',
@@ -1,7 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- Response = Struct.new('Responce', :body)
4
-
5
3
  describe Taobao do
6
4
  describe 'set the public API key' do
7
5
  it 'should have rw access' do
@@ -18,8 +16,7 @@ describe Taobao do
18
16
  end
19
17
  describe 'API request' do
20
18
  it 'should always return a Hash object' do
21
- fixture = Response.new('category.json'.str_fixture)
22
- Net::HTTP.stub(:post_form).and_return(fixture)
19
+ Net::HTTP.stub(:post_form).and_return 'category'.to_response
23
20
  result = Taobao.api_request(method: 'taobao.itemcats.get',
24
21
  fields: 'cid,parent_cid,name,is_parent', cids: 0)
25
22
  result.should be_a_kind_of(Hash)
@@ -27,8 +24,7 @@ describe Taobao do
27
24
  end
28
25
  describe 'failed API request' do
29
26
  it 'should throws an exception' do
30
- fixture = Response.new('error.json'.str_fixture)
31
- Net::HTTP.stub(:post_form).and_return(fixture)
27
+ Net::HTTP.stub(:post_form).and_return 'error'.to_response
32
28
  expect { Taobao.api_request({}) }
33
29
  .to raise_error(Taobao::ApiError, 'Invalid arguments:cid')
34
30
  end
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
  describe Taobao::User do
5
5
  describe 'getting user by valid nickname' do
6
6
  it 'should returns valid user info' do
7
- fixture = 'user.json'.json_fixture
7
+ fixture = 'user'.json_fixture
8
8
  args = {
9
9
  method: 'taobao.user.get',
10
10
  fields: 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,' +
@@ -16,6 +16,7 @@ describe Taobao::User do
16
16
 
17
17
  user = Taobao::User.new('喜客多2008')
18
18
 
19
+ user.nick.should == '喜客多2008'
19
20
  user.good_purchases_count.should == 1292
20
21
  user.buyer_level.should == 8
21
22
  user.buyer_score.should == 1292
@@ -41,7 +42,7 @@ describe Taobao::User do
41
42
  end
42
43
  describe 'getting user who does not specify his/her sex' do
43
44
  it 'should returns :unknown value' do
44
- fixture = 'user_unknown_sex.json'.json_fixture
45
+ fixture = 'user_unknown_sex'.json_fixture
45
46
  args = {
46
47
  method: 'taobao.user.get',
47
48
  fields: 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,' +
@@ -57,7 +58,7 @@ describe Taobao::User do
57
58
 
58
59
  describe 'getting non-existent user' do
59
60
  it 'should throws an exception' do
60
- fixture = 'absent_user.json'.json_fixture
61
+ fixture = 'absent_user'.json_fixture
61
62
  args = {
62
63
  method: 'taobao.user.get',
63
64
  fields: 'user_id,uid,nick,sex,buyer_credit,seller_credit,location,' +
data/spec/spec_helper.rb CHANGED
@@ -8,13 +8,19 @@ end
8
8
 
9
9
  require 'taobaorb'
10
10
 
11
+ Response = Struct.new('Responce', :body)
12
+
11
13
  class String
12
- def str_fixture
13
- open("spec/fixtures/#{self}").read
14
+ def to_response
15
+ Response.new get_fixture_as_text
14
16
  end
15
17
 
16
18
  def json_fixture
17
- contents = open("spec/fixtures/#{self}").read
18
- JSON.parse contents, {symbolize_names: true}
19
+ JSON.parse get_fixture_as_text, {symbolize_names: true}
20
+ end
21
+
22
+ private
23
+ def get_fixture_as_text
24
+ open("spec/fixtures/#{self}.json").read
19
25
  end
20
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taobaorb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby wrapper for the Taobao API
15
15
  email: a741su@gmail.com
@@ -38,6 +38,7 @@ files:
38
38
  - spec/fixtures/absent_user.json
39
39
  - spec/fixtures/category.json
40
40
  - spec/fixtures/category_properties.json
41
+ - spec/fixtures/category_without_subcategories.json
41
42
  - spec/fixtures/error.json
42
43
  - spec/fixtures/incorrect_category.json
43
44
  - spec/fixtures/items.json
@@ -75,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
76
  version: '0'
76
77
  segments:
77
78
  - 0
78
- hash: 1557645060868365656
79
+ hash: -1342558535374082704
79
80
  required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  none: false
81
82
  requirements:
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
85
  version: '0'
85
86
  segments:
86
87
  - 0
87
- hash: 1557645060868365656
88
+ hash: -1342558535374082704
88
89
  requirements: []
89
90
  rubyforge_project:
90
91
  rubygems_version: 1.8.24