Class: Rack::Offline

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/offline.rb,
lib/rack/offline/config.rb,
lib/rack/offline/version.rb

Constant Summary

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Offline

Returns a new instance of Offline



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/offline.rb', line 14

def initialize(options = {}, &block)
  @cache    = options[:cache]

  @logger   = options[:logger] || begin
    ::Logger.new(STDOUT).tap {|logger| logger.level = 1 }
  end

  @root     = Pathname.new(options[:root] || Dir.pwd)

  if block_given?
    @config = Rack::Offline::Config.new(@root, &block)
  end

  if @cache
    raise "In order to run Rack::Offline in cached mode, " \
          "you need to supply a root so Rack::Offline can " \
          "calculate a hash of the files." unless @root
    precache_key!
  end
end

Class Method Details

.configure(*args, &block) ⇒ Object



10
11
12
# File 'lib/rack/offline.rb', line 10

def self.configure(*args, &block)
  new(*args, &block)
end

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/offline.rb', line 35

def call(env)
  key = @key || Digest::SHA2.hexdigest(Time.now.to_s + Time.now.usec.to_s)

  body = ["CACHE MANIFEST"]
  body << "# #{key}"
  @config.cache.each do |item|
    body << URI.escape(item.to_s)
  end

  unless @config.network.empty?
    body << "" << "NETWORK:"
    @config.network.each do |item|
      body << URI.escape(item.to_s)
    end
  end

  unless @config.fallback.empty?
    body << "" << "FALLBACK:"
    @config.fallback.each do |namespace, url|
      body << "#{namespace} #{URI.escape(url.to_s)}"
    end
  end

  @logger.debug body.join("\n")

  [200, {"Content-Type" => "text/cache-manifest"}, body.join("\n")]
end