sinatra-jsonp 0.1.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +33 -23
  2. data/lib/sinatra/jsonp.rb +17 -12
  3. metadata +3 -4
data/README.md CHANGED
@@ -1,39 +1,49 @@
1
1
  Sinatra::JSONP
2
2
  =================
3
3
 
4
- JSONP output for [Sinatra](http://sinatrarb.com). Automatically detects callback params
5
- and returns proper JSONP output. If no callback params where detected returns plain JSON.
6
- Works with [jQuery](http://jquery.com) [jQuery.getJSON](http://api.jquery.com/jQuery.getJSON/) function out of the box.
4
+ JSONP output helper for [Sinatra](http://sinatrarb.com). Automatically detects callback params and returns proper JSONP output.
5
+ If no callback params where detected it returns plain JSON.
6
+ Works with [jQuery](http://jquery.com) [jQuery.getJSON](http://api.jquery.com/jQuery.getJSON/) method out of the box.
7
+
7
8
 
8
9
  Installation
9
10
  ------------
10
-
11
- gem install sinatra-jsonp
11
+ <pre>
12
+ sudo gem install sinatra-jsonp
13
+ </pre>
12
14
 
13
15
  Usage
14
16
  -----
17
+ <pre>
18
+ require "sinatra"
19
+ require "sinatra/jsonp"
20
+
21
+ get '/hello' do
22
+ data = ["hello","hi","hallo"]
23
+ JSONP data # JSONP is an alias for jsonp method
24
+ end
25
+
26
+ # define your own callback as second string param
27
+ get '/hi' do
28
+ data = ["hello","hi","hallo"]
29
+ jsonp data, 'functionA'
30
+ end
31
+
32
+ # same with symbol param
33
+ get '/hallo' do
34
+ data = ["hello","hi","hallo"]
35
+ jsonp data, :functionB
36
+ end
37
+ </pre>
38
+
39
+ Links
40
+ -----
15
41
 
16
- Simple example:
17
-
18
- require "sinatra"
19
- require "sinatra/jsonp"
20
-
21
-
22
- get '/users/:user' do
23
- user = User[params[:user]]
24
- JSONP user
25
- end
26
-
27
- # or define your own callback as second param
42
+ * [jQuery](http://jquery.com)
43
+ * [Sinatra](http://www.sinatrarb.com)
28
44
 
29
- get '/' do
30
- data = ["hello","hi","hallo"]
31
- jsonp data, 'functionA'
32
- end
33
45
 
34
46
  License
35
47
  -------
36
48
 
37
49
  sinatra-jsonp is licensed under the MIT license.
38
-
39
-
data/lib/sinatra/jsonp.rb CHANGED
@@ -3,19 +3,24 @@ require 'json'
3
3
 
4
4
  module Sinatra
5
5
  module Jsonp
6
- # Format data according to request JSON/JSONP
7
- def jsonp(obj, callback = nil)
8
- data = obj.to_json
9
- ['callback','jscallback','jsonp'].each do |x|
10
- callback = params.delete(x) if not callback
6
+ def jsonp(*args)
7
+ if args.size > 0
8
+ data = args[0].to_json
9
+ if args.size > 1
10
+ callback = args[1].to_s
11
+ else
12
+ ['callback','jscallback','jsonp'].each do |x|
13
+ callback = params.delete(x) if not callback
14
+ end
15
+ end
16
+ if callback
17
+ content_type :js
18
+ response = "#{callback}(#{data})"
19
+ else
20
+ response = data
21
+ end
22
+ response
11
23
  end
12
- if callback
13
- content_type :js
14
- response = "#{callback}(#{data})"
15
- else
16
- response = data
17
- end
18
- response
19
24
  end
20
25
  alias JSONP jsonp
21
26
  end
metadata CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - Serg Podtynnyi
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-05-07 00:00:00 +04:00
16
+ date: 2010-06-09 00:00:00 +04:00
18
17
  default_executable:
19
18
  dependencies:
20
19
  - !ruby/object:Gem::Dependency