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']

Constants included from Options

Options::COLLECTION_OPTS, Options::RACK_ENV_NS

Instance Method Summary collapse

Methods included from Options

included

Instance Method Details

#cacheObject



59
60
61
# File 'lib/rack/sprockets/request.rb', line 59

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

#cached?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rack/sprockets/request.rb', line 90

def cached?
  File.exists?(File.join(cache, "#{path_info_resource}#{path_info_format}"))
end

#for_js?Boolean

Returns:

  • (Boolean)


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

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_info_format)
end

#for_sprockets?Boolean

Determine if the request is for a non-cached 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 has already been cached

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

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/rack/sprockets/request.rb', line 99

def for_sprockets?
  get? &&               # GET on js resource in :hosted_at (fast, check first)
  for_js? &&
  hosted_at? &&
  !cached? &&           # resource not cached (little slower)
  !source.files.empty?  # there is source for the resource (slow, check last)
end

#hosted_at?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rack/sprockets/request.rb', line 86

def hosted_at?
  path_info =~ /^#{hosted_at_option}/
end

#hosted_at_optionObject



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

def hosted_at_option
  # sanitized :hosted_at option
  #  remove any trailing '/'
  #  ensure single leading '/'
  @hosted_at_option ||= options(:hosted_at).sub(/\/+$/, '').sub(/^\/*/, '/')
end

#http_acceptObject



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

def http_accept
  @env['HTTP_ACCEPT']
end

#path_infoObject



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

def path_info
  @env['PATH_INFO']
end

#path_info_formatObject



55
56
57
# File 'lib/rack/sprockets/request.rb', line 55

def path_info_format
  @path_info_format ||= File.extname(path_info.gsub(/\/+/, '/'))
end

#path_info_resourceObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/sprockets/request.rb', line 40

def path_info_resource
  # sanitized path to the resource being requested
  #  ensure single leading '/'
  #  remove any resource format
  #  ex:
  #  '/something.js' => '/something'
  #  '/nested/something.js' => '/nested/something'
  #  '///something.js' => '/something'
  #  '/nested///something.js' => '/nested/something'
  @path_info_resource ||= File.join(
    File.dirname(path_info.gsub(/\/+/, '/')).sub(/^#{hosted_at_option}/, ''),
    File.basename(path_info.gsub(/\/+/, '/'), path_info_format)
  ).sub(/^\/*/, '/')
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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/sprockets/request.rb', line 64

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_info_resource, source_opts)
  end
end