Friday, August 22, 2008

Bossman Gem

So I recently needed to implement a site wide search and happened to find this ruby gem at the right time (http://github.com/jpignata/bossman-gem/tree/master). I did have to mess with a few things to get it working the way I wanted it so I figured I would share my example here.

First you need to add the git hub sources and install the gem

gem sources -a http://gems.github.com
gem install jpignata-bossman


Next you'll want to create a new initializer, I called mine boss.rb and put this code in it:

require 'bossman'
include BOSSMan
BOSSMan.application_id = "your_yahoo_api_key"


Next you'll want to create a search controller in your application, then you will want put this code in the controller for your action that will handle the results.

@search = BOSSMan::Search.web("#{params[:your_form][:your_search_field]}", 0, 50)

The 50 is the number of results that you want back. I do not store the search results or do any pagination in this example.

Lastly you'll want to code up your views. Here is a sample of my results view and my search form:

*** Search Form ******

<% form_tag :action => "search_results" do %>


<%= label :search_form, :find %>:
<%= text_field :search_form, :find %>



<%= submit_tag 'Search' %>


<% end %>

*** Results View *****

<% if @search.count.to_i > 0 %>
<% @search.results.each do |result| %>


<%= link_to result.title, result.url %>

<%= result.abstract %>


<% end %>
<% else %>
No results found
<% end %>