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

UNCACHED_KEY_INTERVAL =

interval in seconds used to compute the cache key when in uncached mode which can be set by passing in options note: setting it to 0 or a low value will change the cache key every request which means the manifest will never successfully download (since it gets downloaded again at the end)

10
VERSION =
"0.6.4"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Offline.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/offline.rb', line 21

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!
  else
    @cache_interval = (options[:cache_interval] || UNCACHED_KEY_INTERVAL).to_i
  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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rack/offline.rb', line 44

def call(env)
  key = @key || uncached_key

  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