rack-livejs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg
6
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-livejs.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 10.0.4"
7
+
8
+ group :test do
9
+ gem "rack-test", "~> 0.6.2"
10
+ gem "rspec", "~> 2.13.0"
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Williams
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Rack::Livejs
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-livejs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-livejs
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake"
4
+
5
+ task "default" => "spec"
6
+
7
+ require "rspec/core/rake_task"
8
+
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.rspec_opts = ["--format", "nested"]
11
+ end
@@ -0,0 +1,44 @@
1
+ require "rack/livejs/version"
2
+
3
+ module Rack
4
+ class Livejs
5
+
6
+ attr_reader :app
7
+
8
+ def initialize(app, options = {})
9
+ @app, @options = app, options
10
+ end
11
+
12
+ def call(env)
13
+ self.dup._call(env)
14
+ end
15
+
16
+ protected
17
+
18
+ def _call(env)
19
+ status, headers, body = @app.call(env)
20
+ if headers["Content-Type"] == "text/html"
21
+ html = read_all(body)
22
+ modified_html = inject_tag_into(html)
23
+ headers["Content-Length"] = modified_html.size
24
+ body = [modified_html]
25
+ end
26
+ [status, headers, body]
27
+ end
28
+
29
+ def read_all(body)
30
+ html = ""
31
+ body.each { |fragment| html << fragment }
32
+ html
33
+ end
34
+
35
+ def inject_tag_into(html)
36
+ doc = Nokogiri::HTML(html)
37
+ head = doc.css("html head").first
38
+ return html unless head
39
+ head.add_child(%{<script src="http://livejs.com/live.js"/>})
40
+ doc.to_html
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Livejs
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/livejs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-livejs"
8
+ spec.version = Rack::Livejs::VERSION
9
+ spec.authors = ["Mike Williams"]
10
+ spec.email = ["mdub@dogbiscuit.org"]
11
+ spec.homepage = "http://github.com/mdub/rack-livejs"
12
+ spec.license = "MIT"
13
+
14
+ spec.summary = %q{Reload web-pages automatically}
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "nokogiri", ">= 1.4.0"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+
24
+ end
@@ -0,0 +1,8 @@
1
+ <html>
2
+ <head>
3
+ <title>Sample page</title>
4
+ </head>
5
+ <body>
6
+ Sample content
7
+ </body>
8
+ </html>
@@ -0,0 +1 @@
1
+ sample content
@@ -0,0 +1,59 @@
1
+ require 'nokogiri'
2
+ require 'rack'
3
+ require 'rack/livejs'
4
+ require 'rack/test'
5
+ require 'rspec'
6
+
7
+ describe Rack::Livejs do
8
+
9
+ include Rack::Test::Methods
10
+
11
+ let(:decorated_app) do
12
+ fixtures_dir = File.expand_path("../../fixtures", __FILE__)
13
+ Rack::Directory.new(fixtures_dir)
14
+ end
15
+
16
+ let(:app) { described_class.new(decorated_app) }
17
+
18
+ describe "getting an HTML file" do
19
+
20
+ before do
21
+ get "page.html"
22
+ end
23
+
24
+ let(:html) { Nokogiri::HTML(last_response.body) }
25
+
26
+ it "retains the HTML content" do
27
+ expect(html.css("head title").text).to eq("Sample page")
28
+ expect(html.css("body").text.strip).to eq("Sample content")
29
+ end
30
+
31
+ it "injects the live.js script" do
32
+ script_tag = html.css("head script").first
33
+ expect(script_tag).to_not be_nil
34
+ expect(script_tag[:src]).to eq("http://livejs.com/live.js")
35
+ end
36
+
37
+ end
38
+
39
+ describe "getting a non-HTML file" do
40
+
41
+ before do
42
+ get "text.txt"
43
+ end
44
+
45
+ it "returns the expected content" do
46
+ expect(last_response.body).to include("sample content")
47
+ end
48
+
49
+ it "returns the expected content-type" do
50
+ expect(last_response.content_type).to eq "text/plain"
51
+ end
52
+
53
+ it "does not inject live.js" do
54
+ expect(last_response.body).to_not include("live.js")
55
+ end
56
+
57
+ end
58
+
59
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-livejs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ description:
47
+ email:
48
+ - mdub@dogbiscuit.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/rack/livejs.rb
60
+ - lib/rack/livejs/version.rb
61
+ - rack-livejs.gemspec
62
+ - spec/fixtures/page.html
63
+ - spec/fixtures/text.txt
64
+ - spec/rack/livejs_spec.rb
65
+ homepage: http://github.com/mdub/rack-livejs
66
+ licenses:
67
+ - MIT
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -132143474654823442
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ segments:
88
+ - 0
89
+ hash: -132143474654823442
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.23
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Reload web-pages automatically
96
+ test_files:
97
+ - spec/fixtures/page.html
98
+ - spec/fixtures/text.txt
99
+ - spec/rack/livejs_spec.rb