Sha256: 6bfb1d9fdd083b43cca81632fa3421fca287ce2144d45d051478fcbde2d15b23

Contents?: true

Size: 1.58 KB

Versions: 54

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
#
require 'sinatra/base'
require 'i18n'
require 'haml'
require 'csv'
require 'picky'
require 'picky-client'
require_relative 'book'
require_relative 'logging'
require_relative 'books_index'
require_relative 'books_search'

# This app shows how to integrate the Picky server directly
# inside a web app. However, if you really need performance
# and easy caching, this is not recommended.
#
class BookSearch < Sinatra::Application

  set :static,        true
  set :public_folder, File.dirname(__FILE__)
  set :views,         File.expand_path('../views', __FILE__)
  set :haml,          :format => :html5

  # Root, the search page.
  #
  get '/' do
    @query = params[:q]

    haml :'/search'
  end

  # Renders the results into the json.
  #
  # You get the results from the (local) picky server and then
  # populate the result hash with rendered models.
  #
  get '/search/full' do
    results = BooksSearch.search params[:query], params[:ids] || 20, params[:offset] || 0
    Picky.logger.info results
    results = results.to_hash
    results.extend Picky::Convenience
    results.populate_with Book do |book|
      book.to_s
    end

    #
    # Or, to populate with the model instances, use:
    #   results.populate_with Book
    #
    # Then to render:
    #   rendered_entries = results.entries.map do |book| (render each book here) end
    #

    Yajl::Encoder.encode results
  end

  # Updates the search count while the user is typing.
  #
  get '/search/live' do
    results = BooksSearch.search params[:query], params[:ids] || 20, params[:offset] || 0
    results.to_json
  end

end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
picky-generators-4.31.3 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.31.2 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.31.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.31.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.30.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.28.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.27.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.27.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.26.2 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.26.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.26.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.25.3 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.25.2 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.25.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.25.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.24.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.23.2 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.23.1 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.23.0 prototypes/all_in_one/sinatra/app.rb
picky-generators-4.22.0 prototypes/all_in_one/sinatra/app.rb