Class: DeprecationCollector::Web::Router::ActionContext
- Inherits:
-
Object
- Object
- DeprecationCollector::Web::Router::ActionContext
- Defined in:
- lib/deprecation_collector/web/router.rb
Overview
:nodoc:
Constant Summary collapse
- VIEW_PATH =
"#{__dir__}/views"
Instance Attribute Summary collapse
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #call(_env, application) ⇒ Object
- #env ⇒ Object
- #halt(res, content = nil) ⇒ Object
-
#initialize(request, &block) ⇒ ActionContext
constructor
A new instance of ActionContext.
- #params ⇒ Object
- #redirect_to(location) ⇒ Object
-
#render(plain: nil, html: nil, json: nil, erb: nil, slim: nil, status: 200, locals: nil, layout: "layout.html.slim") ⇒ Object
rubocop:disable Metrics.
- #render_template(template, &block) ⇒ Object
- #route_params ⇒ Object
Constructor Details
#initialize(request, &block) ⇒ ActionContext
Returns a new instance of ActionContext.
118 119 120 121 |
# File 'lib/deprecation_collector/web/router.rb', line 118 def initialize(request, &block) @request = request @block = block end |
Instance Attribute Details
#request ⇒ Object
Returns the value of attribute request.
116 117 118 |
# File 'lib/deprecation_collector/web/router.rb', line 116 def request @request end |
Instance Method Details
#call(_env, application) ⇒ Object
123 124 125 126 |
# File 'lib/deprecation_collector/web/router.rb', line 123 def call(_env, application) @web = application.web instance_exec(&@block) end |
#env ⇒ Object
128 129 130 |
# File 'lib/deprecation_collector/web/router.rb', line 128 def env request.env end |
#halt(res, content = nil) ⇒ Object
142 143 144 |
# File 'lib/deprecation_collector/web/router.rb', line 142 def halt(res, content = nil) throw :halt, [res, { "content-type" => "text/plain" }, [content || res.to_s]] end |
#params ⇒ Object
136 137 138 139 140 |
# File 'lib/deprecation_collector/web/router.rb', line 136 def params @params ||= Hash.new { |hash, key| hash[key.to_s] if key.is_a?(Symbol) } .merge!(request.params) .merge!(route_params.transform_keys(&:to_s)) end |
#redirect_to(location) ⇒ Object
146 147 148 |
# File 'lib/deprecation_collector/web/router.rb', line 146 def redirect_to(location) throw :halt, [302, { "location" => "#{request.base_url}#{location}" }, []] end |
#render(plain: nil, html: nil, json: nil, erb: nil, slim: nil, status: 200, locals: nil, layout: "layout.html.slim") ⇒ Object
rubocop:disable Metrics
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/deprecation_collector/web/router.rb', line 150 def render(plain: nil, html: nil, json: nil, erb: nil, slim: nil, # rubocop:disable Metrics status: 200, locals: nil, layout: "layout.html.slim") unless [plain, html, json, erb, slim].compact.size == 1 raise ArgumentError, "provide exactly one render format" end if json json = JSON.generate(json) unless json.is_a?(String) return [status, { "content-type" => "application/json" }, [json]] end return [status, { "content-type" => "text/plain" }, [plain.to_s]] if plain _define_locals(locals) if locals template = "#{erb}.erb" if erb template = "#{slim}.slim" if slim html = render_template(template) if template html = render_template(layout) { html } if layout color_scheme_headers = { "Accept-CH" => "Sec-CH-Prefers-Color-Scheme", "Vary" => "Sec-CH-Prefers-Color-Scheme", "Critical-CH" => "Sec-CH-Prefers-Color-Scheme" } return [status, { "content-type" => "text/html" }.merge(color_scheme_headers), [html.to_s]] if html nil end |
#render_template(template, &block) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/deprecation_collector/web/router.rb', line 181 def render_template(template, &block) template_target = template.gsub(/\.slim\z/, ".template.rb") template_method_name = "_template_#{template_target.gsub(/[^\w]/, '_')}" template_filename = File.join(VIEW_PATH, template_target.to_s) _recompile_template(template, template_filename, template_method_name) if _recompile_enabled? _load_template(template_filename, template_method_name) unless respond_to?(template_method_name) send(template_method_name, &block) end |
#route_params ⇒ Object
132 133 134 |
# File 'lib/deprecation_collector/web/router.rb', line 132 def route_params env[Router::ROUTE_PARAMS] end |