Difference between revisions of "ExampleStore Ruby"
From the Directed Edge Developer Base
(New page: <source lang="ruby"> #!/usr/bin/ruby require 'rubygems' require 'activerecord' require 'directed_edge' ActiveRecord::Base.establish_connection(:adapter => 'mysql', ...) |
|||
(One intermediate revision by the same user not shown) | |||
Line 27: | Line 27: | ||
def export_from_mysql | def export_from_mysql | ||
− | |||
− | |||
− | |||
− | |||
exporter = DirectedEdge::Exporter.new('examplestore.xml') | exporter = DirectedEdge::Exporter.new('examplestore.xml') | ||
− | |||
− | |||
Customer.find(:all).each do |customer| | Customer.find(:all).each do |customer| | ||
− | |||
− | |||
− | |||
item = DirectedEdge::Item.new(exporter.database, "customer#{customer.id}") | item = DirectedEdge::Item.new(exporter.database, "customer#{customer.id}") | ||
− | |||
− | |||
− | |||
item.add_tag('customer') | item.add_tag('customer') | ||
− | |||
− | |||
− | |||
purchases = Purchase.find(:all, :conditions => { :customer => customer.id }) | purchases = Purchase.find(:all, :conditions => { :customer => customer.id }) | ||
− | |||
− | |||
− | |||
− | |||
purchases.each { |purchase| item.link_to("product#{purchase.product}") } | purchases.each { |purchase| item.link_to("product#{purchase.product}") } | ||
− | |||
− | |||
− | |||
exporter.export(item) | exporter.export(item) | ||
end | end | ||
− | |||
− | |||
Product.find(:all).each do |product| | Product.find(:all).each do |product| | ||
− | |||
− | |||
− | |||
item = DirectedEdge::Item.new(exporter.database, "product#{product.id}") | item = DirectedEdge::Item.new(exporter.database, "product#{product.id}") | ||
− | |||
− | |||
− | |||
item.add_tag('product') | item.add_tag('product') | ||
− | |||
− | |||
− | |||
exporter.export(item) | exporter.export(item) | ||
end | end | ||
− | |||
− | |||
exporter.finish | exporter.finish | ||
end | end | ||
− | |||
− | |||
def import_to_directededge | def import_to_directededge | ||
@database.import('examplestore.xml') | @database.import('examplestore.xml') | ||
end | end | ||
− | |||
− | |||
− | |||
def create_customer(id) | def create_customer(id) | ||
Line 95: | Line 55: | ||
item.save | item.save | ||
end | end | ||
− | |||
− | |||
− | |||
def create_product(id) | def create_product(id) | ||
Line 104: | Line 61: | ||
item.save | item.save | ||
end | end | ||
− | |||
− | |||
def add_purchase(customer_id, product_id) | def add_purchase(customer_id, product_id) | ||
Line 112: | Line 67: | ||
item.save | item.save | ||
end | end | ||
− | |||
− | |||
def related_products(product_id) | def related_products(product_id) | ||
Line 119: | Line 72: | ||
item.related(['product']).map { |product| product.sub('product', '').to_i } | item.related(['product']).map { |product| product.sub('product', '').to_i } | ||
end | end | ||
− | |||
− | |||
def personalized_recommendations(customer_id) | def personalized_recommendations(customer_id) | ||
Line 129: | Line 80: | ||
store = ExampleStore.new | store = ExampleStore.new | ||
− | |||
− | |||
store.export_from_mysql | store.export_from_mysql | ||
− | |||
− | |||
− | |||
store.import_to_directededge | store.import_to_directededge | ||
− | |||
− | |||
store.create_customer(500) | store.create_customer(500) | ||
− | |||
− | |||
− | |||
store.create_product(2000) | store.create_product(2000) | ||
− | |||
− | |||
− | |||
store.add_purchase(500, 2000) | store.add_purchase(500, 2000) | ||
− | |||
− | |||
store.related_products(1).each do |product| | store.related_products(1).each do |product| | ||
puts "Related products for product 1: #{product}" | puts "Related products for product 1: #{product}" | ||
end | end | ||
− | |||
− | |||
store.personalized_recommendations(1).each do |product| | store.personalized_recommendations(1).each do |product| |
Latest revision as of 18:05, 23 November 2009
#!/usr/bin/ruby
require 'rubygems'
require 'activerecord'
require 'directed_edge'
ActiveRecord::Base.establish_connection(:adapter => 'mysql',
:host => 'localhost',
:username => 'examplestore',
:password => 'password',
:database => 'examplestore')
class Customer < ActiveRecord::Base
end
class Product < ActiveRecord::Base
end
class Purchase < ActiveRecord::Base
end
class ExampleStore
def initialize
@database = DirectedEdge::Database.new('examplestore', 'password')
end
def export_from_mysql
exporter = DirectedEdge::Exporter.new('examplestore.xml')
Customer.find(:all).each do |customer|
item = DirectedEdge::Item.new(exporter.database, "customer#{customer.id}")
item.add_tag('customer')
purchases = Purchase.find(:all, :conditions => { :customer => customer.id })
purchases.each { |purchase| item.link_to("product#{purchase.product}") }
exporter.export(item)
end
Product.find(:all).each do |product|
item = DirectedEdge::Item.new(exporter.database, "product#{product.id}")
item.add_tag('product')
exporter.export(item)
end
exporter.finish
end
def import_to_directededge
@database.import('examplestore.xml')
end
def create_customer(id)
item = DirectedEdge::Item.new(@database, "customer#{id}")
item.add_tag('customer')
item.save
end
def create_product(id)
item = DirectedEdge::Item.new(@database, "product#{id}")
item.add_tag('product')
item.save
end
def add_purchase(customer_id, product_id)
item = DirectedEdge::Item.new(@database, "customer#{customer_id}")
item.link_to("product#{product_id}")
item.save
end
def related_products(product_id)
item = DirectedEdge::Item.new(@database, "product#{product_id}")
item.related(['product']).map { |product| product.sub('product', '').to_i }
end
def personalized_recommendations(customer_id)
item = DirectedEdge::Item.new(@database, "customer#{customer_id}")
item.recommended(['product']).map { |product| product.sub('product', '').to_i }
end
end
store = ExampleStore.new
store.export_from_mysql
store.import_to_directededge
store.create_customer(500)
store.create_product(2000)
store.add_purchase(500, 2000)
store.related_products(1).each do |product|
puts "Related products for product 1: #{product}"
end
store.personalized_recommendations(1).each do |product|
puts "Personalized recommendations for user 1: #{product}"
end