Created
February 2, 2012 17:19
-
-
Save anonymous/1724664 to your computer and use it in GitHub Desktop.
Usage example of bol Ruby gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require 'bol' | |
Bol.configure do |c| | |
c.access_key = 'foo' | |
c.secret = 'bar' | |
end | |
get '/' do | |
@categories = Bol.categories | |
erb :search | |
end | |
post '/search' do | |
@products = Bol::Scope.new(params[:category_id]).search(params[:q]) | |
erb :results | |
end | |
get '/product/:id' do | |
@product = Bol::Product.find(params[:id]) | |
erb :product | |
end | |
__END__ | |
@@ layout | |
<!doctype html> | |
<html> | |
<head><title>Search bol.com</title></head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
@@ search | |
<h1>Search bol.com</h1> | |
<form method="post" action="/search"> | |
<label>Category: | |
<select name="category_id"> | |
<% @categories.each do |category| %> | |
<option value="<%= category.id %>"><%= category.name %> (<%= category.count %>)</option> | |
<% end %> | |
</select> | |
</label> | |
<label>Query: <input type="text" name="q"></label> | |
<input type="submit"> | |
</form> | |
@@ results | |
<h1>Search results for <%= params[:q] %></h1> | |
<ol> | |
<% @products.each do |product| %> | |
<li><a href="/product/<%= product.id %>"><%= product.title %></a></li> | |
<% end %> | |
</ol> | |
@@ product | |
<h1><%= @product.title %></h1> | |
<dl> | |
<% @product.attributes.each_pair do |k, v| %> | |
<dt><%= k %></dt> | |
<dd><pre><%= v %></pre></dd> | |
<% end %> | |
</dl> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment