class Gluey::RackMountable

Constants

ALLOWED_HEADERS
PATH_PARSER

Public Class Methods

new(env, logger) click to toggle source
# File lib/gluey/base/rack_mountable.rb, line 8
def initialize(env, logger)
  @environment = env
  @logger = logger
end

Public Instance Methods

call(env) click to toggle source
# 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
inspect() click to toggle source
# File lib/gluey/base/rack_mountable.rb, line 44
def inspect
  '#<Gluey::RackMountable>'
end

Private Instance Methods

bat_path_response() click to toggle source
# File lib/gluey/base/rack_mountable.rb, line 67
def bat_path_response
  [400, {'Content-Type' => "text/plain", 'Content-Length' => "8"}, ['Bad Path']]
end
method_not_allowed_response() click to toggle source
# File lib/gluey/base/rack_mountable.rb, line 63
def method_not_allowed_response
  [405, {'Content-Type' => "text/plain", 'Content-Length' => "18"}, ['Method Not Allowed']]
end
not_found_response() click to toggle source

Returns a 404 Not Found response tuple

# File lib/gluey/base/rack_mountable.rb, line 72
def not_found_response
  [404, {'Content-Type' => "text/plain", 'Content-Length' => "9"}, ['Not found']]
end
ok_response(file, to_cache) click to toggle source

Returns a 200 OK response tuple

# File lib/gluey/base/rack_mountable.rb, line 51
def ok_response(file, to_cache)
  headers = {
      'Content-Length' => File.stat(file).size,
      'Content-Type' => Rack::Mime.mime_type(File.extname(file), 'text/plain')
  }
  if to_cache
    headers['Cache-Control'] = "public, max-age=31536000"
  end

  [200, headers, FileBody.new(file)]
end