Module: Landline::DSL::ProbeMethods
- Included in:
- ProbeContext, TemplateContext
- Defined in:
- lib/landline/dsl/methods_probe.rb
Overview
Common methods for Probe objects
Instance Method Summary collapse
-
#cookie(*params, **options) ⇒ Object
Set response cookie.
-
#delete_cookie(key, value = nil) ⇒ Object
Delete a cookie If no value is provided, deletes all cookies with the same key.
-
#delete_header(key, value = nil) ⇒ Object
Delete a header value from the headers hash If no value is provided, deletes all key entries.
-
#escape_html(text) ⇒ Object
Escape HTML entities.
-
#file(path, mode = "r", *all, &block) ⇒ Object
Open a file relative to current filepath.
-
#form ⇒ Hash{String=>(String,Landline::Util::FormPart)}
Returns formdata.
-
#form? ⇒ Boolean
Checks if current request has multipart/form-data associated with it.
-
#header(key, value) ⇒ Object
Set response header (generate response if one doesn’t exist yet).
-
#request ⇒ Landline::Request
Get the current request.
-
#status(status) ⇒ Object
(also: #code)
Set response status (generate response if one doesn’t exist yet).
-
#unescape_html(text) ⇒ Object
Unescape HTML entities.
Instance Method Details
#cookie(*params, **options) ⇒ Object
Set response cookie
68 69 70 71 72 73 |
# File 'lib/landline/dsl/methods_probe.rb', line 68 def (*params, **) @origin.response = (@origin.response or Landline::Response.new) @origin.response.( Landline::Cookie.new(*params, **) ) end |
#delete_cookie(key, value = nil) ⇒ Object
Delete a cookie If no value is provided, deletes all cookies with the same key
79 80 81 82 83 |
# File 'lib/landline/dsl/methods_probe.rb', line 79 def (key, value = nil) return unless @origin.response @origin.response.(key, value) end |
#delete_header(key, value = nil) ⇒ Object
Delete a header value from the headers hash If no value is provided, deletes all key entries
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/landline/dsl/methods_probe.rb', line 50 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, value) end |
#escape_html(text) ⇒ Object
Escape HTML entities
118 119 120 |
# File 'lib/landline/dsl/methods_probe.rb', line 118 def escape_html(text) Landline::Util.escape_html(text) end |
#file(path, mode = "r", *all, &block) ⇒ Object
Open a file relative to current filepath
112 113 114 |
# File 'lib/landline/dsl/methods_probe.rb', line 112 def file(path, mode = "r", *all, &block) File.open("#{request.filepath}/#{path}", mode, *all, &block) end |
#form ⇒ Hash{String=>(String,Landline::Util::FormPart)}
Returns formdata
101 102 103 104 105 106 107 108 |
# File 'lib/landline/dsl/methods_probe.rb', line 101 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
Checks if current request has multipart/form-data associated with it
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/landline/dsl/methods_probe.rb', line 87 def form? value, opts = Landline::Util::ParserCommon.parse_value( request.headers["content-type"] ) if value == "multipart/form-data" and opts["boundary"] true else false end end |
#header(key, value) ⇒ Object
Set response header (generate response if one doesn’t exist yet)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/landline/dsl/methods_probe.rb', line 30 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 @origin.response.add_header(key, value) end |
#request ⇒ Landline::Request
Get the current request
14 15 16 |
# File 'lib/landline/dsl/methods_probe.rb', line 14 def request @origin.request end |
#status(status) ⇒ Object Also known as: code
Set response status (generate response if one doesn’t exist yet)
20 21 22 23 |
# File 'lib/landline/dsl/methods_probe.rb', line 20 def status(status) @origin.response = (@origin.response or Landline::Response.new) @origin.response.status = status end |
#unescape_html(text) ⇒ Object
Unescape HTML entities
124 125 126 |
# File 'lib/landline/dsl/methods_probe.rb', line 124 def unescape_html(text) Landline::Util.unescape_html(text) end |