JSONP output helper for Sinatra
Ruby
Latest commit c276dd5 May 17, 2016 @shtirlic Update .travis.yml
Permalink
Failed to load latest commit information.
lib/sinatra Add missing "content_type :json" for pure JSON output May 16, 2013
spec Add optional prettify closes #3 Dec 10, 2012
.gitignore
.rspec
.travis.yml
Gemfile Fix Gemfile May 16, 2013
LICENSE
README.md Add gem badge May 17, 2016
Rakefile Update rakefile for gem taks Dec 10, 2012
sinatra-jsonp.gemspec

README.md

Sinatra::Jsonp Build Status Gem Version

JSONP output helper for Sinatra. Automatically detects callback params and returns proper JSONP output. If callback params where not detected it returns plain JSON. Works with jQuery jQuery.getJSON out of the box.

Installation

Gem install

gem install sinatra-jsonp

Gemfile

gem 'sinatra-jsonp'

Usage

Classic:

require "sinatra"
require "sinatra/jsonp"

get '/hello' do
  data = ["hello","hi","hallo"]
  JSONP data      # JSONP is an alias for jsonp method
end

# define your own callback as second string param
get '/hi' do
  data = ["hello","hi","hallo"]
  jsonp data, 'functionA'
end

# same with symbol param
get '/hallo' do
  data = ["hello","hi","hallo"]
  jsonp data, :functionB
end

Modular:

require "sinatra/base"
require "sinatra/jsonp"

class Foo < Sinatra::Base
  helpers Sinatra::Jsonp

  # Enable JSON pretty output
  #enable :json_pretty

  get '/' do
    data = ["hello","hi","hallo"]
    jsonp data
  end
end

Links

License

MIT License. See LICENSE for details.