Module: Landline::DSL::ProbeMethods
- Included in:
- App, ProcessorContext, TemplateContext
- Defined in:
- lib/landline/dsl/methods_probe.rb,
lib/landline/extensions/session.rb
Overview
Common methods for Probe objects
Instance Method Summary collapse
-
#call(application) ⇒ Array(Integer, Hash{String => Object}, Object)
(in Landline::Path context) Pass the requested environment to a different application.
-
#cookie(*params, **options) ⇒ Object
(in Landline::Probe context) Set response cookie.
-
#defer(&callable) ⇒ Object
(in Landline::Probe context) Add a finalizer callable to the response.
-
#delete_cookie(key, value = nil) ⇒ Object
(in Landline::Probe context) Delete a cookie If no value is provided, deletes all cookies with the same key.
-
#delete_header(key, value = nil) ⇒ Object
(in Landline::Probe context) Delete a header value from the headers hash If no value is provided, deletes all key entries.
-
#escape_html(text) ⇒ Object
(in Landline::Probe context) Escape HTML entities.
-
#file(path, mode = "r", *all, &block) ⇒ Object
(in Landline::Probe context) Open a file relative to current filepath.
-
#form ⇒ Hash{String=>(String,Landline::Util::FormPart)}
(in Landline::Probe context) Returns formdata.
-
#form? ⇒ Boolean
(in Landline::Probe context) Checks if current request has multipart/form-data associated with it.
-
#header(key, value) ⇒ Object
(in Landline::Probe context) Set response header (generate response if one doesn’t exist yet).
-
#hijack ⇒ IO
(in Landline::Probe context) Fully hijack IO.
-
#json ⇒ Object
(in Landline::Probe context) Return parse JSON object.
-
#json? ⇒ Boolean
(in Landline::Probe context) Check if body is a JSON object.
-
#jump(path) ⇒ Object
(in Landline::Probe context) Do serverside request redirection.
-
#partial_hijack(&block) ⇒ Object
(in Landline::Probe context) Set a partial hijack callback.
-
#query ⇒ Hash{String => Object}
(in Landline::Probe context) Returns parsed query hash.
-
#query? ⇒ Boolean
(in Landline::Probe context) Checks if current request has urlencoded query string.
-
#query_shallow ⇒ Hash{String => Object}
(in Landline::Probe context) Returns shallow parsed query hash.
-
#redirect(path) ⇒ Object
(in Landline::Probe context) Do clientside request redirection via 302 code.
-
#redirect_with_method(path) ⇒ Object
(in Landline::Probe context) Do clientside request redirection via 307 code.
-
#request ⇒ Landline::Request
(in Landline::Probe context) Get the current request.
-
#session ⇒ Landline::Session::Session
(in Landline::Probe context) Return session storage hash.
-
#status(status) ⇒ Object
(also: #code)
(in Landline::Probe context) Set response status (generate response if one doesn’t exist yet).
-
#unescape_html(text) ⇒ Object
(in Landline::Probe context) Unescape HTML entities.
Instance Method Details
#call(application) ⇒ Array(Integer, Hash{String => Object}, Object)
(in Landline::Path context) Pass the requested environment to a different application
226 227 228 |
# File 'lib/landline/dsl/methods_probe.rb', line 226 def call(application) application.call(@origin.request.env) end |
#cookie(*params, **options) ⇒ Object
(in Landline::Probe context) Set response cookie
124 125 126 127 128 129 |
# File 'lib/landline/dsl/methods_probe.rb', line 124 def (*params, **) @origin.response = (@origin.response or Landline::Response.new) @origin.response.( Landline::Cookie.new(*params, **) ) end |
#defer(&callable) ⇒ Object
(in Landline::Probe context) Add a finalizer callable to the response
31 32 33 34 35 36 37 38 |
# File 'lib/landline/dsl/methods_probe.rb', line 31 def defer(&callable) rack = @origin.request.rack if rack.respond_to?(:response_finished) rack.response_finished.append(callable) end # puma for some reason isn't compatible with the 3.0.0 spec on this rack.after_reply.append(callable) if rack.respond_to?(:after_reply) end |
#delete_cookie(key, value = nil) ⇒ Object
(in Landline::Probe context) Delete a cookie If no value is provided, deletes all cookies with the same key
136 137 138 139 140 |
# File 'lib/landline/dsl/methods_probe.rb', line 136 def (key, value = nil) return unless @origin.response @origin.response.(key, value) end |
#delete_header(key, value = nil) ⇒ Object
(in Landline::Probe context) Delete a header value from the headers hash If no value is provided, deletes all key entries
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/landline/dsl/methods_probe.rb', line 105 def delete_header(key, value = nil) return unless @origin.response return if key.downcase == "status" unless key.match(Landline::Util::HeaderRegexp::TOKEN) raise ArgumentError, "header key has invalid characters" end unless value&.match(Landline::Util::HeaderRegexp::PRINTABLE) raise ArgumentError, "value key has invalid characters" end @origin.response.delete_header(key.to_s, value) end |
#escape_html(text) ⇒ Object
(in Landline::Probe context) Escape HTML entities
211 212 213 |
# File 'lib/landline/dsl/methods_probe.rb', line 211 def escape_html(text) Landline::Util.escape_html(text) end |
#file(path, mode = "r", *all, &block) ⇒ Object
(in Landline::Probe context) Open a file relative to current filepath
204 205 206 |
# File 'lib/landline/dsl/methods_probe.rb', line 204 def file(path, mode = "r", *all, &block) File.open("#{request.filepath}/#{path}", mode, *all, &block) end |
#form ⇒ Hash{String=>(String,Landline::Util::FormPart)}
reads request.input - may nullify request.body.
(in Landline::Probe context) Returns formdata
154 155 156 157 158 159 160 161 |
# File 'lib/landline/dsl/methods_probe.rb', line 154 def form _, opts = Landline::Util::ParserCommon.parse_value( request.headers["content-type"] ) Landline::Util::MultipartParser.new( request.input, opts["boundary"] ).to_h end |
#form? ⇒ Boolean
(in Landline::Probe context) Checks if current request has multipart/form-data associated with it
145 146 147 148 |
# File 'lib/landline/dsl/methods_probe.rb', line 145 def form? value, opts = _verify_content_type('multipart/form-data') !!(value && opts && opts['boundary']) end |
#header(key, value) ⇒ Object
(in Landline::Probe context) Set response header (generate response if one doesn’t exist yet)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/landline/dsl/methods_probe.rb', line 84 def header(key, value) return status(value) if key.downcase == "status" unless key.match(Landline::Util::HeaderRegexp::TOKEN) raise ArgumentError, "header key has invalid characters" end unless value&.match(Landline::Util::HeaderRegexp::PRINTABLE) raise ArgumentError, "value key has invalid characters" end @origin.response = (@origin.response or Landline::Response.new) key = key.downcase.to_s @origin.response.add_header(key, value) end |
#hijack ⇒ IO
(in Landline::Probe context) Fully hijack IO
76 77 78 |
# File 'lib/landline/dsl/methods_probe.rb', line 76 def hijack @origin.request.hijack.call end |
#json ⇒ Object
reads request.input - may nullify request.body.
(in Landline::Probe context) Return parse JSON object
197 198 199 |
# File 'lib/landline/dsl/methods_probe.rb', line 197 def json JSON.parse(request.input) end |
#json? ⇒ Boolean
(in Landline::Probe context) Check if body is a JSON object
189 190 191 |
# File 'lib/landline/dsl/methods_probe.rb', line 189 def json? !!_verify_content_type('application/json') end |
#jump(path) ⇒ Object
this essentially reprocesses the whole request - be mindful of processing time!
(in Landline::Probe context) Do serverside request redirection
44 45 46 47 |
# File 'lib/landline/dsl/methods_probe.rb', line 44 def jump(path) @origin.request.path = path throw(:finish, [307, { "x-internal-jump": true }, []]) end |
#partial_hijack(&block) ⇒ Object
(in Landline::Probe context) Set a partial hijack callback
68 69 70 71 |
# File 'lib/landline/dsl/methods_probe.rb', line 68 def partial_hijack(&block) @origin.response ||= Landline::Response.new @origin.response.add_header("rack.hijack", block) end |
#query ⇒ Hash{String => Object}
reads request.body - may nullify .input, .body data is memoized
(in Landline::Probe context) Returns parsed query hash
174 175 176 |
# File 'lib/landline/dsl/methods_probe.rb', line 174 def query Landline::Util::Query.new(request.body).parse end |
#query? ⇒ Boolean
(in Landline::Probe context) Checks if current request has urlencoded query string
166 167 168 |
# File 'lib/landline/dsl/methods_probe.rb', line 166 def query? !!_verify_content_type("application/x-www-form-urlencode") end |
#query_shallow ⇒ Hash{String => Object}
reads request.body - may nullify .input, .body data is memoized
(in Landline::Probe context) Returns shallow parsed query hash
182 183 184 |
# File 'lib/landline/dsl/methods_probe.rb', line 182 def query_shallow Landline::Util::Query.new(request.body).parse_shallow end |
#redirect(path) ⇒ Object
(in Landline::Probe context) Do clientside request redirection via 302 code
52 53 54 |
# File 'lib/landline/dsl/methods_probe.rb', line 52 def redirect(path) throw(:finish, [302, { "location": path }, []]) end |
#redirect_with_method(path) ⇒ Object
(in Landline::Probe context) Do clientside request redirection via 307 code
59 60 61 |
# File 'lib/landline/dsl/methods_probe.rb', line 59 def redirect_with_method(path) throw(:finish, [307, { "location": path }, []]) end |
#request ⇒ Landline::Request
(in Landline::Probe context) Get the current request
16 17 18 |
# File 'lib/landline/dsl/methods_probe.rb', line 16 def request @origin.request end |
#session ⇒ Landline::Session::Session
(in Landline::Probe context) Return session storage hash
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/landline/extensions/session.rb', line 81 def session return @session if @session @session = Landline::Session::Session.new( request..dig('session', 0)&.value, proc do |value| ("session") ("session", value) end ) request.postprocessors.append(proc do @session = nil end) @session end |
#status(status) ⇒ Object Also known as: code
(in Landline::Probe context) Set response status (generate response if one doesn’t exist yet)
23 24 25 26 |
# File 'lib/landline/dsl/methods_probe.rb', line 23 def status(status) @origin.response = (@origin.response or Landline::Response.new) @origin.response.status = status end |
#unescape_html(text) ⇒ Object
(in Landline::Probe context) Unescape HTML entities
218 219 220 |
# File 'lib/landline/dsl/methods_probe.rb', line 218 def unescape_html(text) Landline::Util.unescape_html(text) end |