Module: Rack::HttpRouter::Action
- Included in:
- Rack::HttpRouter
- Defined in:
- lib/rack-http_router/action.rb
Class Method Summary collapse
-
.erb(path, config, route, db, view_params = {}) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- .html(content, status: 200) ⇒ Object
- .html_response(content, status: 200) ⇒ Object
- .included(base) ⇒ Object
- .json(content = {}, status: 200) ⇒ Object
- .json_response(content = {}, status: 200) ⇒ Object
-
.redirect_response(url) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
- .redirect_to(url) ⇒ Object
- .response(body = nil, status = 200, headers = {}) ⇒ Object
- .text(content, status: 200) ⇒ Object
- .text_response(content, status: 200) ⇒ Object
- .view(paths, view_params = {}, status: 200, config: {}, route: nil, db: nil, response_instance: false) ⇒ Object
- .view_response(paths, view_params = {}, status: 200, config: {}, route: nil, db: nil) ⇒ Object
Instance Method Summary collapse
- #erb(path, view_params = {}) ⇒ Object
- #html(content, status: 200) ⇒ Object
- #html_response(content, status: 200) ⇒ Object
- #json(content = {}, status: 200) ⇒ Object
- #json_response(content = {}, status: 200) ⇒ Object
- #redirect_response(url) ⇒ Object
- #redirect_to(url) ⇒ Object
- #response(body = nil, status = 200, headers = {}) ⇒ Object
- #text(content, status: 200) ⇒ Object
- #text_response(content, status: 200) ⇒ Object
Class Method Details
.erb(path, config, route, db, view_params = {}) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
166 167 168 169 170 |
# File 'lib/rack-http_router/action.rb', line 166 def erb(path, config, route, db, view_params = {}) @view = OpenStruct.new(view_params) eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src) end |
.html(content, status: 200) ⇒ Object
88 89 90 |
# File 'lib/rack-http_router/action.rb', line 88 def html(content, status: 200) [status, { 'Content-Type' => 'text/html' }, [content]] end |
.html_response(content, status: 200) ⇒ Object
92 93 94 |
# File 'lib/rack-http_router/action.rb', line 92 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
141 142 143 |
# File 'lib/rack-http_router/action.rb', line 141 def json(content = {}, status: 200) [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]] end |
.json_response(content = {}, status: 200) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/rack-http_router/action.rb', line 145 def json_response(content = {}, status: 200) Rack::Response.new( Oj.dump(content, mode: :compat), status, { 'Content-Type' => 'application/json' } ) end |
.redirect_response(url) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
173 174 175 176 177 178 179 |
# File 'lib/rack-http_router/action.rb', line 173 def redirect_response(url) Rack::Response.new( nil, 302, { 'Location' => url } ) end |
.redirect_to(url) ⇒ Object
181 182 183 |
# File 'lib/rack-http_router/action.rb', line 181 def redirect_to(url) [302, { 'Location' => url }, []] end |
.response(body = nil, status = 200, headers = {}) ⇒ Object
185 186 187 |
# File 'lib/rack-http_router/action.rb', line 185 def response(body = nil, status = 200, headers = {}) Rack::Response.new(body, status, headers) end |
.text(content, status: 200) ⇒ Object
153 154 155 |
# File 'lib/rack-http_router/action.rb', line 153 def text(content, status: 200) [status, { 'Content-Type' => 'text/plain' }, [content]] end |
.text_response(content, status: 200) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/rack-http_router/action.rb', line 157 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
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 |
# File 'lib/rack-http_router/action.rb', line 115 def view( paths, view_params = {}, status: 200, config: {}, route: nil, db: nil, response_instance: false ) erb = if paths.is_a?(Array) paths.map { |path| erb("#{config.dig(:views, :path) || "views"}/#{path}", config, route, db, view_params) }.join else erb("#{config.dig(:views, :path) || "views"}/#{paths}", config, route, db, 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, config: {}, route: nil, db: nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rack-http_router/action.rb', line 96 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
71 72 73 |
# File 'lib/rack-http_router/action.rb', line 71 def erb(path, view_params = {}) Rack::HttpRouter::Action.erb(path, view_params) end |
#html(content, status: 200) ⇒ Object
47 48 49 |
# File 'lib/rack-http_router/action.rb', line 47 def html(content, status: 200) Rack::HttpRouter::Action.html(content, status: status) end |
#html_response(content, status: 200) ⇒ Object
51 52 53 |
# File 'lib/rack-http_router/action.rb', line 51 def html_response(content, status: 200) Rack::HttpRouter::Action.html_response(content, status: status) end |
#json(content = {}, status: 200) ⇒ Object
55 56 57 |
# File 'lib/rack-http_router/action.rb', line 55 def json(content = {}, status: 200) Rack::HttpRouter::Action.json(content, status: status) end |
#json_response(content = {}, status: 200) ⇒ Object
59 60 61 |
# File 'lib/rack-http_router/action.rb', line 59 def json_response(content = {}, status: 200) Rack::HttpRouter::Action.json_response(content, status: status) end |
#redirect_response(url) ⇒ Object
75 76 77 |
# File 'lib/rack-http_router/action.rb', line 75 def redirect_response(url) Rack::HttpRouter::Action.redirect_response(url) end |
#redirect_to(url) ⇒ Object
79 80 81 |
# File 'lib/rack-http_router/action.rb', line 79 def redirect_to(url) Rack::HttpRouter::Action.redirect_to(url) end |
#response(body = nil, status = 200, headers = {}) ⇒ Object
83 84 85 |
# File 'lib/rack-http_router/action.rb', line 83 def response(body = nil, status = 200, headers = {}) Rack::Response.new(body, status, headers) end |
#text(content, status: 200) ⇒ Object
63 64 65 |
# File 'lib/rack-http_router/action.rb', line 63 def text(content, status: 200) Rack::HttpRouter::Action.text(content, status: status) end |
#text_response(content, status: 200) ⇒ Object
67 68 69 |
# File 'lib/rack-http_router/action.rb', line 67 def text_response(content, status: 200) Rack::HttpRouter::Action.text_response(content, status: status) end |