Module: Rack::HttpRouter::Action

Included in:
Rack::HttpRouter
Defined in:
lib/rack-http_router/action.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.erb(content, config, route, db, view_params = {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



198
199
200
201
202
# File 'lib/rack-http_router/action.rb', line 198

def erb(content, config, route, db, view_params = {})
  @view = OpenStruct.new(view_params)

  eval(Erubi::Engine.new(content).src)
end

.html(content, status: 200) ⇒ Object



165
166
167
# File 'lib/rack-http_router/action.rb', line 165

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

.html_response(content, status: 200) ⇒ Object



169
170
171
# File 'lib/rack-http_router/action.rb', line 169

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
37
38
39
40
41
42
43
44
45
# File 'lib/rack-http_router/action.rb', line 10

def self.included(base)
  base.class_eval do
    attr_reader :route, :config, :db if self != Rack::HttpRouter

    def initialize(route: nil, config: nil)
      @route = route
      @config = config
      @db = config[:db]
    end

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

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

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



173
174
175
# File 'lib/rack-http_router/action.rb', line 173

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

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



177
178
179
180
181
182
183
# File 'lib/rack-http_router/action.rb', line 177

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

.layout(layout_path, file_path) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/rack-http_router/action.rb', line 157

def layout(layout_path, file_path)
  [
    "layout/#{layout_path}/_header",
    file_path,
    "layout/#{layout_path}/_footer"
  ]
end

.redirect_response(url) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



205
206
207
208
209
210
211
# File 'lib/rack-http_router/action.rb', line 205

def redirect_response(url)
  Rack::Response.new(
    nil,
    302,
    { 'Location' => url }
  )
end

.redirect_to(url) ⇒ Object



213
214
215
# File 'lib/rack-http_router/action.rb', line 213

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

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



217
218
219
# File 'lib/rack-http_router/action.rb', line 217

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

.text(content, status: 200) ⇒ Object



185
186
187
# File 'lib/rack-http_router/action.rb', line 185

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

.text_response(content, status: 200) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/rack-http_router/action.rb', line 189

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

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rack-http_router/action.rb', line 111

def view(
  paths,
  view_params = {},
  status: 200,
  config: {},
  route: nil,
  db: nil,
  response_instance: false
)
  base_path = config.dig(:views, :path) || "views"

  file_or_nil = lambda do |path|
    ::File.read(path)
  rescue Errno::ENOENT
    nil
  end

  file_content = if paths.is_a?(Array)
                   paths.map { |path| ::File.read("#{base_path}/#{path}.html.erb") }.join
                 else
                   ::File.read("#{base_path}/#{paths}.html.erb")
                 end

  erb = erb(
    [
      file_or_nil.call("#{base_path}/layout/_header.html.erb"),
      file_content,
      file_or_nil.call("#{base_path}/layout/_footer.html.erb")
    ].join,
    config,
    route,
    db,
    view_params
  )

  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, config: {}, route: nil, db: nil) ⇒ Object



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

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

Instance Method Details

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



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

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

#html(content, status: 200) ⇒ Object



51
52
53
# File 'lib/rack-http_router/action.rb', line 51

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

#html_response(content, status: 200) ⇒ Object



55
56
57
# File 'lib/rack-http_router/action.rb', line 55

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

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



59
60
61
# File 'lib/rack-http_router/action.rb', line 59

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

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



63
64
65
# File 'lib/rack-http_router/action.rb', line 63

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

#layout(layout_path, file_path) ⇒ Object



47
48
49
# File 'lib/rack-http_router/action.rb', line 47

def layout(layout_path, file_path)
  Rack::HttpRouter::Action.layout(layout_path, file_path)
end

#redirect_response(url) ⇒ Object



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

def redirect_response(url)
  Rack::HttpRouter::Action.redirect_response(url)
end

#redirect_to(url) ⇒ Object



83
84
85
# File 'lib/rack-http_router/action.rb', line 83

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

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



87
88
89
# File 'lib/rack-http_router/action.rb', line 87

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

#text(content, status: 200) ⇒ Object



67
68
69
# File 'lib/rack-http_router/action.rb', line 67

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

#text_response(content, status: 200) ⇒ Object



71
72
73
# File 'lib/rack-http_router/action.rb', line 71

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