jsonp 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,16 @@
1
+ require 'rbconfig'
2
+
3
+ class JsonpGenerator < Rails::Generator::Base
4
+ def manifest
5
+ record do |m|
6
+ m.directory "public/javascripts"
7
+ m.file "javascripts/jsonp.js", "public/javascripts/jsonp.js"
8
+ end
9
+ end
10
+
11
+ protected
12
+
13
+ def banner
14
+ "Usage: #{$0} jsonp"
15
+ end
16
+ end
@@ -0,0 +1,124 @@
1
+ /*
2
+ * Based on jQuery JSONP Core Plugin by Julian Aubourg.
3
+ * Modified by Jakub Kuźma.
4
+ */
5
+
6
+ var jsonp = function(uri, options) {
7
+ // References to options members
8
+ var done = false,
9
+ id = jsonp.id++,
10
+ onComplete = options.complete,
11
+ onSuccess = options.success,
12
+ onFailure = options.failure,
13
+ callback = options.callback || "_callback",
14
+ data = options.data,
15
+ timeout = options.timeout,
16
+ timeoutTimer,
17
+ frame;
18
+
19
+ // YUI3 Function that concatenates string data for HTTP GET transactions.
20
+ function paramConcat(s, d) {
21
+ s += ((s.indexOf('?') == -1) ? '?' : '&') + d;
22
+ return s;
23
+ }
24
+
25
+ // Abort transaction
26
+ var doAbort = function() {
27
+ doRespond({ status: 0, type: "Abort" });
28
+ };
29
+
30
+ // Transaction timeout
31
+ var doTimeout = function() {
32
+ doRespond({ status: 0, type: "Timeout" });
33
+ };
34
+
35
+ // Iframe variable cleaning function
36
+ var doRemoveFrameVariable = function(frame, name) {
37
+ frame.contentWindow[name] = undefined;
38
+ try { delete frame.contentWindow[name]; } catch(_) {}
39
+ };
40
+
41
+ // Clean up function (declaration)
42
+ var doCleanUp = function() {
43
+ // Clear the timeout (is it exists)
44
+ clearTimeout(timeoutTimer);
45
+ // Open the iframes document & clean
46
+ frame.contentWindow.document.open();
47
+ doRemoveFrameVariable(frame, "e");
48
+ doRemoveFrameVariable(frame, "s");
49
+ frame.contentWindow.document.write("");
50
+ frame.contentWindow.document.close();
51
+ document.body.removeChild(frame);
52
+ };
53
+
54
+ var doRespond = function(response) {
55
+ if(!done) {
56
+ done = true;
57
+
58
+ response.id = id;
59
+
60
+ doCleanUp();
61
+
62
+ if(onComplete) {
63
+ onComplete(response);
64
+ }
65
+ if(response.status >= 200 && response.status < 300) {
66
+ if(onSuccess) {
67
+ onSuccess(response);
68
+ }
69
+ } else {
70
+ if(onFailure) {
71
+ onFailure(response);
72
+ }
73
+ }
74
+ }
75
+ };
76
+
77
+ // Add data from options
78
+ if(data && data.length) {
79
+ uri = paramConcat(uri, data);
80
+ }
81
+
82
+ // Add callback parameter
83
+ uri = paramConcat(uri, callback + "=" + "s");
84
+
85
+ // Prevent caching
86
+ uri = paramConcat(uri, "_=" + (new Date()).getTime());
87
+
88
+ // Create an iframe & add it to the document
89
+ frame = document.createElement("iframe");
90
+ frame.style.display = "none";
91
+ document.body.appendChild(frame);
92
+
93
+ // We have to open the document before declaring variables in the
94
+ // iframe's window
95
+ frame.contentWindow.document.open();
96
+
97
+ // Install callbacks
98
+ frame.contentWindow["s"] = function(response) {
99
+ doRespond(response);
100
+ };
101
+
102
+ frame.contentWindow["e"] = function() {
103
+ doRespond({ status: 0, type: "Unknown" });
104
+ };
105
+
106
+ // Write to the document
107
+ frame.contentWindow.document.write('<html><body onload="e()"><script src="' + uri + '"></script></body></html>');
108
+
109
+ // close (makes some browsers happier)
110
+ frame.contentWindow.document.close();
111
+
112
+ // If a timeout is needed, install it
113
+ if(timeout > 0) {
114
+ timeoutTimer = setTimeout(doTimeout, timeout);
115
+ }
116
+
117
+ return {
118
+ id: id,
119
+ abort: doAbort
120
+ };
121
+ };
122
+
123
+ // Reset request counter
124
+ jsonp.id = 0;
@@ -9,7 +9,7 @@ module Jsonp
9
9
 
10
10
  def call(env)
11
11
  status, headers, response = @app.call(env)
12
- callback = Utils.extract_callback(env)
12
+ callback = Utils.extract_callback(env, @callback_param)
13
13
 
14
14
  if callback
15
15
  prefix, suffix = "#{callback}(", ")"
@@ -9,8 +9,8 @@ module Jsonp
9
9
  end
10
10
 
11
11
  def call(env)
12
- callback = Utils.extract_callback(env)
13
- method = Utils.extract_method(env)
12
+ callback = Utils.extract_callback(env, @callback_param)
13
+ method = Utils.extract_method(env, @method_param)
14
14
 
15
15
  if env["REQUEST_METHOD"] == "GET" && callback && method
16
16
  env["REQUEST_METHOD"] = method
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jakub Ku\xC5\xBAma"
@@ -38,6 +38,8 @@ files:
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - VERSION
41
+ - generators/jsonp/jsonp_generator.rb
42
+ - generators/jsonp/templates/javascripts/jsonp.js
41
43
  - lib/jsonp.rb
42
44
  - lib/jsonp/callback_wrapper.rb
43
45
  - lib/jsonp/method_override.rb