Module: Hyde::DSL::ProbeMethods
- Included in:
- ProbeContext, TemplateContext
- Defined in:
- lib/hyde/dsl/methods_probe.rb
Overview
Common methods for Probe objects
Instance Method Summary collapse
-
#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,Hyde::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).
-
#remove_header(key, value = nil) ⇒ Object
Delete a header value from the headers hash If no value is provided, deletes all key entries.
-
#request ⇒ Hyde::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
#escape_html(text) ⇒ Object
Escape HTML entities
99 100 101 |
# File 'lib/hyde/dsl/methods_probe.rb', line 99 def escape_html(text) Hyde::Util.escape_html(text) end |
#file(path, mode = "r", *all, &block) ⇒ Object
Open a file relative to current filepath
93 94 95 |
# File 'lib/hyde/dsl/methods_probe.rb', line 93 def file(path, mode = "r", *all, &block) File.open("#{request.filepath}/#{path}", mode, *all, &block) end |
#form ⇒ Hash{String=>(String,Hyde::Util::FormPart)}
Returns formdata
82 83 84 85 86 87 88 89 |
# File 'lib/hyde/dsl/methods_probe.rb', line 82 def form _, opts = Hyde::Util::ParserCommon.parse_value( request.headers["content-type"] ) Hyde::Util::MultipartParser.new( request.input, opts["boundary"] ).to_h end |
#form? ⇒ Boolean
Checks if current request has multipart/form-data associated with it
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hyde/dsl/methods_probe.rb', line 68 def form? value, opts = Hyde::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/hyde/dsl/methods_probe.rb', line 30 def header(key, value) return status(value) if key.downcase == "status" if key.match(Hyde::Util::HeaderRegexp::TOKEN) raise ArgumentError, "header key has invalid characters" end if value&.match(Hyde::Util::HeaderRegexp::PRINTABLE) raise ArgumentError, "value key has invalid characters" end @origin.response = (@origin.response or Hyde::Response.new) key = key.downcase @origin.response.add_header(key, value) end |
#remove_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/hyde/dsl/methods_probe.rb', line 50 def remove_header(key, value = nil) return unless @origin.response return if key.downcase == "status" if key.match(Hyde::Util::HeaderRegexp::TOKEN) raise ArgumentError, "header key has invalid characters" end if value&.match(Hyde::Util::HeaderRegexp::PRINTABLE) raise ArgumentError, "value key has invalid characters" end @origin.response.delete_header(key, value) end |
#request ⇒ Hyde::Request
Get the current request
14 15 16 |
# File 'lib/hyde/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/hyde/dsl/methods_probe.rb', line 20 def status(status) @origin.response = (@origin.response or Hyde::Response.new) @origin.response.status = status end |
#unescape_html(text) ⇒ Object
Unescape HTML entities
105 106 107 |
# File 'lib/hyde/dsl/methods_probe.rb', line 105 def unescape_html(text) Hyde::Util.unescape_html(text) end |