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

Instance Method Details

Set response cookie

See Also:

  • Cookie.new


68
69
70
71
72
73
# File 'lib/landline/dsl/methods_probe.rb', line 68

def cookie(*params, **options)
  @origin.response = (@origin.response or Landline::Response.new)
  @origin.response.add_cookie(
    Landline::Cookie.new(*params, **options)
  )
end

Delete a cookie If no value is provided, deletes all cookies with the same key

Parameters:

  • key (String)

    cookie key

  • value (String, nil) (defaults to: nil)

    cookie.value



79
80
81
82
83
# File 'lib/landline/dsl/methods_probe.rb', line 79

def delete_cookie(key, value = nil)
  return unless @origin.response

  @origin.response.delete_cookie(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

Parameters:

  • key (String)

    header name

  • value (String, nil) (defaults to: nil)

    header value



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

See Also:



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

See Also:

  • File.open


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

#formHash{String=>(String,Landline::Util::FormPart)}

Returns formdata

Returns:



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

Returns:

  • (Boolean)


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)

Parameters:

  • key (String)

    header name

  • value (String)

    header value



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

#requestLandline::Request

Get the current request

Returns:



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)

Parameters:

  • status (Integer)

    http status code



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

See Also:



124
125
126
# File 'lib/landline/dsl/methods_probe.rb', line 124

def unescape_html(text)
  Landline::Util.unescape_html(text)
end