Class: Rack::Less::Request

Inherits:
Request
  • Object
show all
Includes:
Options
Defined in:
lib/rack/less/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

> from: github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/request.rb

Constant Summary collapse

CSS_PATH_FORMATS =
['.css']

Constants included from Options

Options::RACK_ENV_NS

Instance Method Summary collapse

Methods included from Options

included

Instance Method Details

#cacheObject



60
61
62
# File 'lib/rack/less/request.rb', line 60

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

#cached?Boolean

Returns:

  • (Boolean)


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

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

#for_css?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/rack/less/request.rb', line 76

def for_css?
  (http_accept && http_accept.include?(Rack::Less::MIME_TYPE)) ||
  (media_type  && media_type.include?(Rack::Less::MIME_TYPE )) ||
  CSS_PATH_FORMATS.include?(path_info_format)
end

#for_less?Boolean

Determine if the request is for a non-cached existing LESS CSS source file This will be called on every request so speed is an issue

> first check if the request is a GET on a css resource in :hosted_at (fast)

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

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/rack/less/request.rb', line 94

def for_less?
  get? &&               # GET on css resource in :hosted_at (fast, check first)
  for_css? &&
  hosted_at? &&
  !source.files.empty?  # there is source for the resource (slow, check last)
end

#hosted_at?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rack/less/request.rb', line 82

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

#hosted_at_optionObject



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

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



26
27
28
# File 'lib/rack/less/request.rb', line 26

def http_accept
  @env['HTTP_ACCEPT']
end

#path_infoObject



30
31
32
# File 'lib/rack/less/request.rb', line 30

def path_info
  @env['PATH_INFO']
end

#path_info_formatObject



56
57
58
# File 'lib/rack/less/request.rb', line 56

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

#path_info_resourceObject



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

def path_info_resource
  # sanitized path to the resource being requested
  #  ensure single leading '/'
  #  remove any resource format
  #  ex:
  #  '/something.css' => '/something'
  #  '/nested/something.css' => '/nested/something'
  #  '///something.css' => '/something'
  #  '/nested///something.css' => '/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.



22
23
24
# File 'lib/rack/less/request.rb', line 22

def request_method
  @env['REQUEST_METHOD']
end

#sourceObject

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/rack/less/request.rb', line 65

def source
  @source ||= begin
    source_opts = {
      :folder   => File.join(options(:root), options(:source)),
      :cache    => Rack::Less.config.cache? ? cache : nil,
      :compress => Rack::Less.config.compress?
    }
    Source.new(path_info_resource, source_opts)
  end
end