Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Created December 14, 2010 17:23
Show Gist options
  • Select an option

  • Save reactormonk/740739 to your computer and use it in GitHub Desktop.

Select an option

Save reactormonk/740739 to your computer and use it in GitHub Desktop.
require 'bundler'
require 'ostruct'
Bundler.require
# Load the "model".
#
require File.expand_path 'card', File.dirname(__FILE__)
set :haml, { :format => :html5 }
# Sets up two query instances.
#
FullCards = Picky::Client::Full.new :host => 'localhost', :port => 8080, :path => '/cards/full'
LiveCards = Picky::Client::Live.new :host => 'localhost', :port => 8080, :path => '/cards/live'
set :static, true
set :public, File.dirname(__FILE__)
set :views, File.expand_path('views', File.dirname(__FILE__))
# Root, the search interface.
#
get '/' do
haml :search
end
# Configure. The configuration info page.
#
get '/configure' do
haml :configure
end
# For full results, you get the ids from the picky server
# and then populate the result with models (rendered, even).
#
get '/search/full' do
results = FullCards.search :query => params[:query], :offset => params[:offset]
results.extend Picky::Convenience
Card.find(results.ids) do |card|
Tilt[:haml].new('./views/card.haml').render(OpenStruct.new(card: card))
end
#
# Or use:
# results.populate_with Book
#
# Then:
# rendered_entries = results.entries.map do |book| (render each book here) end
#
ActiveSupport::JSON.encode results
end
# For live results, you'd actually go directly to the search server without taking the detour.
#
get '/search/live' do
LiveCards.search :query => params[:query], :offset => params[:offset]
end
helpers do
def js path
"<script src='javascripts/#{path}.js' type='text/javascript'></script>"
end
end
CardDB = CouchRest.database "http://127.0.0.1:5984/magic_cards"
class Card < Struct.new :id, :gatherer_id, :name, :mana, :types, :subtypes, :textboxes, :flavor, :power, :toughness, :edition, :rarity, :number
# Find uses a lookup table.
#
def self.find ids, _ = {}
Array(ids).map do |id|
new CardDB.get id.to_s(16)
end
end
def initialize hash
hash.each do |k,v|
if members.include? k.to_sym
self[k] = v
end
end
self.id = hash['_id']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment