hork 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +29 -0
- data/lib/hork.rb +43 -0
- metadata +115 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 Sam Stephenson
|
2
|
+
Copyright (c) 2010 Josh Peek
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Hork
|
2
|
+
====
|
3
|
+
|
4
|
+
Hork is Rack middleware for transparently compressing JavaScript and
|
5
|
+
CSS assets with the YUI Compressor.
|
6
|
+
|
7
|
+
Sample `config.ru`:
|
8
|
+
|
9
|
+
require "hork"
|
10
|
+
use Hork
|
11
|
+
|
12
|
+
require "your_app"
|
13
|
+
run YourApp
|
14
|
+
|
15
|
+
|
16
|
+
# Installation
|
17
|
+
|
18
|
+
$ gem install hork
|
19
|
+
|
20
|
+
Requires
|
21
|
+
[ruby-yui-compressor](http://github.com/sstephenson/ruby-yui-compressor/)
|
22
|
+
and [Rack](http://rack.rubyforge.org/).
|
23
|
+
|
24
|
+
|
25
|
+
# License
|
26
|
+
|
27
|
+
Copyright (c) 2010 Sam Stephenson.
|
28
|
+
|
29
|
+
Released under the MIT license. See `LICENSE` for details.
|
data/lib/hork.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "digest/md5"
|
2
|
+
require "rack"
|
3
|
+
require "yui/compressor"
|
4
|
+
|
5
|
+
class Hork
|
6
|
+
VERSION = "1.0.0"
|
7
|
+
|
8
|
+
attr_reader :app, :css_compressor, :js_compressor, :compressed_files
|
9
|
+
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
@css_compressor = YUI::CssCompressor.new
|
13
|
+
@js_compressor = YUI::JavaScriptCompressor.new(:munge => true)
|
14
|
+
@compressed_files = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
status, headers, body = response = app.call(env)
|
19
|
+
|
20
|
+
case headers["Content-Type"]
|
21
|
+
when /\bcss\b/ then compress_response(:css, *response)
|
22
|
+
when /\bjavascript\b/ then compress_response(:js, *response)
|
23
|
+
else response
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def compress_response(type, status, headers, body)
|
28
|
+
compressed_body = compress(type, read_body(body))
|
29
|
+
headers["Content-Length"] = Rack::Utils.bytesize(compressed_body).to_s
|
30
|
+
[status, headers, [compressed_body]]
|
31
|
+
end
|
32
|
+
|
33
|
+
def compress(type, source)
|
34
|
+
compressed_files[Digest::MD5.hexdigest(source)] ||=
|
35
|
+
send("#{type}_compressor").compress(source)
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_body(source_body)
|
39
|
+
"".tap do |body|
|
40
|
+
source_body.each { |part| body << part }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hork
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sam Stephenson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-28 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: yui-compressor
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 57
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 9
|
49
|
+
- 1
|
50
|
+
version: 0.9.1
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rack-test
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
description: Rack middleware for compressing JS and CSS assets with YUI Compressor
|
68
|
+
email:
|
69
|
+
- sstephenson@gmail.com
|
70
|
+
executables: []
|
71
|
+
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
76
|
+
files:
|
77
|
+
- lib/hork.rb
|
78
|
+
- README.md
|
79
|
+
- LICENSE
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/sstephenson/hork
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.3.7
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Rack middleware for compressing JS and CSS assets
|
114
|
+
test_files: []
|
115
|
+
|