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
-
#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).
Instance Method Details
#file(path, mode = "r", *all, &block) ⇒ Object
Open a file relative to current filepath
88 89 90 |
# File 'lib/hyde/dsl/methods_probe.rb', line 88 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
79 80 81 82 83 84 |
# File 'lib/hyde/dsl/methods_probe.rb', line 79 def form _, opts = Hyde::Util.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
67 68 69 70 71 72 73 74 75 |
# File 'lib/hyde/dsl/methods_probe.rb', line 67 def form? value, opts = Hyde::Util.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)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/hyde/dsl/methods_probe.rb', line 29 def header(key, value) return status(value) if key.downcase == "status" if key.match(/(?:[(),\/:;<=>?@\[\]{}"]|[^ -~])/) raise StandardError, "header key has invalid characters" end if value.match(/[^ -~]/) raise StandardError, "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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/hyde/dsl/methods_probe.rb', line 49 def remove_header(key, value = nil) return unless @origin.response return if key.downcase == "status" if key.match(/(?:[(),\/:;<=>?@\[\]{}"]|[^ -~])/) raise StandardError, "header key has invalid characters" end if value&.match(/[^ -~]/) raise StandardError, "value key has invalid characters" end @origin.response.delete_header(key, value) end |
#request ⇒ Hyde::Request
Get the current request
13 14 15 |
# File 'lib/hyde/dsl/methods_probe.rb', line 13 def request @origin.request end |