Module: Sentiero::Web::Formatting

Included in:
BaseApp, Views::BaseView
Defined in:
lib/sentiero/web/formatting.rb

Overview

Small presentation formatters shared by the Rack apps and the view layer.

Instance Method Summary collapse

Instance Method Details

#format_duration(first_event_at, last_event_at) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sentiero/web/formatting.rb', line 22

def format_duration(first_event_at, last_event_at)
  return "N/A" unless first_event_at && last_event_at

  # Event timestamps are in milliseconds
  total_ms = (last_event_at - first_event_at).abs
  total_seconds = (total_ms / 1000.0).round

  if total_seconds < 60
    "#{total_seconds}s"
  elsif total_seconds < 3600
    minutes = total_seconds / 60
    seconds = total_seconds % 60
    (seconds > 0) ? "#{minutes}m #{seconds}s" : "#{minutes}m"
  else
    hours = total_seconds / 3600
    minutes = (total_seconds % 3600) / 60
    (minutes > 0) ? "#{hours}h #{minutes}m" : "#{hours}h"
  end
end

#format_vital(metric, value) ⇒ Object

CLS is a unitless ratio (3 decimals); the other Web Vitals are millisecond durations.



18
19
20
# File 'lib/sentiero/web/formatting.rb', line 18

def format_vital(metric, value)
  (metric == "CLS") ? format("%.3f", value) : "#{value.round} ms"
end

#parse_browser(user_agent) ⇒ Object



13
14
15
# File 'lib/sentiero/web/formatting.rb', line 13

def parse_browser(user_agent)
  Sentiero::UserAgent.browser(user_agent)
end

#parse_device(user_agent) ⇒ Object



9
10
11
# File 'lib/sentiero/web/formatting.rb', line 9

def parse_device(user_agent)
  Sentiero::UserAgent.device(user_agent)
end