Sha256: d7a4006af693ab543680a6660354b39354f5ca490120c4b36665a550b30679c8

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

require 'sinatra/base'
require 'soulmate'
require 'rack/contrib'

module Soulmate

  class Server < Sinatra::Base
    include Helpers
    
    use Rack::JSONP
    
    before do
      content_type 'application/json', :charset => 'utf-8'
    end
    
    get '/' do
      MultiJson.encode({ :soulmate => Soulmate::Version::STRING, :status   => "ok" })
    end
    
    get '/search' do
      raise Sinatra::NotFound unless (params[:term] and params[:types] and params[:types].is_a?(Array))
      
      limit = (params[:limit] || 5).to_i
      types = params[:types].map { |t| normalize(t) }
      term  = params[:term]
      
      results = {}
      types.each do |type|
        matcher = Matcher.new(type)
        results[type] = matcher.matches_for_term(term, :limit => limit)
      end
      
      MultiJson.encode({
        :term    => params[:term],
        :results => results
      })
    end
    
    not_found do
      content_type 'application/json', :charset => 'utf-8'
      MultiJson.encode({ :error => "not found" })
    end
    
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
soulmate-1.1.0 lib/soulmate/server.rb
soulmate-1.0.0 lib/soulmate/server.rb
soulmate-0.1.3 lib/soulmate/server.rb
soulmate-0.1.2 lib/soulmate/server.rb
soulmate-0.1.1 lib/soulmate/server.rb
soulmate-0.1.0 lib/soulmate/server.rb
soulmate-0.0.6 lib/soulmate/server.rb
soulmate-0.0.5 lib/soulmate/server.rb