Commit a7c40115 by vulehuan

update models and seeds file for dummy products from yahoo shopping api

parent 1cd32a56
......@@ -13,7 +13,7 @@ class Product
field :price_currency, type: String
field :price, type: Float
field :product_category_id, type: Integer
field :user_id, type: Integer
field :user_id, type: String
field :status, type: Mongoid::Boolean
belongs_to :product_category
......@@ -22,15 +22,15 @@ class Product
validates :user, presence: true
validates :name, :code, :condition, :image_medium, :price, presence: true
validates :name, length: { maximum: 255 }
validates :price, :product_category_id, :user_id, numericality: true
validates :price, :product_category_id, numericality: true
validates :price_currency, presence: true, if: "price > 0"
# searchable :auto_index => false do
# text :name, :description
# string :headline, :code, :condition, :price_currency
# double :price
# integer :product_category_id, :user_id
# end
include Sunspot::Mongoid
searchable :auto_index => false do
text :name, :description
string :headline, :code, :condition, :price_currency
double :price
end
# Returns recommended products (order by rate, and available)
def self.get_recommended_products(options = { limit: 8 })
......
class User
include Mongoid::Document
include ActiveModel::SecurePassword
field :name, type: String
field :email, type: String
field :password_digest, type: String
has_secure_password
field :remember_token, type: String
field :admin, type: Mongoid::Boolean
......@@ -12,7 +14,6 @@ class User
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
# has_secure_password
validates :password, length: { minimum: 6 }
def User.new_remember_token
......
......@@ -5,3 +5,122 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require 'net/http'
require 'rexml/document'
# Product Category
# http://developer.yahoo.co.jp/webapi/dir/category/v1/category.html
category_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/categorySearch'
request_url = category_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=1'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract category information
doc = REXML::Document.new(xml_data)
i = 0
category_ids = Array.new
doc.elements.each('ResultSet/Result/Categories/Children/Child') do |ele|
i += 1
break if i > 20
id = ele.elements['Id'].text
title = ele.elements['Title']
title.elements.each('Long') do |child_ele|
title = child_ele.text
end
category_ids.push(id)
ProductCategory.create!(
id: id,
name: title,
weight: id,
status: true
)
end
# Create admin account (product belongs to user)
@user = User.new(
name: "admin",
email: "admin@local.net",
admin: true,
password: "123456",
password_confirmation: "123456"
)
@user.save
# Product list
# http://developer.yahoo.co.jp/webapi/shopping/shopping/v1/itemsearch.html
list_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
i = 0
max_item_count = 10000
category_ids.each do |category_id|
break if i > max_item_count
(1..20).each do |offset|
break if i > max_item_count
request_url = list_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=' + category_id.to_s + '&offset=' + offset.to_s + '&hits=50'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract product information
doc = REXML::Document.new(xml_data)
doc.elements.each('ResultSet/Result/Hit') do |ele|
next if ele.elements['Name'] == nil
i += 1
break if i > max_item_count
name = ele.elements['Name'].text
description = ele.elements['Description'].text
headline = ele.elements['Headline'].text
availability = ele.elements['Availability'].text
code = ele.elements['Code'].text
condition = ele.elements['Condition'].text
image = ele.elements['Image']
image_small = ''
image_medium = ''
image.elements.each('Small') do |child_ele|
image_small = child_ele.text
end
image.elements.each('Medium') do |child_ele|
image_medium = child_ele.text
end
review = ele.elements['Review']
review_rate = 0
review_count = 0
review.elements.each('Rate') do |child_ele|
review_rate = child_ele.text
end
review.elements.each('Count') do |child_ele|
review_count = child_ele.text
end
price = ele.elements['Price']
price_currency = price.attributes["currency"]
price = price.text
begin
Product.create!(
name: name,
description: description,
headline: headline,
availability: availability,
code: code,
condition: condition,
image_small: image_small,
image_medium: image_medium,
review_rate: review_rate,
review_count: review_count,
price_currency: price_currency,
price: price,
product_category_id: category_id,
user_id: @user.id,
status: true
)
rescue
next
end
end
end
end
puts '==========='
puts 'DONE'
puts '==========='
namespace :db do
desc "Fill database with Yahoo shopping data"
task populate: :environment do
require 'net/http'
require 'rexml/document'
# Product Category
# http://developer.yahoo.co.jp/webapi/dir/category/v1/category.html
category_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/categorySearch'
request_url = category_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=1'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract category information
doc = REXML::Document.new(xml_data)
i = 0
category_ids = Array.new
doc.elements.each('ResultSet/Result/Categories/Children/Child') do |ele|
i += 1
break if i > 20
id = ele.elements['Id'].text
title = ele.elements['Title']
title.elements.each('Long') do |child_ele|
title = child_ele.text
end
category_ids.push(id)
ProductCategory.create!(
id: id,
name: title,
weight: id,
status: true
)
end
# Create admin account (product belongs to user)
@user = User.new(
name: "admin",
email: "admin@local.net",
admin: true,
password: "123456",
password_confirmation: "123456"
)
@user.save
# Product list
# http://developer.yahoo.co.jp/webapi/shopping/shopping/v1/itemsearch.html
list_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
i = 0
category_ids.each do |category_id|
(1..20).each do |offset|
request_url = list_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=' + category_id.to_s + '&offset=' + offset.to_s + '&hits=50'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(request_url)).body
# extract product information
doc = REXML::Document.new(xml_data)
doc.elements.each('ResultSet/Result/Hit') do |ele|
next if ele.elements['Name'] == nil
i += 1
break if i > 10000
name = ele.elements['Name'].text
description = ele.elements['Description'].text
headline = ele.elements['Headline'].text
availability = ele.elements['Availability'].text
code = ele.elements['Code'].text
condition = ele.elements['Condition'].text
image = ele.elements['Image']
image_small = ''
image_medium = ''
image.elements.each('Small') do |child_ele|
image_small = child_ele.text
end
image.elements.each('Medium') do |child_ele|
image_medium = child_ele.text
end
review = ele.elements['Review']
review_rate = 0
review_count = 0
review.elements.each('Rate') do |child_ele|
review_rate = child_ele.text
end
review.elements.each('Count') do |child_ele|
review_count = child_ele.text
end
price = ele.elements['Price']
price_currency = price.attributes["currency"]
price = price.text
begin
Product.create!(
name: name,
description: description,
headline: headline,
availability: availability,
code: code,
condition: condition,
image_small: image_small,
image_medium: image_medium,
review_rate: review_rate,
review_count: review_count,
price_currency: price_currency,
price: price,
product_category_id: category_id,
user_id: @user.id,
status: true
)
rescue
next
end
end
end
end
end
end
namespace :sunspot do
namespace :solr do
desc 'Start the Solr instance'
task :start => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.start
else
Sunspot::Solr::Server.new.start
end
puts "Successfully started Solr ..."
end
desc 'Run the Solr instance in the foreground'
task :run => :environment do
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.run
else
Sunspot::Solr::Server.new.run
end
end
desc 'Stop the Solr instance'
task :stop => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
if defined?(Sunspot::Rails::Server)
Sunspot::Rails::Server.new.stop
else
Sunspot::Solr::Server.new.stop
end
puts "Successfully stopped Solr ..."
end
task :reindex => :"sunspot:reindex"
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment