rack-iframe-transport 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +18 -0
  2. data/README.md +40 -0
  3. data/lib/rack_iframe_transport.rb +44 -0
  4. metadata +47 -0
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 Charles Barbier <http://github.com/unixcharles>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ rack-iframe-transport
2
+ =====================
3
+
4
+ Rack middleware for iframe-transport hacks.
5
+
6
+ Get Rails to work nicely with [jquery-iframe-transport](https://github.com/cmlenz/jquery-iframe-transport/).
7
+ Post data to an iframe and retrive data from there is still the only reliable way to post file in a javascript application.
8
+
9
+ This middleware get Rails to work nicely with [jquery-iframe-transport](https://github.com/cmlenz/jquery-iframe-transport/) or other code of yours that exploit this hack.
10
+
11
+ What it does?
12
+ -------------
13
+
14
+ It wrap the response in a html. The original response is insert inside a `<textarea>` tag.
15
+
16
+ Since you can't get headers from an iframe post, metadata are exposed as `'data-*'` attributes of the textarea tag.
17
+
18
+ ```
19
+ > curl localhost:3000/api/2/account
20
+ {"message":"Unauthorized"}
21
+
22
+ > curl 'localhost:3000/api/2/account?X-Requested-With=IFrame'
23
+ <!DOCTYPE html><html><body><textarea 'data-status'='401' 'data-statusText'='Unauthorized' 'data-type'='text/html'>{"message":"Unauthorized"}</textarea></body></html>
24
+ ```
25
+
26
+ How?
27
+ ----
28
+
29
+ Something like this, its just a Rack middleware.
30
+
31
+ ```ruby
32
+ # config/initializer/iframe_transport.rb
33
+ require 'rack_iframe_transport'
34
+ Teambox::Application.config.middleware.use Rack::IframeTransport
35
+ ```
36
+
37
+ Pull request?
38
+ -------------
39
+
40
+ Yes.
@@ -0,0 +1,44 @@
1
+ module Rack
2
+ class IframeTransport
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ dup._call(env)
9
+ end
10
+
11
+ def _call(env)
12
+ @status, @headers, @response = @app.call(env)
13
+ @request = Rack::Request.new(env)
14
+ @headers['Content-Type'] = 'text/html' if iframe_transport?
15
+ [@status, @headers, self]
16
+ end
17
+
18
+ def each(&block)
19
+ block.call(html_document_left) if iframe_transport?
20
+ @response.each(&block)
21
+ block.call(html_document_right) if iframe_transport?
22
+ end
23
+
24
+ def iframe_transport?
25
+ @request.params['X-Requested-With'] == 'IFrame'
26
+ end
27
+
28
+ def html_document_left
29
+ "<!DOCTYPE html><html><body><textarea #{metadata}>"
30
+ end
31
+
32
+ def html_document_right
33
+ "</textarea></body></html>"
34
+ end
35
+
36
+ def metadata
37
+ meta = {}
38
+ meta[:'data-status'] = @response.status if @response.respond_to? :status
39
+ meta[:'data-statusText'] = @response.status_message if @response.respond_to? :status_message
40
+ meta[:'data-type'] = @headers['Content-Type'] if @headers.has_key?('Content-Type')
41
+ meta.map {|key,value| "'#{key}'='#{value}'" }.join(' ')
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-iframe-transport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Barbier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: unixcharles@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - LICENSE
22
+ - lib/rack_iframe_transport.rb
23
+ homepage: http://github.com/unixcharles/rack-iframe-transport
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Rack middleware for iframe-transport hacks.
47
+ test_files: []