Module: Rackr::Action
- Included in:
- Rackr
- Defined in:
- lib/rackr/action.rb
Class Method Summary collapse
-
.erb(content, view_params = {}, config: nil, routes: nil, db: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- .html(content, status: 200, headers: {}) ⇒ Object
- .html_response(content, status: 200, headers: {}) ⇒ Object
- .included(base) ⇒ Object
- .json(content = {}, status: 200, headers: {}) ⇒ Object
- .json_response(content = {}, status: 200, headers: {}) ⇒ Object
- .layout(layout_path, file_path) ⇒ Object
-
.redirect_response(url, headers: {}) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
- .redirect_to(url, headers: {}) ⇒ Object
- .response(body = nil, status = 200, headers = {}) ⇒ Object
- .text(content, status: 200, headers: {}) ⇒ Object
- .text_response(content, status: 200, headers: {}) ⇒ Object
- .view(paths, view_params = {}, status: 200, headers: {}, config: {}, routes: nil, db: nil, response_instance: false) ⇒ Object
- .view_response(paths, view_params = {}, status: 200, headers: {}, config: {}, routes: nil, db: nil) ⇒ Object
Instance Method Summary collapse
- #erb(content, view_params = {}) ⇒ Object
- #html(content, status: 200, headers: {}) ⇒ Object
- #html_response(content, status: 200) ⇒ Object
- #json(content = {}, status: 200, headers: {}) ⇒ Object
- #json_response(content = {}, status: 200, headers: {}) ⇒ Object
- #layout(layout_path, file_path) ⇒ Object
- #redirect_response(url, headers: {}) ⇒ Object
- #redirect_to(url, headers: {}) ⇒ Object
- #response(body = nil, status = 200, headers = {}) ⇒ Object
- #text(content, status: 200, headers: {}) ⇒ Object
- #text_response(content, status: 200, headers: {}) ⇒ Object
Class Method Details
.erb(content, view_params = {}, config: nil, routes: nil, db: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
202 203 204 205 206 |
# File 'lib/rackr/action.rb', line 202 def erb(content, view_params = {}, config: nil, routes: nil, db: nil) @view = OpenStruct.new(view_params) eval(Erubi::Engine.new(content).src) end |
.html(content, status: 200, headers: {}) ⇒ Object
169 170 171 |
# File 'lib/rackr/action.rb', line 169 def html(content, status: 200, headers: {}) [status, { 'Content-Type' => 'text/html' }.merge(headers), [content]] end |
.html_response(content, status: 200, headers: {}) ⇒ Object
173 174 175 |
# File 'lib/rackr/action.rb', line 173 def html_response(content, status: 200, headers: {}) Rack::Response.new(content, status, { 'Content-Type' => 'text/html' }.merge(headers)) end |
.included(base) ⇒ Object
9 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 46 |
# File 'lib/rackr/action.rb', line 9 def self.included(base) base.class_eval do attr_reader :routes, :config, :db if self != Rackr def initialize(routes: nil, config: nil) @routes = routes @config = config @db = config[:db] end def view_response(a_path, a_view_params = {}, status: 200, headers: {}) Rackr::Action.view_response( a_path, a_view_params, status: status, headers: headers, config: config, routes: routes, db: db ) end def view( a_path, a_view_params = {}, status: 200, response_instance: false, headers: {} ) Rackr::Action.view( a_path, a_view_params, status: status, headers: headers, config: config, routes: routes, db: db, response_instance: response_instance ) end end end |
.json(content = {}, status: 200, headers: {}) ⇒ Object
177 178 179 |
# File 'lib/rackr/action.rb', line 177 def json(content = {}, status: 200, headers: {}) [status, { 'Content-Type' => 'application/json' }.merge(headers), [Oj.dump(content, mode: :compat)]] end |
.json_response(content = {}, status: 200, headers: {}) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/rackr/action.rb', line 181 def json_response(content = {}, status: 200, headers: {}) Rack::Response.new( Oj.dump(content, mode: :compat), status, { 'Content-Type' => 'application/json' }.merge(headers) ) end |
.layout(layout_path, file_path) ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/rackr/action.rb', line 161 def layout(layout_path, file_path) [ "layout/#{layout_path}/_header", file_path, "layout/#{layout_path}/_footer" ] end |
.redirect_response(url, headers: {}) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
209 210 211 212 213 214 215 |
# File 'lib/rackr/action.rb', line 209 def redirect_response(url, headers: {}) Rack::Response.new( nil, 302, { 'Location' => url }.merge(headers) ) end |
.redirect_to(url, headers: {}) ⇒ Object
217 218 219 |
# File 'lib/rackr/action.rb', line 217 def redirect_to(url, headers: {}) [302, { 'Location' => url }.merge(headers), []] end |
.response(body = nil, status = 200, headers = {}) ⇒ Object
221 222 223 |
# File 'lib/rackr/action.rb', line 221 def response(body = nil, status = 200, headers = {}) Rack::Response.new(body, status, headers) end |
.text(content, status: 200, headers: {}) ⇒ Object
189 190 191 |
# File 'lib/rackr/action.rb', line 189 def text(content, status: 200, headers: {}) [status, { 'Content-Type' => 'text/plain' }.merge(headers), [content]] end |
.text_response(content, status: 200, headers: {}) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/rackr/action.rb', line 193 def text_response(content, status: 200, headers: {}) Rack::Response.new( content, status, { 'Content-Type' => 'text/plain' }.merge(headers) ) end |
.view(paths, view_params = {}, status: 200, headers: {}, config: {}, routes: nil, db: nil, response_instance: false) ⇒ Object
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 156 157 158 159 |
# File 'lib/rackr/action.rb', line 114 def view( paths, view_params = {}, status: 200, headers: {}, config: {}, routes: 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, view_params, config: config, routes: routes, db: db ) if response_instance return Rack::Response.new( erb, status, { 'Content-Type' => 'text/html' }.merge(headers) ) end [status, { 'Content-Type' => 'text/html' }.merge(headers), [erb]] end |
.view_response(paths, view_params = {}, status: 200, headers: {}, config: {}, routes: nil, db: nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rackr/action.rb', line 93 def view_response( paths, view_params = {}, status: 200, headers: {}, config: {}, routes: nil, db: nil ) view( paths, view_params, status: status, config: config, routes: routes, headers: headers, db: db, response_instance: true ) end |
Instance Method Details
#erb(content, view_params = {}) ⇒ Object
76 77 78 |
# File 'lib/rackr/action.rb', line 76 def erb(content, view_params = {}) Rackr::Action.erb(content, view_params) end |
#html(content, status: 200, headers: {}) ⇒ Object
52 53 54 |
# File 'lib/rackr/action.rb', line 52 def html(content, status: 200, headers: {}) Rackr::Action.html(content, status: status, headers: headers) end |
#html_response(content, status: 200) ⇒ Object
56 57 58 |
# File 'lib/rackr/action.rb', line 56 def html_response(content, status: 200) Rackr::Action.html_response(content, status: status, headers: headers) end |
#json(content = {}, status: 200, headers: {}) ⇒ Object
60 61 62 |
# File 'lib/rackr/action.rb', line 60 def json(content = {}, status: 200, headers: {}) Rackr::Action.json(content, status: status, headers: headers) end |
#json_response(content = {}, status: 200, headers: {}) ⇒ Object
64 65 66 |
# File 'lib/rackr/action.rb', line 64 def json_response(content = {}, status: 200, headers: {}) Rackr::Action.json_response(content, status: status, headers: headers) end |
#layout(layout_path, file_path) ⇒ Object
48 49 50 |
# File 'lib/rackr/action.rb', line 48 def layout(layout_path, file_path) Rackr::Action.layout(layout_path, file_path) end |
#redirect_response(url, headers: {}) ⇒ Object
80 81 82 |
# File 'lib/rackr/action.rb', line 80 def redirect_response(url, headers: {}) Rackr::Action.redirect_response(url, headers: headers, headers: headers) end |
#redirect_to(url, headers: {}) ⇒ Object
84 85 86 |
# File 'lib/rackr/action.rb', line 84 def redirect_to(url, headers: {}) Rackr::Action.redirect_to(url, headers: headers, headers: headers) end |
#response(body = nil, status = 200, headers = {}) ⇒ Object
88 89 90 |
# File 'lib/rackr/action.rb', line 88 def response(body = nil, status = 200, headers = {}) Rack::Response.new(body, status, headers) end |
#text(content, status: 200, headers: {}) ⇒ Object
68 69 70 |
# File 'lib/rackr/action.rb', line 68 def text(content, status: 200, headers: {}) Rackr::Action.text(content, status: status, headers: headers) end |
#text_response(content, status: 200, headers: {}) ⇒ Object
72 73 74 |
# File 'lib/rackr/action.rb', line 72 def text_response(content, status: 200, headers: {}) Rackr::Action.text_response(content, status: status, headers: headers) end |