Class: Gluey::RackMountable

Inherits:
Object
  • Object
show all
Defined in:
lib/gluey/base/rack_mountable.rb

Defined Under Namespace

Classes: FileBody

Constant Summary collapse

ALLOWED_HEADERS =
%w[GET HEAD].freeze
PATH_PARSER =
%r_^/(\w+)/([^\.]+)\.(?:([a-f0-9]{32})\.)?\w+$_

Instance Method Summary collapse

Constructor Details

#initialize(env, logger) ⇒ RackMountable

Returns a new instance of RackMountable.



8
9
10
11
# File 'lib/gluey/base/rack_mountable.rb', line 8

def initialize(env, logger)
  @environment = env
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gluey/base/rack_mountable.rb', line 13

def call(env)
  material = nil
  path = nil
  start_time = Time.now.to_f
  time_elapsed = lambda { ((Time.now.to_f - start_time) * 1000).to_i }

  unless ALLOWED_HEADERS.include? env['REQUEST_METHOD']
    return method_not_allowed_response
  end

  # Extract the path from everything after the leading slash
  _, material, path, mark = env['PATH_INFO'].to_s.match(PATH_PARSER).to_a
  unless path
    return bat_path_response
  end

  file = @environment[material, path, mark]
  unless file
    @logger.info "Not found glued asset #{path} (material=#{material}) - 404 (#{time_elapsed.call}ms)"
    return not_found_response
  end

  @logger.info "Served glued asset #{path} (material=#{material}) - 200 (#{time_elapsed.call}ms)"
  ok_response file, (mark && @environment.mark_versions)

rescue => e
  @logger.error "Error gluying asset #{path}  (material=#{material}):"
  @logger.error "#{e.class.name}: #{e.message}"
  raise
end

#inspectObject



44
45
46
# File 'lib/gluey/base/rack_mountable.rb', line 44

def inspect
  '#<Gluey::RackMountable>'
end