Class: Rack::Unpoly::Inspector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rack/unpoly/inspector.rb

Overview

Easily inspect the Unpoly environment of the current request. Inspectors are not normally instantiated by users, but accessed through env or one of the convenience wrappers for Roda and Sinatra.

Direct Known Subclasses

Sinatra::Unpoly::SinatraInspector

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Inspector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Inspector.



21
22
23
# File 'lib/rack/unpoly/inspector.rb', line 21

def initialize(request)
  @request = request
end

Instance Method Details

#any_target?(tested_target) ⇒ Boolean

Determine if the tested_target is the current target for a successful or failed request.

Returns:

  • (Boolean)


66
67
68
# File 'lib/rack/unpoly/inspector.rb', line 66

def any_target?(tested_target)
  target?(tested_target) || fail_target?(tested_target)
end

#fail_targetString?

The CSS selector for the fragment Unpoly will update if the request fails. Requires Unpoly >= 0.50

Returns:

  • (String, nil)


51
52
53
# File 'lib/rack/unpoly/inspector.rb', line 51

def fail_target
  get_header("HTTP_X_UP_FAIL_TARGET")
end

#fail_target?(tested_target) ⇒ Boolean

Determine if the tested_target is the current target for a failed request. Requires Unpoly >= 0.50

Returns:

  • (Boolean)


59
60
61
# File 'lib/rack/unpoly/inspector.rb', line 59

def fail_target?(tested_target)
  query_target(fail_target, tested_target)
end

#query_target(actual_target, tested_target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • actual_target (String)
  • tested_target (String)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rack/unpoly/inspector.rb', line 97

def query_target(actual_target, tested_target)
  if up?
    if actual_target == tested_target
      true
    elsif actual_target == "html"
      true
    elsif actual_target == "body"
      !%w[head title meta].include?(tested_target)
    else
      false
    end
  else
    true
  end
end

#set_title(response, new_title) ⇒ Object

Set the page title.

Parameters:

  • response (Rack::Response)
  • new_title (String)

    the title to set



75
76
77
# File 'lib/rack/unpoly/inspector.rb', line 75

def set_title(response, new_title)
  response.headers["X-Up-Title"] = new_title
end

#targetString?

The actual target as requested by Unpoly.

Returns:

  • (String, nil)


43
44
45
# File 'lib/rack/unpoly/inspector.rb', line 43

def target
  get_header("HTTP_X_UP_TARGET")
end

#target?(tested_target) ⇒ Boolean

Identify if the tested_target will match the actual target requested.

Parameters:

  • tested_target (String)

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/unpoly/inspector.rb', line 36

def target?(tested_target)
  query_target(target, tested_target)
end

#unpoly?Boolean Also known as: up?

Determine if this is an Unpoly request.

Returns:

  • (Boolean)


27
28
29
# File 'lib/rack/unpoly/inspector.rb', line 27

def unpoly?
  target.to_s.strip != ""
end

#validate?Boolean

Determine if this is a validate request.

Returns:

  • (Boolean)


82
83
84
# File 'lib/rack/unpoly/inspector.rb', line 82

def validate?
  validate_name.to_s.strip != ""
end

#validate_nameString?

The name attribute of the form field that triggered the validation.

Returns:

  • (String, nil)


90
91
92
# File 'lib/rack/unpoly/inspector.rb', line 90

def validate_name
  get_header("HTTP_X_UP_VALIDATE")
end