Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

JSONP support #4

Merged
merged 2 commits into from
May 10, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ And viewing the service in your browser: <a href="http://localhost:5678/search?t
}
}

The `/search` method supports multiple `types` as well as an optional `limit`. For example: `http://localhost:5678/search?types[]=event&types[]=venue&types[]=performer&limit=3&term=yank`.
The `/search` method supports multiple `types` as well as an optional `limit`. For example: `http://localhost:5678/search?types[]=event&types[]=venue&types[]=performer&limit=3&term=yank`. You can also add the `callback` parameter to enable JSONP output.

Contributing to soulmate
------------------------
Expand Down
15 changes: 10 additions & 5 deletions lib/soulmate/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ module Soulmate
class Server < Sinatra::Base
include Helpers

before do
content_type 'application/json', :charset => 'utf-8'
def handle_jsonp(data)
if params[:callback]
content_type 'text/javascript', :charset => 'utf-8'
"#{params[:callback]}(#{data})"
else
content_type 'application/json', :charset => 'utf-8'
data
end
end

get '/' do
Expand All @@ -27,15 +33,14 @@ class Server < Sinatra::Base
results[type] = matcher.matches_for_term(term, :limit => limit)
end

JSON.pretty_generate({
handle_jsonp JSON.pretty_generate({
:term => params[:term],
:results => results
})
end

not_found do
content_type 'application/json', :charset => 'utf-8'
JSON.pretty_generate({ :error => "not found" })
handle_jsonp JSON.pretty_generate({ :error => "not found" })
end

end
Expand Down