Class: Mailpeek::WebAction

Inherits:
Object
  • Object
show all
Defined in:
lib/mailpeek/web/action.rb

Overview

Public: WebAction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, block) ⇒ WebAction

Returns a new instance of WebAction.



87
88
89
90
91
92
93
# File 'lib/mailpeek/web/action.rb', line 87

def initialize(env, block)
  @_erb = false
  @env = env
  @block = block

  # @@files ||= {}
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



6
7
8
# File 'lib/mailpeek/web/action.rb', line 6

def block
  @block
end

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/mailpeek/web/action.rb', line 6

def env
  @env
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/mailpeek/web/action.rb', line 6

def type
  @type
end

Instance Method Details

#erb(content, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



16
17
18
# File 'lib/mailpeek/web/action.rb', line 16

def halt(res)
  throw :halt, res
end

#json(payload) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/mailpeek/web/action.rb', line 67

def json(payload)
  [
    200,
    { 'Content-Type' => 'application/json', 'Cache-Control' => 'no-cache' },
    [JSON.generate(payload)]
  ]
end

#paramsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object



20
21
22
# File 'lib/mailpeek/web/action.rb', line 20

def redirect(location)
  halt([302, { 'Location' => "#{request.base_url}#{location}" }, []])
end

#render(engine, content, options = {}) ⇒ Object



61
62
63
64
65
# 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

#requestObject



12
13
14
# File 'lib/mailpeek/web/action.rb', line 12

def request
  @request ||= ::Rack::Request.new(env)
end

#route_paramsObject



39
40
41
# File 'lib/mailpeek/web/action.rb', line 39

def route_params
  env[WebRouter::ROUTE_PARAMS]
end

#send_file(path, filename) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# 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

#settingsObject



8
9
10
# File 'lib/mailpeek/web/action.rb', line 8

def settings
  Web.settings
end