Class: Rack::Sprockets::Request

Inherits:
Request
  • Object
show all
Includes:
Options
Defined in:
lib/rack/sprockets/request.rb

Overview

Provides access to the HTTP request. Request objects respond to everything defined by Rack::Request as well as some additional convenience methods defined here

Constant Summary collapse

JS_PATH_FORMATS =
['.js']

Instance Method Summary collapse

Instance Method Details

#for_js?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/rack/sprockets/request.rb', line 63

def for_js?
  (http_accept && http_accept.include?(Rack::Sprockets::MIME_TYPE)) ||
  (media_type  && media_type.include?(Rack::Sprockets::MIME_TYPE )) ||
  JS_PATH_FORMATS.include?(path_resource_format)
end

#for_sprockets?Boolean

Determine if the request is for an existing Sprockets source file This will be called on every request so speed is an issue

> first check if the request is a GET on a js resource (fast)

> then check for sprockets source files that match the request (slow)

Returns:

  • (Boolean)


73
74
75
# File 'lib/rack/sprockets/request.rb', line 73

def for_sprockets?
  get? && for_js? && !source.files.empty?
end

#http_acceptObject



29
30
31
# File 'lib/rack/sprockets/request.rb', line 29

def http_accept
  @env['HTTP_ACCEPT']
end

#path_infoObject



25
26
27
# File 'lib/rack/sprockets/request.rb', line 25

def path_info
  @env['PATH_INFO']
end

#path_resource_formatObject



37
38
39
# File 'lib/rack/sprockets/request.rb', line 37

def path_resource_format
  File.extname(path_info)
end

#path_resource_nameObject



33
34
35
# File 'lib/rack/sprockets/request.rb', line 33

def path_resource_name
  File.basename(path_info, path_resource_format)
end

#request_methodObject

The HTTP request method. This is the standard implementation of this method but is respecified here due to libraries that attempt to modify the behavior to respect POST tunnel method specifiers. We always want the real request method.



21
22
23
# File 'lib/rack/sprockets/request.rb', line 21

def request_method
  @env['REQUEST_METHOD']
end

#sourceObject

The Rack::Sprockets::Source that the request is for



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/sprockets/request.rb', line 42

def source
  @source ||= begin
    cache = if Rack::Sprockets.config.cache?
      File.join(options(:root), options(:public), options(:hosted_at))
    else
      nil
    end
    source_opts = {
      :folder    => File.join(options(:root), options(:source)),
      :cache     => cache,
      :compress  => Rack::Sprockets.config.compress,
      :secretary => {
        :root         => options(:root),
        :load_path    => options(:load_path),
        :expand_paths => options(:expand_paths)
      }
    }
    Source.new(path_resource_name, source_opts)
  end
end