fastr-compress 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ fastr-compress
2
+ ================
3
+
4
+ *fastr-compress* is a plugin for [fastr](http://github.com/chrismoos/fastr) that will compress a Rack response body.
5
+
6
+ how it works
7
+ -------------
8
+
9
+ The plugin is called after a Rack response has been returned from your application. It will inspect the **Accept-Encoding** header and attempt to encode the response's body if possible. Currently gzip and deflate are supported.
10
+
11
+ usage
12
+ --------
13
+
14
+ Add *fastr-compress* to your Gemfile
15
+
16
+ source "http://rubygems.org"
17
+ gem 'fastr-compress'
18
+ ...
19
+
20
+ Enable the plugin in your fastr application by modifying your **app/config/settings.rb** file
21
+
22
+ config.plugins << Fastr::Compress
@@ -0,0 +1,68 @@
1
+ begin
2
+ require 'zlib'
3
+ HAS_ZLIB = true
4
+ rescue
5
+ HAS_ZLIB = false
6
+ end
7
+
8
+ require 'stringio'
9
+ require 'fastr/logger'
10
+
11
+ module Fastr::Compress
12
+ include Fastr::Log
13
+
14
+ ACCEPTABLE_ENCODINGS = [:gzip, :deflate]
15
+
16
+ def self.after_boot(app)
17
+ if HAS_ZLIB
18
+ logger.debug "Compression plugin loaded."
19
+ else
20
+ logger.error "Compression plugin could not be loaded (zlib not found)."
21
+ end
22
+ end
23
+
24
+ def self.compress(response, encoding)
25
+ code, hdrs, body = response
26
+
27
+ # Set the content encoding header
28
+ hdrs['Content-Encoding'] = encoding.to_s
29
+
30
+ case encoding
31
+ when :gzip
32
+ fake_io = StringIO.new
33
+ gz = Zlib::GzipWriter.new(fake_io, Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_STRATEGY)
34
+ gz.write(body.join(""))
35
+ gz.close
36
+ [code, hdrs, [fake_io.string]]
37
+ when :deflate
38
+ z = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION)
39
+ dst = z.deflate(body.join(""), Zlib::FINISH)
40
+ z.close
41
+ [code, hdrs, [dst.to_s]]
42
+ else
43
+ raise StandardError.new("not an acceptable encoding format: #{encoding}")
44
+ end
45
+ end
46
+
47
+ def self.after_dispatch(app, env, response)
48
+ return response if not HAS_ZLIB
49
+ return response if env['HTTP_ACCEPT_ENCODING'].nil?
50
+
51
+ code, hdrs, body = response
52
+
53
+ # Check to see what the acceptable content encodings are
54
+ accept_encodings = env['HTTP_ACCEPT_ENCODING'].split(/,/).collect { |e| e.downcase.to_sym }
55
+ encoding_format = nil
56
+
57
+ accept_encodings.each do |enc|
58
+ if ACCEPTABLE_ENCODINGS.include? enc
59
+ encoding_format = enc
60
+ break
61
+ end
62
+ end
63
+
64
+ return response if encoding_format.nil?
65
+
66
+ compress(response, encoding_format)
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ module Fastr
2
+ module Compress
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastr-compress
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Chris Moos
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-26 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: fastr
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 1
31
+ - 0
32
+ version: 0.1.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Compresses output with GZip or Deflate for responses.
36
+ email:
37
+ - chris@tech9computers.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/fastr/compress/version.rb
46
+ - lib/fastr/compress.rb
47
+ - README.md
48
+ has_rdoc: true
49
+ homepage: http://github.com/chrismoos/fastr-compress
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 3
73
+ - 6
74
+ version: 1.3.6
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: fastr plugin for compressing output
82
+ test_files: []
83
+