Module: Aliexpress

Extended by:
Configure
Defined in:
lib/aliexpress.rb,
lib/aliexpress/base.rb,
lib/aliexpress/data.rb,
lib/aliexpress/image.rb,
lib/aliexpress/order.rb,
lib/aliexpress/common.rb,
lib/aliexpress/freight.rb,
lib/aliexpress/message.rb,
lib/aliexpress/product.rb,
lib/aliexpress/version.rb,
lib/aliexpress/category.rb,
lib/aliexpress/configure.rb,
lib/aliexpress/logistics.rb,
lib/aliexpress/marketing.rb,
lib/aliexpress/evaluation.rb,
lib/aliexpress/token/store.rb,
lib/aliexpress/authorization.rb,
lib/aliexpress/token/redis_store.rb,
lib/aliexpress/token/object_store.rb

Defined Under Namespace

Modules: Configure, Token Classes: Authorization, Base, Category, Common, Data, Evaluation, Freight, Image, Logistics, Marketing, Message, Order, Product

Constant Summary

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.export_all_categoryObject

导出所有的分类信息



87
88
89
90
91
92
93
# File 'lib/aliexpress/category.rb', line 87

def self.export_all_category
  # 缓存分类
  cache_category

  # 导出分类
  dump_category
end

.generate_csv(id = 0) ⇒ Object

生成并保存 目录的 csv 文件 如果想要 层级获取,必然需要递归调用



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aliexpress/category.rb', line 68

def self.generate_csv(id = 0)
  category_file = "tmp/aliexpress_category_#{Time.now.strftime('%Y%m%d%H%M')}.csv"

  headers = %w( id 父等级 名称-中文 名称-英文)
  categories = Aliexpress::Category.getChildrenPostCategoryById(id)

  CSV.open category_file, 'wb' do |csv|
    csv << headers

    categories['aeopPostCategoryList'].each do |category|
      csv << [category['id'], id, category['names']['zh'], category['names']['en']]
    end

  end
end

Instance Method Details

#generate_csv(categories, csv, id = 0) ⇒ Object

这段递归的代码写的有些问题



107
108
109
110
111
112
113
114
115
# File 'lib/aliexpress/category.rb', line 107

def generate_csv(categories, csv, id = 0)
  categories['aeopPostCategoryList'].each do |category|
    csv << [category['id'], category['level'], category['names']['zh'], category['names']['en'], id]

    unless category['isleaf']
      generate_csv Aliexpress::Category.getChildrenPostCategoryById(category['id']), csv, category['id']
    end
  end
end

#test_for_output_csvObject

尝试递归失败, 递归学不好。 递归传文件做为参数,可能有些问题。 换个方式,利用 缓存(Cache) 处理。



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aliexpress/category.rb', line 97

def test_for_output_csv
  category_file = "tmp/aliexpress_category_#{Time.now.strftime('%Y%m%d%h%m')}.csv"
  headers = %w( id 等级 名称-中文 名称-英文 parent_id)
  categories = Aliexpress::Category.getChildrenPostCategoryById(0)

  csv = CSV.open category_file, 'wb'

  csv << headers

  # 这段递归的代码写的有些问题
  def generate_csv(categories, csv, id = 0)
    categories['aeopPostCategoryList'].each do |category|
      csv << [category['id'], category['level'], category['names']['zh'], category['names']['en'], id]

      unless category['isleaf']
        generate_csv Aliexpress::Category.getChildrenPostCategoryById(category['id']), csv, category['id']
      end
    end
  end

  generate_csv categories, csv, 0
end