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

#cacheObject



41
42
43
# File 'lib/rack/sprockets/request.rb', line 41

def cache
  File.join(options(:root), options(:public), options(:hosted_at))
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  File.exists?(File.join(cache, "#{path_resource_name}#{path_resource_format}"))
end

#for_js?Boolean

Returns:

  • (Boolean)


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

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 in :hosted_at (fast)

> don’t process if a file already exists in :hosted_at

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

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/rack/sprockets/request.rb', line 81

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

#hosted_at?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/rack/sprockets/request.rb', line 68

def hosted_at?
  File.basename(File.dirname(path_info)) == File.basename(options(:hosted_at))
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



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

def source
  @source ||= begin
    source_opts = {
      :folder    => File.join(options(:root), options(:source)),
      :cache     => Rack::Sprockets.config.cache? ? cache : nil,
      :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