taobaorb 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Taobao
4
+ module Generators
5
+ class ConfigGenerator < Rails::Generators::Base
6
+ desc "Creates a taobaorb configuration file at config/taobaorb.yml"
7
+ source_root File.dirname(__FILE__)
8
+ def copy_config
9
+ copy_file 'taobaorb.yml', 'config/taobaorb.yml'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ development: &default
2
+ public_key: put your public key here
3
+ private_key: and private key here
4
+ test:
5
+ <<: *default
6
+ production:
7
+ <<: *default
@@ -0,0 +1,17 @@
1
+ require 'rails/railtie'
2
+
3
+ module Taobao
4
+ class Railtie < ::Rails::Railtie
5
+ initializer 'set API keys' do
6
+ begin
7
+ config = YAML.load_file(Rails.root.join('config', 'taobaorb.yml'))[Rails.env]
8
+ Taobao.public_key = config['public_key']
9
+ Taobao.private_key = config['private_key']
10
+ p Taobao.public_key
11
+ rescue Errno::ENOENT
12
+ puts 'Taobao config not found.'
13
+ puts 'To generate one run: rails g taobao:config'
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/taobao/base.rb CHANGED
@@ -13,6 +13,10 @@ module Taobao
13
13
  attr_writer :private_key
14
14
  end
15
15
 
16
+ # Performs API calls to Taobao
17
+ #
18
+ # @param options [Hash]
19
+ # @return [Hash] API request result
16
20
  def self.api_request(options)
17
21
  uri = URI(PRODUCTION_URL)
18
22
  response = Net::HTTP.post_form uri, self.append_required_options(options)
@@ -1,22 +1,33 @@
1
1
  class Taobao::Category
2
+ include Taobao::Util
2
3
  attr_reader :id
3
4
 
5
+ # @param category_id [Integer]
4
6
  def initialize(category_id)
5
7
  @id = category_id.to_i
6
8
  end
7
9
 
10
+ # @return [String]
8
11
  def name
9
12
  @name ||= category_request(cids: @id).first[:name]
10
13
  end
11
14
 
15
+ # @return [Array<Taobao::Category>]
12
16
  def subcategories
13
- @subcategories ||= category_request(parent_cid: @id)
17
+ return @subcategories if @subcategories
18
+ @subcategories = category_request(parent_cid: @id).map do |cat|
19
+ category = self.class.new cat[:id]
20
+ category.to_object(cat)
21
+ category
22
+ end
14
23
  end
15
24
 
25
+ # @return [Taobao::PropertyList]
16
26
  def properties
17
27
  @properties ||= Taobao::PropertyList.new(cid: @id)
18
28
  end
19
29
 
30
+ # @return [Taobao::ProductList]
20
31
  def products
21
32
  @products ||= Taobao::ProductList.new(cid: @id)
22
33
  end
@@ -2,6 +2,7 @@ class Taobao::Property
2
2
  attr_reader :multi, :must, :name, :pid, :values
3
3
  include Taobao::Util
4
4
 
5
+ # @param response [Hash]
5
6
  def initialize(response)
6
7
  @response = response
7
8
  to_object response
data/lib/taobao/search.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  module Taobao
2
+ # Search products by name
3
+ # @param query [String]
4
+ # @return [Taobao::ProductList] list of found products
2
5
  def self.search(query)
3
6
  ProductList.new(q: query)
4
7
  end
data/lib/taobao/user.rb CHANGED
@@ -2,58 +2,73 @@ require 'date'
2
2
 
3
3
  class Taobao::User
4
4
 
5
+ # Retrive user info by nickname
6
+ # @param nickname [String]
5
7
  def initialize(nickname)
6
8
  @nick = nickname
7
9
  end
8
10
 
11
+ # @return [Integer]
9
12
  def good_purchases_count
10
13
  cached_response[:buyer_credit][:good_num].to_i
11
14
  end
12
15
 
16
+ # @return [Integer]
13
17
  def buyer_level
14
18
  cached_response[:buyer_credit][:level].to_i
15
19
  end
16
20
 
21
+ # @return [Integer]
17
22
  def buyer_score
18
23
  cached_response[:buyer_credit][:score].to_i
19
24
  end
20
25
 
26
+ # @return [Integer]
21
27
  def total_purchases_count
22
28
  cached_response[:buyer_credit][:total_num].to_i
23
29
  end
24
30
 
31
+ # @return [Integer]
25
32
  def good_sales_count
26
33
  cached_response[:seller_credit][:good_num].to_i
27
34
  end
28
35
 
36
+ # @return [Integer]
29
37
  def seller_level
30
38
  cached_response[:seller_credit][:level].to_i
31
39
  end
32
40
 
41
+ # @return [Integer]
33
42
  def seller_score
34
43
  cached_response[:seller_credit][:score].to_i
35
44
  end
36
45
 
46
+ # @return [Integer]
37
47
  def total_sales_count
38
48
  cached_response[:seller_credit][:total_num].to_i
39
49
  end
40
50
 
51
+ # @return [DateTime]
41
52
  def registration_date
42
53
  DateTime.parse cached_response[:created]
43
54
  end
44
55
 
56
+ # @return [DateTime]
45
57
  def last_visit
46
58
  DateTime.parse cached_response[:last_visit]
47
59
  end
48
60
 
61
+ # @return [String]
49
62
  def city
50
63
  cached_response[:location][:city]
51
64
  end
52
65
 
66
+ # @return [String]
53
67
  def state
54
68
  cached_response[:location][:state]
55
69
  end
56
70
 
71
+ # @return [Symbol]
57
72
  def sex
58
73
  if cached_response.has_key? :sex
59
74
  return :male if cached_response[:sex] == 'm'
@@ -62,14 +77,17 @@ class Taobao::User
62
77
  :unknown
63
78
  end
64
79
 
80
+ # @return [String]
65
81
  def type
66
82
  cached_response[:type]
67
83
  end
68
84
 
85
+ # @return [String]
69
86
  def uid
70
87
  cached_response[:uid]
71
88
  end
72
89
 
90
+ # @return [Integer]
73
91
  def id
74
92
  cached_response[:user_id].to_i
75
93
  end
@@ -0,0 +1,3 @@
1
+ module Taobao
2
+ VERSION = '0.9.1'
3
+ end
data/lib/taobaorb.rb CHANGED
@@ -14,3 +14,5 @@ require 'taobao/user'
14
14
  require 'taobao/abstract_list'
15
15
  require 'taobao/product_list'
16
16
  require 'taobao/property_list'
17
+
18
+ require 'rails/taobao-railtie' if defined?(Rails)
@@ -2,23 +2,25 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Taobao::Category do
5
-
6
- it 'should have name' do
7
- category = Taobao::Category.new(28)
8
- category.id.should == 28
9
-
10
- fixture = 'category.json'.json_fixture
11
- args = {
12
- method: 'taobao.itemcats.get',
13
- fields: 'cid,parent_cid,name,is_parent',
14
- cids: 28
15
- }
16
- Taobao.stub(:api_request).with(args).and_return(fixture)
17
- category.name.should == 'ZIPPO/瑞士军刀/眼镜'
5
+ describe '#id' do
6
+ it 'should returns ID of the category' do
7
+ category = Taobao::Category.new(28)
8
+ category.id.should == 28
9
+ end
18
10
  end
19
-
20
- describe 'with incorrect id' do
21
- it 'should throws an exception' do
11
+ describe '#name' do
12
+ it 'should returns the name of the category' do
13
+ category = Taobao::Category.new(28)
14
+ fixture = 'category.json'.json_fixture
15
+ args = {
16
+ method: 'taobao.itemcats.get',
17
+ fields: 'cid,parent_cid,name,is_parent',
18
+ cids: 28
19
+ }
20
+ Taobao.stub(:api_request).with(args).and_return(fixture)
21
+ category.name.should == 'ZIPPO/瑞士军刀/眼镜'
22
+ end
23
+ it 'should throws an exception if the category ID is incorrect' do
22
24
  fixture = 'incorrect_category.json'.json_fixture
23
25
  args = {
24
26
  method: 'taobao.itemcats.get',
@@ -33,30 +35,33 @@ describe Taobao::Category do
33
35
  end
34
36
  end
35
37
 
36
- describe 'subcategories' do
37
- it 'top level category should contains a few subcategories' do
38
- category = Taobao::Category.new(28)
39
-
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
- }
38
+ 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
+ }
46
+ it 'should returns subcategories if they are exists' do
46
47
  Taobao.stub(:api_request).with(args).and_return(fixture)
47
48
  category.subcategories.should have_at_least(1).subcategory
48
49
  end
50
+ 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)
53
+ end
49
54
  end
50
55
 
51
- describe 'properties' do
52
- it 'should return empty array for top level category' do
56
+ describe '#properties' do
57
+ it 'should return a PropertyList object' do
53
58
  category = Taobao::Category.new(0)
54
- category.properties.should be_a_kind_of(Taobao::PropertyList)
59
+ category.properties.should be_a_kind_of(Taobao::PropertyList)
55
60
  end
56
61
  end
57
62
 
58
- describe 'products' do
59
- it 'should return ProductList object' do
63
+ describe '#products' do
64
+ it 'should return a ProductList object' do
60
65
  category = Taobao::Category.new(28)
61
66
  category.products.should be_a_kind_of(Taobao::ProductList)
62
67
  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.0
4
+ version: 0.9.1
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-07-27 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby wrapper for the Taobao API
15
15
  email: a741su@gmail.com
@@ -17,6 +17,9 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/generators/taobao/config/config_generator.rb
21
+ - lib/generators/taobao/config/taobaorb.yml
22
+ - lib/rails/taobao-railtie.rb
20
23
  - lib/taobao/abstract_list.rb
21
24
  - lib/taobao/base.rb
22
25
  - lib/taobao/category.rb
@@ -30,6 +33,7 @@ files:
30
33
  - lib/taobao/search.rb
31
34
  - lib/taobao/user.rb
32
35
  - lib/taobao/util.rb
36
+ - lib/taobao/version.rb
33
37
  - lib/taobaorb.rb
34
38
  - spec/fixtures/absent_user.json
35
39
  - spec/fixtures/category.json
@@ -69,12 +73,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
73
  - - ! '>='
70
74
  - !ruby/object:Gem::Version
71
75
  version: '0'
76
+ segments:
77
+ - 0
78
+ hash: 1557645060868365656
72
79
  required_rubygems_version: !ruby/object:Gem::Requirement
73
80
  none: false
74
81
  requirements:
75
82
  - - ! '>='
76
83
  - !ruby/object:Gem::Version
77
84
  version: '0'
85
+ segments:
86
+ - 0
87
+ hash: 1557645060868365656
78
88
  requirements: []
79
89
  rubyforge_project:
80
90
  rubygems_version: 1.8.24
@@ -82,3 +92,4 @@ signing_key:
82
92
  specification_version: 3
83
93
  summary: Ruby wrapper for the Taobao API
84
94
  test_files: []
95
+ has_rdoc: