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

Instance Method Details

#call(application) ⇒ Array(Integer, Hash{String => Object}, Object)

(in Landline::Path context) Pass the requested environment to a different application

Parameters:

  • application (#call)

    Rack application

Returns:

  • (Array(Integer, Hash{String => Object}, Object))

    response



226
227
228
# File 'lib/landline/dsl/methods_probe.rb', line 226

def call(application)
  application.call(@origin.request.env)
end

(in Landline::Probe context) Set response cookie

See Also:

  • Cookie.new


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

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

#defer(&callable) ⇒ Object

(in Landline::Probe context) Add a finalizer callable to the response

Parameters:



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

(in Landline::Probe context) 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



136
137
138
139
140
# File 'lib/landline/dsl/methods_probe.rb', line 136

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

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

Parameters:

  • key (String)

    header name

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

    header value



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

See Also:



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

See Also:

  • File.open


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

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

Note:

reads request.input - may nullify request.body.

(in Landline::Probe context) Returns formdata

Returns:



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

Returns:

  • (Boolean)


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)

Parameters:

  • key (String)

    header name

  • value (String)

    header value



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

#hijackIO

(in Landline::Probe context) Fully hijack IO

Returns:

  • (IO)


76
77
78
# File 'lib/landline/dsl/methods_probe.rb', line 76

def hijack
  @origin.request.hijack.call
end

#jsonObject

Note:

reads request.input - may nullify request.body.

(in Landline::Probe context) Return parse JSON object

Returns:

  • (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

Returns:

  • (Boolean)


189
190
191
# File 'lib/landline/dsl/methods_probe.rb', line 189

def json?
  !!_verify_content_type('application/json')
end

#jump(path) ⇒ Object

Note:

this essentially reprocesses the whole request - be mindful of processing time!

(in Landline::Probe context) Do serverside request redirection

Parameters:

  • path (String)


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

Parameters:

  • block (#call)

    Callable block



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

#queryHash{String => Object}

Note:

reads request.body - may nullify .input, .body data is memoized

(in Landline::Probe context) Returns parsed query hash

Returns:

  • (Hash{String => Object})

    query data



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

Returns:

  • (Boolean)


166
167
168
# File 'lib/landline/dsl/methods_probe.rb', line 166

def query?
  !!_verify_content_type("application/x-www-form-urlencode")
end

#query_shallowHash{String => Object}

Note:

reads request.body - may nullify .input, .body data is memoized

(in Landline::Probe context) Returns shallow parsed query hash

Returns:

  • (Hash{String => Object})

    query data



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

Parameters:

  • path (String)


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

Parameters:

  • path (String)


59
60
61
# File 'lib/landline/dsl/methods_probe.rb', line 59

def redirect_with_method(path)
  throw(:finish, [307, { "location": path }, []])
end

#requestLandline::Request

(in Landline::Probe context) Get the current request

Returns:



16
17
18
# File 'lib/landline/dsl/methods_probe.rb', line 16

def request
  @origin.request
end

#sessionLandline::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.cookies.dig('session', 0)&.value,
    proc do |value|
      delete_cookie("session")
      cookie("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)

Parameters:

  • status (Integer)

    http status code



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

See Also:



218
219
220
# File 'lib/landline/dsl/methods_probe.rb', line 218

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