class Mailpeek::WebAction

Public: WebAction

Attributes

block[RW]
env[RW]
type[RW]

Public Class Methods

new(env, block) click to toggle source
# File lib/mailpeek/web/action.rb, line 87
def initialize(env, block)
  @_erb = false
  @env = env
  @block = block

  # @@files ||= {}
end

Public Instance Methods

erb(content, options = {}) click to toggle source
# File lib/mailpeek/web/action.rb, line 43
def erb(content, options = {})
  if content.is_a? Symbol
    unless respond_to?(:"_erb_#{content}")
      src = ERB.new(File.read("#{Web.settings.views}/#{content}.erb")).src
      WebAction.class_eval("def _erb_#{content}\n#{src}\n end")
    end
  end

  if @_erb
    _erb(content, options[:locals])
  else
    @_erb = true
    content = _erb(content, options[:locals])

    _render { content }
  end
end
halt(res) click to toggle source
# File lib/mailpeek/web/action.rb, line 16
def halt(res)
  throw :halt, res
end
json(payload) click to toggle source
# File lib/mailpeek/web/action.rb, line 67
def json(payload)
  [
    200,
    { 'Content-Type' => 'application/json', 'Cache-Control' => 'no-cache' },
    [JSON.generate(payload)]
  ]
end
params() click to toggle source
# File lib/mailpeek/web/action.rb, line 24
def params
  return @params if @params

  @params =
    route_params
    .merge(request.params)
    .each_with_object({}) do |(key, value), results|
      results[key.to_s] = value
      results[key.to_sym] = value
      results
    end

  @params
end
redirect(location) click to toggle source
# File lib/mailpeek/web/action.rb, line 20
def redirect(location)
  halt([302, { 'Location' => "#{request.base_url}#{location}" }, []])
end
render(engine, content, options = {}) click to toggle source
# File lib/mailpeek/web/action.rb, line 61
def render(engine, content, options = {})
  raise 'Only erb templates are supported' if engine != :erb

  erb(content, options)
end
request() click to toggle source
# File lib/mailpeek/web/action.rb, line 12
def request
  @request ||= ::Rack::Request.new(env)
end
route_params() click to toggle source
# File lib/mailpeek/web/action.rb, line 39
def route_params
  env[WebRouter::ROUTE_PARAMS]
end
send_file(path, filename) click to toggle source
# File lib/mailpeek/web/action.rb, line 75
def send_file(path, filename)
  [
    200,
    {
      'Cache-Control' => 'no-cache',
      'Content-Type' => 'application/octet-stream',
      'Content-Disposition' => "attachment; filename=\"#{filename}\""
    },
    [File.read(path)]
  ]
end
settings() click to toggle source
# File lib/mailpeek/web/action.rb, line 8
def settings
  Web.settings
end

Private Instance Methods

_erb(file, locals) click to toggle source
# File lib/mailpeek/web/action.rb, line 97
def _erb(file, locals)
  locals&.each do |k, v|
    define_singleton_method(k) { v } unless singleton_methods.include?(k)
  end

  if file.is_a?(String)
    ERB.new(file).result(binding)
  else
    send(:"_erb_#{file}")
  end
end