Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shtirlic/sinatra-jsonp
JSONP output helper for Sinatra
https://github.com/shtirlic/sinatra-jsonp
Last synced: about 2 months ago
JSON representation
JSONP output helper for Sinatra
- Host: GitHub
- URL: https://github.com/shtirlic/sinatra-jsonp
- Owner: shtirlic
- License: mit
- Created: 2010-05-03T12:12:11.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2023-01-25T19:53:05.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T16:59:49.031Z (2 months ago)
- Language: Ruby
- Homepage: http://podtynnyi.com/2010/05/28/sinatra-jsonp-output-helper/
- Size: 28.3 KB
- Stars: 60
- Watchers: 4
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sinatra::Jsonp [![Gem Version](https://badge.fury.io/rb/sinatra-jsonp.svg)](https://badge.fury.io/rb/sinatra-jsonp)
JSONP output helper for [Sinatra](http://sinatrarb.com). Automatically detects callback params and returns proper JSONP output.
If callback params where not detected it returns plain JSON.
Works with [jQuery](http://jquery.com) [jQuery.getJSON](http://api.jquery.com/jQuery.getJSON/) out of the box.## Installation
Gem install
```bash
gem install sinatra-jsonp
```Gemfile
```ruby
gem 'sinatra-jsonp'
```## Usage
Classic:
```ruby
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:```ruby
require "sinatra/base"
require "sinatra/jsonp"class Foo < Sinatra::Base
helpers Sinatra::Jsonp# Enable JSON pretty output
#enable :json_prettyget '/' do
data = ["hello","hi","hallo"]
jsonp data
end
end
```## Links
* [jQuery](http://jquery.com)
* [Sinatra](http://www.sinatrarb.com)## License
MIT License. See LICENSE for details.