Module: Locomotive::Steam::Middlewares::Concerns::Helpers

Constant Summary collapse

HTML_CONTENT_TYPE =
'text/html'.freeze
HTML_MIME_TYPES =
[nil, 'text/html', 'application/x-www-form-urlencoded', 'multipart/form-data'].freeze
CACHE_HEADERS =
{
  'steam.cache_control'       => 'Cache-Control',
  'steam.cache_vary'          => 'Vary',
  'steam.cache_etag'          => 'ETag',
  'steam.cache_last_modified' => 'Last-Modified'
}.freeze

Instance Method Summary collapse

Instance Method Details

#html?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 17

def html?
  HTML_MIME_TYPES.include?(self.request.media_type) &&
  !self.request.xhr? &&
  !self.json?
end

#inject_cookies(headers) ⇒ Object



51
52
53
54
55
56
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 51

def inject_cookies(headers)
  _cookies = env['steam.cookies'] || {}
  _cookies.each do |key, vals|
    Rack::Utils.set_cookie_header!(headers, key, vals.symbolize_keys!)
  end
end

#json?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 23

def json?
  self.request.content_type == 'application/json' || File.extname(self.request.path) == '.json'
end

#log(msg, offset = 2) ⇒ Object



82
83
84
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 82

def log(msg, offset = 2)
  Locomotive::Common::Logger.info (' ' * offset) + msg
end

#make_local_path(location) ⇒ Object

make sure the location passed in parameter doesn’t include the “mounted_on” parameter. If so, returns the location without the “mounted_on” string.



73
74
75
76
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 73

def make_local_path(location)
  return location if mounted_on.blank?
  location.gsub(Regexp.new('^' + mounted_on), '')
end

#modify_path(path = nil) {|segments| ... } ⇒ Object

Yields:

  • (segments)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 58

def modify_path(path = nil, &block)
  path ||= env['steam.path']

  segments = path.split('/')
  yield(segments) if block_given?
  path = segments.join('/')

  path = '/' if path.blank?
  path += "?#{request.query_string}" unless request.query_string.empty?
  path
end

#mounted_onObject



78
79
80
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 78

def mounted_on
  request.env['steam.mounted_on']
end

#redirect_to(location, type = 301) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 40

def redirect_to(location, type = 301)
  _location = mounted_on && !location.starts_with?(mounted_on) && (location =~ Locomotive::Steam::IsHTTP).nil? ? "#{mounted_on}#{location}" : location

  self.log "Redirected to #{_location}".blue

  headers = { 'Content-Type' => HTML_CONTENT_TYPE, 'Location' => _location }
  inject_cookies(headers)

  @next_response = [type, headers, []]
end

#render_response(content, code = 200, type = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/locomotive/steam/middlewares/concerns/helpers.rb', line 27

def render_response(content, code = 200, type = nil)
  base_headers = { 'Content-Type' => type || HTML_CONTENT_TYPE }

  CACHE_HEADERS.each do |key, http_name|
    base_headers[http_name] = env[key] if env[key]
  end

  _headers = env['steam.headers'] || {}
  inject_cookies(_headers)

  @next_response = [code, base_headers.merge(_headers), [content]]
end