Difference between revisions of "Ruby Bindings for E-Commerce Tutorial"

From the Directed Edge Developer Base
Jump to: navigation, search
Line 2: Line 2:
  
 
If you want to see all of the code in one go, you can get it in full syntax-highlighted glory here: [[ExampleStore Class]], or download it [[Media:example_store.rb|here]].  You'll also want our Ruby bindings, which you can get from GitHub [http://github.com/directededge/directed-edge-bindings/blob/6f5f421d25f4b43e6b39cb35702987b5598cc85f/Ruby/directed_edge.rb here] ([http://github.com/directededge/directed-edge-bindings/raw/6f5f421d25f4b43e6b39cb35702987b5598cc85f/Ruby/directed_edge.rb download]).
 
If you want to see all of the code in one go, you can get it in full syntax-highlighted glory here: [[ExampleStore Class]], or download it [[Media:example_store.rb|here]].  You'll also want our Ruby bindings, which you can get from GitHub [http://github.com/directededge/directed-edge-bindings/blob/6f5f421d25f4b43e6b39cb35702987b5598cc85f/Ruby/directed_edge.rb here] ([http://github.com/directededge/directed-edge-bindings/raw/6f5f421d25f4b43e6b39cb35702987b5598cc85f/Ruby/directed_edge.rb download]).
 +
 +
== Creating the testing database ==
 +
 +
You've probably got your own site's database that you're more interested in, but if you want to follow along in the code, you can use the store data that we randomly generated.  We created 2000 users with 500 products and had each of those users "buy" between 0 and 30 products.
 +
 +
For that we created three very simple tables.  Since we don't care about the other properties of the customers or products, all that those tables contain is an ID.  The purchase table just contains ''customer'' and ''product'' columns.
 +
 +
<table style="padding: 0.5em; border: 1px solid #ccc; text-align: center;">
 +
<tr>
 +
<td>
 +
<tt>customers</tt> table:
 +
 +
{| style="background: #eee; padding: 0.2em;" border="1" cellpadding="10"
 +
! id
 +
|}
 +
 +
</td><td>
 +
 +
<tt>products</tt> table:
 +
 +
{| style="background: #eee; padding: 0.2em;" border="1" cellpadding="10"
 +
! id
 +
|}
 +
 +
</td><td>
 +
 +
<tt>purchases</tt> table:
 +
 +
{| style="background: #eee; padding: 0.2em;" border="1" cellpadding="10"
 +
! customer !! product
 +
|}
 +
 +
</td>
 +
</tr>
 +
</table>
 +
 +
It really can't get much simpler than that.  You can get a dump of the database [[Media:examplestore.mysql|here]].  If you have a local MySQL running, you can create and import the database with these commands:
 +
 +
<pre>
 +
$ mysql --user=root
 +
</pre>
 +
 +
You should now be at the MySQL prompt:
 +
 +
<pre>
 +
mysql> create database examplestore;
 +
mysql> create user 'examplestore'@'localhost' identified by 'password';
 +
mysql> grant all on examplestore.* to 'examplestore'@'localhost';
 +
</pre>
 +
 +
Now back at the command line do:
 +
 +
<pre>
 +
$ mysql --user=examplestore -p examplestore < examplestore.mysql
 +
</pre>
 +
 +
Unless you changed the password above, the password is just ''password''.  You've now got the same data that the examples use imported to a database called ''examplestore''.

Revision as of 14:31, 25 May 2009

One of the main things that people often want to do is to integrate Directed Edge's recommendation engine into their online store. Many of those stores are using Ruby already, so we decided to give a very concrete walk-through of doing recommendations based on previous customer purchases starting from exporting data all the way to keeping it in sync.

If you want to see all of the code in one go, you can get it in full syntax-highlighted glory here: ExampleStore Class, or download it here. You'll also want our Ruby bindings, which you can get from GitHub here (download).

Creating the testing database

You've probably got your own site's database that you're more interested in, but if you want to follow along in the code, you can use the store data that we randomly generated. We created 2000 users with 500 products and had each of those users "buy" between 0 and 30 products.

For that we created three very simple tables. Since we don't care about the other properties of the customers or products, all that those tables contain is an ID. The purchase table just contains customer and product columns.

customers table:

id

products table:

id

purchases table:

customer product

It really can't get much simpler than that. You can get a dump of the database here. If you have a local MySQL running, you can create and import the database with these commands:

$ mysql --user=root

You should now be at the MySQL prompt:

mysql> create database examplestore;
mysql> create user 'examplestore'@'localhost' identified by 'password';
mysql> grant all on examplestore.* to 'examplestore'@'localhost';

Now back at the command line do:

$ mysql --user=examplestore -p examplestore < examplestore.mysql

Unless you changed the password above, the password is just password. You've now got the same data that the examples use imported to a database called examplestore.