rack-offline 0.5.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Yehuda Katz
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.
21
+
@@ -0,0 +1,45 @@
1
+ require "rack/offline"
2
+
3
+ module Rails
4
+ class Offline < Rack::Offline
5
+ def self.call(env)
6
+ @app ||= new
7
+ @app.call(env)
8
+ end
9
+
10
+ def initialize(app = Rails.application, &block)
11
+ config = app.config
12
+ root = Rails.public_path
13
+
14
+ block = cache_block unless block_given?
15
+
16
+ opts = {
17
+ :cache => config.cache_classes,
18
+ :root => root,
19
+ :logger => Rails.logger
20
+ }
21
+
22
+ super opts, &block
23
+ end
24
+
25
+ private
26
+
27
+ def cache_block
28
+ Proc.new do
29
+ files = Dir[
30
+ "#{root}/**/*.html"
31
+ "#{root}/stylesheets/**/*.css",
32
+ "#{root}/javascripts/**/*.js",
33
+ "#{root}/images/**"]
34
+
35
+ files.each do |file|
36
+ cache file.relative_path_from(root)
37
+ end
38
+
39
+ cache *files
40
+ network "/"
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,72 @@
1
+ require "rack/offline/config"
2
+ require "rack/offline/version"
3
+ require "digest/sha2"
4
+ require "logger"
5
+ require "pathname"
6
+
7
+ module Rack
8
+ class Offline
9
+ def self.configure(*args, &block)
10
+ new(*args, &block)
11
+ end
12
+
13
+ def initialize(options = {}, &block)
14
+ @cache = options[:cache]
15
+
16
+ @logger = options[:logger] || begin
17
+ ::Logger.new(STDOUT).tap {|logger| logger.level = 1 }
18
+ end
19
+
20
+ @root = Pathname.new(options[:root] || Dir.pwd)
21
+
22
+ if block_given?
23
+ @config = Rack::Offline::Config.new(&block)
24
+ end
25
+
26
+ if @cache
27
+ raise "In order to run Rack::Offline in cached mode, " \
28
+ "you need to supply a root so Rack::Offline can " \
29
+ "calculate a hash of the files." unless @root
30
+ precache_key!
31
+ end
32
+ end
33
+
34
+ def call(env)
35
+ key = @key || Digest::SHA2.hexdigest(Time.now.to_s + Time.now.usec.to_s)
36
+
37
+ body = ["CACHE MANIFEST"]
38
+ body << "# #{key}"
39
+ @config.cached.each do |item|
40
+ body << item
41
+ end
42
+
43
+ unless @config.network.empty?
44
+ body << "" << "NETWORK:"
45
+ @config.network.each do |item|
46
+ body << item
47
+ end
48
+ end
49
+
50
+ unless @config.fallback.empty?
51
+ body << "" << "FALLBACK:"
52
+ @config.fallback.each do |namespace, url|
53
+ body << "#{namespace} #{url}"
54
+ end
55
+ end
56
+
57
+ @logger.debug body.join("\n")
58
+
59
+ [200, {"Content-Type" => "text/cache-manifest"}, body.join("\n")]
60
+ end
61
+
62
+ private
63
+
64
+ def precache_key!
65
+ hash = @config.cached.map do |item|
66
+ Digest::SHA2.hexdigest(@root.join(item).read)
67
+ end
68
+
69
+ @key = Digest::SHA2.hexdigest(hash.join)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ module Rack
2
+ class Offline
3
+ class Config
4
+ attr_reader :cached, :network, :fallback
5
+
6
+ def initialize(&block)
7
+ @cached = []
8
+ @network = []
9
+ @fallback = {}
10
+ instance_eval(&block) if block_given?
11
+ end
12
+
13
+ def cache(*names)
14
+ @cached.concat(names)
15
+ end
16
+
17
+ def network(*names)
18
+ @network.concat(names)
19
+ end
20
+
21
+ def fallback(hash = {})
22
+ @fallback.merge(hash)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Offline
3
+ VERSION = "0.5.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-offline
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 1
9
+ version: 0.5.1
10
+ platform: ruby
11
+ authors:
12
+ - Yehuda Katz
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A Rack endpoint that generates cache manifests and other useful HTML5 offline utilities that are useful on the server-side. Rack::Offline also provides a conventional Rails endpoint (Rails::Offline) that configures Rack::Offline using expected Rails settings
22
+ email: wycats@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - LICENSE
31
+ - lib/rack/offline/config.rb
32
+ - lib/rack/offline/version.rb
33
+ - lib/rack/offline.rb
34
+ - lib/rack-offline.rb
35
+ has_rdoc: true
36
+ homepage: http://www.yehudakatz.com
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project: rack-offline
61
+ rubygems_version: 1.3.6
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Rack toolkit for working with offline applications
65
+ test_files: []
66
+