Module: Rack::Way::Action

Included in:
Rack::Way
Defined in:
lib/rack-way/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.erb(path, _route, view_params = {}) ⇒ Object



140
141
142
143
144
# File 'lib/rack-way/action.rb', line 140

def erb(path, _route, view_params = {})
  @view = OpenStruct.new(view_params)

  eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
end

.html(content, status: 200) ⇒ Object



75
76
77
# File 'lib/rack-way/action.rb', line 75

def html(content, status: 200)
  [status, { 'Content-Type' => 'text/html' }, [content]]
end

.html_response(content, status: 200) ⇒ Object



79
80
81
# File 'lib/rack-way/action.rb', line 79

def html_response(content, status: 200)
  Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
end

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack-way/action.rb', line 10

def self.included(base)
  base.class_eval do
    attr_reader :route if self != Rack::Way

    def initialize(route)
      @route = route
    end

    def view_response(a_path, a_view_params = {}, status: 200)
      Rack::Way::Action.view_response(
        a_path,
        a_view_params,
        status: status, route: route
      )
    end

    def view(
      a_path, a_view_params = {}, status: 200, response_instance: false
    )
      Rack::Way::Action.view(
        a_path,
        a_view_params,
        status: status, response_instance: response_instance, route: route
      )
    end
  end
end

.json(content = {}, status: 200) ⇒ Object



116
117
118
# File 'lib/rack-way/action.rb', line 116

def json(content = {}, status: 200)
  [status, { 'Content-Type' => 'application/json' }, [content.to_json]]
end

.json_response(content = {}, status: 200) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/rack-way/action.rb', line 120

def json_response(content = {}, status: 200)
  Rack::Response.new(
    content.to_json,
    status,
    { 'Content-Type' => 'application/json' }
  )
end

.redirect_to(url) ⇒ Object



146
147
148
# File 'lib/rack-way/action.rb', line 146

def redirect_to(url)
  [302, { 'Location' => url }, []]
end

.response(body = nil, status = 200, headers = {}) ⇒ Object



150
151
152
# File 'lib/rack-way/action.rb', line 150

def response(body = nil, status = 200, headers = {})
  Rack::Response.new(body, status, headers)
end

.text(content, status: 200) ⇒ Object



128
129
130
# File 'lib/rack-way/action.rb', line 128

def text(content, status: 200)
  [status, { 'Content-Type' => 'text/plain' }, [content]]
end

.text_response(content, status: 200) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/rack-way/action.rb', line 132

def text_response(content, status: 200)
  Rack::Response.new(
    content,
    status,
    { 'Content-Type' => 'text/plain' }
  )
end

.view(paths, view_params = {}, status: 200, response_instance: false, route: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rack-way/action.rb', line 92

def view(
  paths,
  view_params = {},
  status: 200,
  response_instance: false,
  route: nil
)
  erb = if paths.is_a?(Array)
          paths.map { |path| erb("views/#{path}", route, view_params) }.join
        else
          erb("views/#{paths}", route, view_params)
        end

  if response_instance
    return Rack::Response.new(
      erb,
      status,
      { 'Content-Type' => 'text/html' }
    )
  end

  [status, { 'Content-Type' => 'text/html' }, [erb]]
end

.view_response(paths, view_params = {}, status: 200, route: nil) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/rack-way/action.rb', line 83

def view_response(paths, view_params = {}, status: 200, route: nil)
  view(
    paths,
    view_params,
    status: status, response_instance: true,
    route: route
  )
end

Instance Method Details

#erb(path, view_params = {}) ⇒ Object



62
63
64
# File 'lib/rack-way/action.rb', line 62

def erb(path, view_params = {})
  Rack::Way::Action.erb(path, view_params)
end

#html(content, status: 200) ⇒ Object



38
39
40
# File 'lib/rack-way/action.rb', line 38

def html(content, status: 200)
  Rack::Way::Action.html(content, status: status)
end

#html_response(content, status: 200) ⇒ Object



42
43
44
# File 'lib/rack-way/action.rb', line 42

def html_response(content, status: 200)
  Rack::Way::Action.html_response(content, status: status)
end

#json(content = {}, status: 200) ⇒ Object



46
47
48
# File 'lib/rack-way/action.rb', line 46

def json(content = {}, status: 200)
  Rack::Way::Action.json(content, status: status)
end

#json_response(content = {}, status: 200) ⇒ Object



50
51
52
# File 'lib/rack-way/action.rb', line 50

def json_response(content = {}, status: 200)
  Rack::Way::Action.json_response(content, status: status)
end

#redirect_to(url) ⇒ Object



66
67
68
# File 'lib/rack-way/action.rb', line 66

def redirect_to(url)
  Rack::Way::Action.redirect_to(url)
end

#response(body = nil, status = 200, headers = {}) ⇒ Object



70
71
72
# File 'lib/rack-way/action.rb', line 70

def response(body = nil, status = 200, headers = {})
  Rack::Response.new(body, status, headers)
end

#text(content, status: 200) ⇒ Object



54
55
56
# File 'lib/rack-way/action.rb', line 54

def text(content, status: 200)
  Rack::Way::Action.text(content, status: status)
end

#text_response(content, status: 200) ⇒ Object



58
59
60
# File 'lib/rack-way/action.rb', line 58

def text_response(content, status: 200)
  Rack::Way::Action.text_response(content, status: status)
end