Class: Rack::Offline

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

Direct Known Subclasses

Rails::Offline

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"0.6.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Offline.



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

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



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

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

Instance Method Details

#call(env) ⇒ Object



34
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
# File 'lib/rack/offline.rb', line 34

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 << item
  end

  unless @config.network.empty?
    body << "" << "NETWORK:"
    @config.network.each do |item|
      body << item
    end
  end

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

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

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