module Locomotive::Steam::Middlewares::Concerns::Helpers

Constants

CACHE_HEADERS
HTML_CONTENT_TYPE
HTML_MIME_TYPES

Public Instance Methods

html?() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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| ... } click to toggle source
# 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_on() click to toggle source
# File lib/locomotive/steam/middlewares/concerns/helpers.rb, line 78
def mounted_on
  request.env['steam.mounted_on']
end
redirect_to(location, type = 301) click to toggle source
# 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) click to toggle source
# 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