Module: Rack::HttpRouter::Action
- Included in:
- Rack::HttpRouter
- Defined in:
- lib/rack-http_router/action.rb
Class Method Summary collapse
- .assign(obj, hash) ⇒ Object
-
.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
- #assign(obj, hash) ⇒ Object
- #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
.assign(obj, hash) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/rack-http_router/action.rb', line 92 def assign(obj, hash) hash.each do |k, v| obj.define_singleton_method(k) { v } end obj end |
.erb(path, config, route, db, view_params = {}) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
178 179 180 181 182 |
# File 'lib/rack-http_router/action.rb', line 178 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
100 101 102 |
# File 'lib/rack-http_router/action.rb', line 100 def html(content, status: 200) [status, { 'Content-Type' => 'text/html' }, [content]] end |
.html_response(content, status: 200) ⇒ Object
104 105 106 |
# File 'lib/rack-http_router/action.rb', line 104 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
153 154 155 |
# File 'lib/rack-http_router/action.rb', line 153 def json(content = {}, status: 200) [status, { 'Content-Type' => 'application/json' }, [Oj.dump(content, mode: :compat)]] end |
.json_response(content = {}, status: 200) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/rack-http_router/action.rb', line 157 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
185 186 187 188 189 190 191 |
# File 'lib/rack-http_router/action.rb', line 185 def redirect_response(url) Rack::Response.new( nil, 302, { 'Location' => url } ) end |
.redirect_to(url) ⇒ Object
193 194 195 |
# File 'lib/rack-http_router/action.rb', line 193 def redirect_to(url) [302, { 'Location' => url }, []] end |
.response(body = nil, status = 200, headers = {}) ⇒ Object
197 198 199 |
# File 'lib/rack-http_router/action.rb', line 197 def response(body = nil, status = 200, headers = {}) Rack::Response.new(body, status, headers) end |
.text(content, status: 200) ⇒ Object
165 166 167 |
# File 'lib/rack-http_router/action.rb', line 165 def text(content, status: 200) [status, { 'Content-Type' => 'text/plain' }, [content]] end |
.text_response(content, status: 200) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/rack-http_router/action.rb', line 169 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
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 |
# File 'lib/rack-http_router/action.rb', line 127 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rack-http_router/action.rb', line 108 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
#assign(obj, hash) ⇒ Object
47 48 49 |
# File 'lib/rack-http_router/action.rb', line 47 def assign(obj, hash) Rack::HttpRouter::Action.assign(obj, hash) end |
#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 |
#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 |