1 # -*- coding: utf-8 -*-
3 require 'rexml/document'
6 #以下はKBMJのASPサービスであるパーソナライズド・レコメンダー用のロジックです
7 class Recommend < ActiveRecord::Base
9 TYPE_BUY, TYPE_VIEW = 0, 1
13 TYPE_BUY => "RECOMMEND_BUY_URL", #購入履歴用のURLを設定して下さい(&id[]= まで)
14 TYPE_VIEW => "RECOMMEND_VIEW_URL", #閲覧履歴用のURLを設定して下さい(&id[]= まで)
16 RANKING_URL = "RECOMMEND_RANKING_URL" #ランキング用のURLを設定して下さい(team=w まで)
18 def self.recommend_get(product_id, type=TYPE_BUY)
19 unless Recommend.find(:first, :conditions => ["product_id = ? and request_type = ? and created_at > ?", product_id, type, Time.now - (60 * 60)])
20 self.recommend_network_get(product_id, type)
23 logger.info(RecommendXml.find(:first, :conditions => ["product_id = ? and request_type = ?", product_id, type]).to_s)
24 RecommendXml.find(:all, :conditions => ["product_id = ? and request_type = ?", product_id, type])
27 def self.ranking_get(limit = nil)
28 unless Recommend.find(:first, :conditions => ["created_at > ? and product_id is null and request_type is null",Time.now - (60 * 60)])
29 self.ranking_network_get
32 RecommendXml.find(:all,:conditions => ["product_id is null and request_type is null"],:limit => limit)
35 def self.recommend_network_get(product_id, type)
36 url = "#{RECOMMEND_URLS[type]}#{product_id}"
37 return unless self.network_get(url, ["product_id = ? and request_type = ?", product_id, type], product_id,type)
38 Recommend.create(:product_id => product_id, :request_type=>type)
41 def self.ranking_network_get
43 return unless self.network_get(url, ["product_id is null and request_type is null"], nil, nil)
44 Recommend.create(:product_id => nil, :request_type=>nil)
47 def self.network_get(url, delete_conditions, product_id, type)
52 doc = REXML::Document.new response
54 RecommendXml.delete_all(delete_conditions)
55 columns = [:name, :url, :categroy, :price]
56 doc.elements.each("items/item") do | r |
57 recommend_xml = RecommendXml.new(:product_id => product_id, :request_type=>type)
58 recommend_xml[:recommend_id] = r.elements["id"].text
59 recommend_xml[:image_url] = r.elements["img_url"].text
60 columns.each do | column |
61 recommend_xml[column.to_s] = r.elements[column.to_s] && r.elements[column.to_s].text
68 rescue TimeoutError => e
70 logger.info "err: #{ e }"