Commit b7c7f19b by vulehuan

products: update code get more product and category from yahoo, change number of…

products: update code get more product and category from yahoo, change number of items display on home page, product page, category page and product detail page
parent d04b2354
class DefaultPagesController < ApplicationController class DefaultPagesController < ApplicationController
def home def home
@recommed_items = Product.get_recommended_products @recommed_items = Product.get_recommended_products(limit: 12)
@newest_items = Product.get_newest_products(limit: 4) @newest_items = Product.get_newest_products(limit: 5)
end end
end end
...@@ -7,7 +7,7 @@ class ProductCategoriesController < ApplicationController ...@@ -7,7 +7,7 @@ class ProductCategoriesController < ApplicationController
@items = Array.new @items = Array.new
@title = '' @title = ''
else else
@items = Product.get_products_by_category(product_category_id: params[:id], page: params[:page], limit: 16) @items = Product.get_products_by_category(product_category_id: params[:id], page: params[:page], limit: 24)
@title = term.name @title = term.name
add_breadcrumb term.name, product_category_path(term) add_breadcrumb term.name, product_category_path(term)
end end
......
...@@ -3,7 +3,7 @@ class ProductsController < ApplicationController ...@@ -3,7 +3,7 @@ class ProductsController < ApplicationController
before_action :signed_in_user, only: [:new] before_action :signed_in_user, only: [:new]
def index def index
@products = Product.get_list(limit: 16, page: params[:page]) @products = Product.get_list(limit: 24, page: params[:page])
end end
def show def show
...@@ -19,7 +19,7 @@ class ProductsController < ApplicationController ...@@ -19,7 +19,7 @@ class ProductsController < ApplicationController
add_breadcrumb product_category.name, product_category_path(product_category) add_breadcrumb product_category.name, product_category_path(product_category)
add_breadcrumb @product.name, product_path(@product) add_breadcrumb @product.name, product_path(@product)
end end
@other_products = Product.get_other_products(@product.id, product_category_id: @product.product_category_id, limit: 16) @other_products = Product.get_other_products(@product.id, product_category_id: @product.product_category_id, limit: 20)
end end
def new def new
......
...@@ -16,7 +16,7 @@ namespace :db do ...@@ -16,7 +16,7 @@ namespace :db do
category_ids = Array.new category_ids = Array.new
doc.elements.each('ResultSet/Result/Categories/Children/Child') do |ele| doc.elements.each('ResultSet/Result/Categories/Children/Child') do |ele|
i += 1 i += 1
break if i > 5 break if i > 20
id = ele.elements['Id'].text id = ele.elements['Id'].text
title = ele.elements['Title'] title = ele.elements['Title']
...@@ -39,63 +39,65 @@ namespace :db do ...@@ -39,63 +39,65 @@ namespace :db do
list_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch' list_url = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch'
category_ids.each do |category_id| category_ids.each do |category_id|
request_url = list_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=' + category_id
# 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)
i = 0 i = 0
doc.elements.each('ResultSet/Result/Hit') do |ele| (1..20).each do |offset|
next if ele.elements['Name'] == nil request_url = list_url + '?appid=' + YAHOO_SHOPPING_DATA_APP_ID + '&category_id=' + category_id.to_s + '&offset=' + offset.to_s + '&hits=50'
i += 1 # get the XML data as a string
break if i > 100 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 > 2000
name = ele.elements['Name'].text name = ele.elements['Name'].text
description = ele.elements['Description'].text description = ele.elements['Description'].text
headline = ele.elements['Headline'].text headline = ele.elements['Headline'].text
availability = ele.elements['Availability'].text availability = ele.elements['Availability'].text
code = ele.elements['Code'].text code = ele.elements['Code'].text
condition = ele.elements['Condition'].text condition = ele.elements['Condition'].text
image = ele.elements['Image'] image = ele.elements['Image']
image_small = '' image_small = ''
image_medium = '' image_medium = ''
image.elements.each('Small') do |child_ele| image.elements.each('Small') do |child_ele|
image_small = child_ele.text image_small = child_ele.text
end end
image.elements.each('Medium') do |child_ele| image.elements.each('Medium') do |child_ele|
image_medium = child_ele.text image_medium = child_ele.text
end end
review = ele.elements['Review'] review = ele.elements['Review']
review_rate = 0 review_rate = 0
review_count = 0 review_count = 0
review.elements.each('Rate') do |child_ele| review.elements.each('Rate') do |child_ele|
review_rate = child_ele.text review_rate = child_ele.text
end end
review.elements.each('Count') do |child_ele| review.elements.each('Count') do |child_ele|
review_count = child_ele.text review_count = child_ele.text
end end
price = ele.elements['Price'] price = ele.elements['Price']
price_currency = price.attributes["currency"] price_currency = price.attributes["currency"]
price = price.text price = price.text
Product.create!( Product.create!(
name: name, name: name,
description: description, description: description,
headline: headline, headline: headline,
availability: availability, availability: availability,
code: code, code: code,
condition: condition, condition: condition,
image_small: image_small, image_small: image_small,
image_medium: image_medium, image_medium: image_medium,
review_rate: review_rate, review_rate: review_rate,
review_count: review_count, review_count: review_count,
price_currency: price_currency, price_currency: price_currency,
price: price, price: price,
product_category_id: category_id, product_category_id: category_id,
status: true status: true
) )
end
end end
end end
end end
end end
\ No newline at end of file
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