mynyml-rack-js4xhr 0.9

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2009 Martin Aumont (mynyml)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ 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 THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,66 @@
1
+ # --------------------------------------------------
2
+ # based on thin's Rakefile (http://github.com/macournoyer/thin)
3
+ # --------------------------------------------------
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'pathname'
7
+ require 'yaml'
8
+
9
+ RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
10
+ WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
11
+ SUDO = (WIN ? "" : "sudo")
12
+
13
+ def gem
14
+ RUBY_1_9 ? 'gem19' : 'gem'
15
+ end
16
+
17
+ def all_except(res)
18
+ Dir['**/*'].reject do |path|
19
+ Array(res).any? {|re| path.match(re) }
20
+ end
21
+ end
22
+
23
+ spec = Gem::Specification.new do |s|
24
+ s.name = 'rack-js4xhr'
25
+ s.version = '0.9'
26
+ s.summary = "Rack middleware to set application/javascript media type on xhr requests."
27
+ s.description = "Rack middleware to set application/javascript media type on xhr requests."
28
+ s.author = "Martin Aumont"
29
+ s.email = 'mynyml@gmail.com'
30
+ s.homepage = ''
31
+ s.has_rdoc = true
32
+ s.require_path = "lib"
33
+ s.files = all_except([/doc/, /pkg/])
34
+ end
35
+
36
+ desc "Generate rdoc documentation."
37
+ Rake::RDocTask.new("rdoc") { |rdoc|
38
+ rdoc.rdoc_dir = 'doc/rdoc'
39
+ rdoc.title = "Rack::Js4Xhr"
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.options << '--charset' << 'utf-8'
42
+ rdoc.rdoc_files.include('README')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ }
45
+
46
+ Rake::GemPackageTask.new(spec) do |p|
47
+ p.gem_spec = spec
48
+ end
49
+
50
+ desc "Remove package products"
51
+ task :clean => :clobber_package
52
+
53
+ desc "Update the gemspec for GitHub's gem server"
54
+ task :gemspec do
55
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
56
+ end
57
+
58
+ desc "Install gem"
59
+ task :install => [:clobber, :package] do
60
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
61
+ end
62
+
63
+ desc "Uninstall gem"
64
+ task :uninstall => :clean do
65
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
66
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ class Js4Xhr
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ if Rack::Request.new(env).xhr?
10
+ env['HTTP_ACCEPT'] = (env['HTTP_ACCEPT'] || '').split(',').unshift('application/javascript').join(',')
11
+ end
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ require 'pathname'
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'rack'
5
+ begin
6
+ require 'ruby-debug'
7
+ require 'phocus/test_unit'
8
+ rescue LoadError, RuntimeError
9
+ end
10
+
11
+ root = Pathname(__FILE__).dirname.parent.expand_path
12
+ $:.unshift(root.join('lib'))
13
+
14
+ require 'rack/js4xhr'
15
+
16
+ class Test::Unit::TestCase
17
+ def self.test(name, &block)
18
+ name = :"test_#{name.gsub(/\s/,'_')}"
19
+ define_method(name, &block)
20
+ end
21
+ end
22
+
@@ -0,0 +1,39 @@
1
+ require 'test/test_helper'
2
+
3
+ class Rack::MockRequest
4
+ def xhr(uri, opts={})
5
+ get(uri, opts.merge('HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'))
6
+ end
7
+ end
8
+
9
+ App = lambda {|env|
10
+ body = env['HTTP_ACCEPT'] || ''
11
+ [200, {}, [body]]
12
+ }
13
+
14
+ class Js4XhrTest < Test::Unit::TestCase
15
+
16
+ def setup
17
+ @client = Rack::MockRequest.new(Rack::Js4Xhr.new(App))
18
+ end
19
+
20
+ test "prepends js media type to Accept header on xhr request" do
21
+ response = @client.xhr('/', 'HTTP_ACCEPT' => 'text/html')
22
+ assert_equal 'application/javascript,text/html', response.body
23
+ end
24
+
25
+ test "leaves Accept header untouched when not xhr request" do
26
+ response = @client.get('/', 'HTTP_ACCEPT' => 'text/html')
27
+ assert_equal 'text/html', response.body
28
+ end
29
+
30
+ test "nil Accept header" do
31
+ response = @client.xhr('/', 'HTTP_ACCEPT' => nil)
32
+ assert_equal 'application/javascript', response.body
33
+ end
34
+
35
+ test "empty Accept header" do
36
+ response = @client.xhr('/', 'HTTP_ACCEPT' => '')
37
+ assert_equal 'application/javascript', response.body
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mynyml-rack-js4xhr
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.9"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-03 21:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rack middleware to set application/javascript media type on xhr requests.
17
+ email: mynyml@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - test
27
+ - test/test_js4xhr.rb
28
+ - test/test_helper.rb
29
+ - examples
30
+ - examples/app.ru
31
+ - lib
32
+ - lib/rack
33
+ - lib/rack/js4xhr.rb
34
+ - LICENSE
35
+ - README
36
+ has_rdoc: true
37
+ homepage: ""
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Rack middleware to set application/javascript media type on xhr requests.
62
+ test_files: []
63
+