Difference between revisions of "ExampleStore Ruby"

From the Directed Edge Developer Base
Jump to: navigation, search
 
Line 27: Line 27:
  
 
   def export_from_mysql
 
   def export_from_mysql
 
    # Use the handy Directed Edge XML exporter to collect store data up to this
 
    # point
 
 
 
     exporter = DirectedEdge::Exporter.new('examplestore.xml')
 
     exporter = DirectedEdge::Exporter.new('examplestore.xml')
 
    # Loop through every customer in the database
 
  
 
     Customer.find(:all).each do |customer|
 
     Customer.find(:all).each do |customer|
 
      # Create a new item in the Directed Edge export file with the ID "customer12345"
 
 
 
       item = DirectedEdge::Item.new(exporter.database, "customer#{customer.id}")
 
       item = DirectedEdge::Item.new(exporter.database, "customer#{customer.id}")
 
      # Mark this item as a customer with a tag
 
 
 
       item.add_tag('customer')
 
       item.add_tag('customer')
 
      # Find all of the purchases for the current customer
 
     
 
 
       purchases = Purchase.find(:all, :conditions => { :customer => customer.id })
 
       purchases = Purchase.find(:all, :conditions => { :customer => customer.id })
 
      # For each purchase create a link from the customer to that item of the form
 
      # "product12345"
 
 
 
       purchases.each { |purchase| item.link_to("product#{purchase.product}") }
 
       purchases.each { |purchase| item.link_to("product#{purchase.product}") }
 
      # And now write the item to the export file
 
 
 
       exporter.export(item)
 
       exporter.export(item)
 
     end
 
     end
 
    # Now go through all of the products creating items for them
 
  
 
     Product.find(:all).each do |product|
 
     Product.find(:all).each do |product|
 
      # Here we'll also use the form "product12345" for our products
 
 
 
       item = DirectedEdge::Item.new(exporter.database, "product#{product.id}")
 
       item = DirectedEdge::Item.new(exporter.database, "product#{product.id}")
 
      # And mark it as a product with a tag
 
 
 
       item.add_tag('product')
 
       item.add_tag('product')
 
      # And export it to the file
 
 
 
       exporter.export(item)
 
       exporter.export(item)
 
     end
 
     end
 
    # We have to tell the exporter to clean up and finish up the file
 
  
 
     exporter.finish
 
     exporter.finish
 
   end
 
   end
 
  # Imports the file exported from the export method to the Directed Edge database
 
  
 
   def import_to_directededge
 
   def import_to_directededge
 
     @database.import('examplestore.xml')
 
     @database.import('examplestore.xml')
 
   end
 
   end
 
  # Creates a new customer in the Directed Edge database that corresponds to the
 
  # given customer ID
 
  
 
   def create_customer(id)
 
   def create_customer(id)
Line 95: Line 55:
 
     item.save
 
     item.save
 
   end
 
   end
 
  # Creates a new product in the Directed Edge database that corresponds to the
 
  # given product ID
 
  
 
   def create_product(id)
 
   def create_product(id)
Line 104: Line 61:
 
     item.save
 
     item.save
 
   end
 
   end
 
  # Notes in the Directed Edge database that customer_id purchased product_id
 
  
 
   def add_purchase(customer_id, product_id)
 
   def add_purchase(customer_id, product_id)
Line 112: Line 67:
 
     item.save
 
     item.save
 
   end
 
   end
 
  # Returns a list of product IDs related to the given product ID
 
  
 
   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
 
  # Returns a list of personalized recommendations for the given customer ID
 
  
 
   def personalized_recommendations(customer_id)
 
   def personalized_recommendations(customer_id)
Line 129: Line 80:
  
 
store = ExampleStore.new
 
store = ExampleStore.new
 
# Export the contents of our MySQL database to XML
 
  
 
store.export_from_mysql
 
store.export_from_mysql
 
# Import that XML to the Directed Edge database
 
 
 
store.import_to_directededge
 
store.import_to_directededge
 
# Add a new customer
 
  
 
store.create_customer(500)
 
store.create_customer(500)
 
# Add a new product
 
 
 
store.create_product(2000)
 
store.create_product(2000)
 
# Set that user as having purchased that product
 
 
 
store.add_purchase(500, 2000)
 
store.add_purchase(500, 2000)
 
# Find related products for the product with the ID 1 (in MySQL)
 
  
 
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
 
# Find personalized recommendations for the user with the ID 1 (in MySQL)
 
  
 
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