nbio-css_http_request 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Cameron Walters (cee-dub.info)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ MIT-LICENSE
2
+ Manifest.txt
3
+ README.markdown
4
+ Rakefile
5
+ css_http_request.gemspec
6
+ init.rb
7
+ install.rb
8
+ lib/css_http_request.rb
9
+ lib/css_http_request_ext.rb
10
+ lib/css_http_request_handler.rb
11
+ rails/init.rb
12
+ test/css_http_request_test.rb
13
+ test/test_helper.rb
@@ -0,0 +1,13 @@
1
+ CssHttpRequest
2
+ ==============
3
+
4
+ (TBA)
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ (also TBA)
11
+
12
+
13
+ Copyright (c) 2008 Cameron Walters, released under the MIT license
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the css_http_request plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the css_http_request plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'CssHttpRequest'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,32 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'css_http_request'
3
+ s.version = '0.1'
4
+ s.date = '2009-01-09'
5
+ s.summary = "CSSHttpRequest is cross-domain AJAX using CSS"
6
+ s.email = 'ping@nb.io'
7
+ s.homepage = 'http://nb.io/hacks/csshttprequest/'
8
+ s.description = "Like JavaScript includes, this works because CSS is not subject to the same-origin policy that affects XMLHttpRequest. CSSHttpRequest functions similarly to JSONP, and is limited to making GET requests. Unlike JSONP, untrusted third-party JavaScript cannot execute in the context of the calling page."
9
+ s.has_rdoc = true
10
+ s.authors = ['Cameron Walters', 'Randy Reddig']
11
+ s.files = [
12
+ "MIT-LICENSE",
13
+ "Manifest.txt",
14
+ "README.markdown",
15
+ "Rakefile",
16
+ "css_http_request.gemspec",
17
+ "init.rb",
18
+ "install.rb",
19
+ "lib/css_http_request.rb",
20
+ "lib/css_http_request_ext.rb",
21
+ "lib/css_http_request_handler.rb",
22
+ "rails/init.rb",
23
+ ]
24
+ s.test_files = [
25
+ "test/css_http_request_test.rb",
26
+ "test/test_helper.rb",
27
+ ]
28
+ s.rdoc_options = ["--main", "README.markdown"]
29
+ s.extra_rdoc_files = ["README.markdown"]
30
+ s.add_dependency 'rails', ['>= 2.1']
31
+ end
32
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "rails", "init")
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,35 @@
1
+ # CSSHttpRequest Ruby Encoder
2
+ #
3
+ # Author: Cameron Walters <cameron@nb.io>
4
+ # License: Apache License 2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
5
+ # Copyright (c) 2008, Cameron Walters
6
+
7
+
8
+ require "erb"
9
+ require "enumerator"
10
+
11
+ module CssHttpRequest
12
+ PREFIX = "data:,".freeze
13
+ LENGTH = (2000 - PREFIX.size).freeze # Internet Explorer 2KB URI limit
14
+
15
+ def encode_chr(str)
16
+ quoted = ERB::Util.url_encode(str)
17
+ slice_num = 0
18
+ last_start = 0
19
+ chunks = (quoted.length.to_f / LENGTH).ceil
20
+ STDERR << chunks
21
+ output = ""
22
+ chunks.times do |n|
23
+ finish = last_start + LENGTH
24
+ output += "#c%d{background:url(%s%s);}\n" % [slice_num, PREFIX, quoted[last_start...finish]]
25
+ slice_num += 1
26
+ last_start = finish
27
+ end
28
+ output
29
+ end
30
+ end
31
+
32
+ if __FILE__ == $PROGRAM_NAME
33
+ include CssHttpRequest
34
+ puts encode_chr(STDIN.read)
35
+ end
@@ -0,0 +1,27 @@
1
+ module CssHttpRequestExt
2
+ def self.included(klass)
3
+ klass.prepend_before_filter :adjust_format_for_css_http_request
4
+ end
5
+
6
+ def adjust_format_for_css_http_request
7
+ request.format = :chr if css_http_request?
8
+ request.format = :jsonc if json_css_http_request?
9
+ end
10
+
11
+ def css_http_request?
12
+ return params[:format] == "chr"
13
+ end
14
+
15
+ def json_css_http_request?
16
+ return params[:format] == "jsonc"
17
+ end
18
+
19
+ def render_with_chr(options = nil, extra_options = {}, &block)
20
+ if options.is_a?(Hash) && options.has_key?(:chr)
21
+ output = render_to_string(options.merge!(options.delete(:chr)), &block)
22
+ render :text => output, :mime_type => Mime::Type.lookup_by_extension(params[:format])
23
+ else
24
+ render_without_chr(options, extra_options, &block)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require "css_http_request"
2
+
3
+ class CssHttpRequestHandler < ActionView::TemplateHandler
4
+ include CssHttpRequest
5
+
6
+ def self.call(template)
7
+ "CssHttpRequestHandler.new(self).render(template, local_assigns)"
8
+ end
9
+
10
+ def initialize(action_view)
11
+ @action_view = action_view
12
+ end
13
+
14
+ def render(template, local_assigns = {})
15
+ prepare_view(local_assigns)
16
+ encode_chr(template.source)
17
+ end
18
+
19
+ private
20
+
21
+ def prepare_view(local_assigns)
22
+ @action_view.controller.headers["Content-Type"] ||= 'text/css'
23
+ @action_view.controller.instance_variables.each do |v|
24
+ instance_variable_set(v, @action_view.controller.instance_variable_get(v))
25
+ end
26
+ end
27
+ end
28
+
29
+ class CssJsonHttpRequestHandler < CssHttpRequestHandler
30
+ def self.call(template)
31
+ "CssJsonHttpRequestHandler.new(self).render(template, local_assigns)"
32
+ end
33
+
34
+ def render(template, local_assigns = {})
35
+ prepare_view(local_assigns)
36
+ output = eval(template.source, nil, '')
37
+ encode_chr(output.to_json)
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ require "css_http_request"
2
+ require "css_http_request_handler"
3
+
4
+ ActionController::Base.class_eval do
5
+ send(:include, CssHttpRequest)
6
+ send(:include, CssHttpRequestExt)
7
+ end
8
+ ActionController::Base.send :alias_method_chain, :render, :chr
9
+
10
+ ActionView::Template.register_template_handler :chr, CssHttpRequestHandler
11
+ ActionView::Template.register_template_handler :jsonc, CssJsonHttpRequestHandler
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class CssHttpRequestTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nbio-css_http_request
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Walters
8
+ - Randy Reddig
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-01-09 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.1"
24
+ version:
25
+ description: Like JavaScript includes, this works because CSS is not subject to the same-origin policy that affects XMLHttpRequest. CSSHttpRequest functions similarly to JSONP, and is limited to making GET requests. Unlike JSONP, untrusted third-party JavaScript cannot execute in the context of the calling page.
26
+ email: ping@nb.io
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - MIT-LICENSE
35
+ - Manifest.txt
36
+ - README.markdown
37
+ - Rakefile
38
+ - css_http_request.gemspec
39
+ - init.rb
40
+ - install.rb
41
+ - lib/css_http_request.rb
42
+ - lib/css_http_request_ext.rb
43
+ - lib/css_http_request_handler.rb
44
+ - rails/init.rb
45
+ has_rdoc: true
46
+ homepage: http://nb.io/hacks/csshttprequest/
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --main
50
+ - README.markdown
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: CSSHttpRequest is cross-domain AJAX using CSS
72
+ test_files:
73
+ - test/css_http_request_test.rb
74
+ - test/test_helper.rb