Class: Goliath::Rack::Sprockets

Inherits:
Object
  • Object
show all
Defined in:
lib/goliath/rack/sprockets.rb,
lib/goliath/rack/sprockets/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(app, options) ⇒ Sprockets

Returns a new instance of Sprockets.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/goliath/rack/sprockets.rb', line 8

def initialize(app, options)
  @app     = app
  @options = options

  @options[:root]     ||= Goliath::Application.root_path
  @options[:url_path] ||= "/assets/"

  if not @options.has_key?(:asset_paths)
    raise ArgumentError.new("You should pass the :asset_paths option")
  end

  if @options[:asset_paths].nil? || @options[:asset_paths].empty?
    raise ArgumentError.new("The :asset_paths option is invalid")
  end
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/goliath/rack/sprockets.rb', line 24

def call(env)
  if (asset_path = env['REQUEST_PATH']).start_with?(@options[:url_path])
    logical_name = File.basename(asset_path)

    # Build sprockets environment
    environment = ::Sprockets::Environment.new(@options[:root])
    @options[:asset_paths].each do |asset_path|
      environment.append_path asset_path
    end

    # Lookup the asset
    asset = environment[logical_name]

    if asset
      [200, {
        'Content-Type' => asset.content_type,
        'Last-Modified' => asset.mtime,
        'ETag' => asset.digest
      }, asset.to_s]
    else
      [404, {}, "No such asset (#{logical_name}) found"]
    end
  else
    @app.call(env)
  end
end