Module: Binocs::ApplicationHelper

Defined in:
app/helpers/binocs/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#client_label(identifier) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'app/helpers/binocs/application_helper.rb', line 50

def client_label(identifier)
  return "Unknown" if identifier.blank?

  prefix, value = identifier.split(":", 2)
  case prefix
  when "session" then "Session #{value.to_s[0, 8]}"
  when "auth" then "Auth #{value.to_s[0, 8]}"
  when "ip" then "IP #{value}"
  else identifier
  end
end

#format_body(body) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/binocs/application_helper.rb', line 62

def format_body(body)
  return body if body.nil?

  begin
    parsed = JSON.parse(body)
    JSON.pretty_generate(parsed)
  rescue JSON::ParserError
    body
  end
end

#format_value(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/binocs/application_helper.rb', line 37

def format_value(value)
  case value
  when Hash, Array
    JSON.pretty_generate(value)
  when nil
    "null"
  else
    value.to_s
  end
rescue
  value.to_s
end

#method_badge_class(method) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/binocs/application_helper.rb', line 5

def method_badge_class(method)
  case method.to_s.upcase
  when "GET"
    "bg-emerald-500/15 text-emerald-400 ring-1 ring-emerald-500/20"
  when "POST"
    "bg-cyan-500/15 text-cyan-400 ring-1 ring-cyan-500/20"
  when "PUT", "PATCH"
    "bg-amber-500/15 text-amber-400 ring-1 ring-amber-500/20"
  when "DELETE"
    "bg-rose-500/15 text-rose-400 ring-1 ring-rose-500/20"
  else
    "bg-zinc-800 text-zinc-400 ring-1 ring-zinc-700"
  end
end

#relative_time(time) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/binocs/application_helper.rb', line 73

def relative_time(time)
  return "N/A" if time.nil?

  seconds = (Time.current - time).to_i
  return "just now" if seconds < 5

  minutes = seconds / 60
  hours = minutes / 60

  if hours >= 3
    time.strftime("%b %d, %H:%M:%S")
  elsif hours >= 1
    "#{hours} #{hours == 1 ? 'hour' : 'hours'} ago"
  elsif minutes >= 1
    "#{minutes} #{minutes == 1 ? 'minute' : 'minutes'} ago"
  else
    "#{seconds} #{seconds == 1 ? 'second' : 'seconds'} ago"
  end
end

#status_badge_class(status) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/binocs/application_helper.rb', line 20

def status_badge_class(status)
  return "bg-zinc-800 text-zinc-400 ring-1 ring-zinc-700" if status.nil?

  case status
  when 200..299
    "bg-emerald-500/15 text-emerald-400 ring-1 ring-emerald-500/20"
  when 300..399
    "bg-cyan-500/15 text-cyan-400 ring-1 ring-cyan-500/20"
  when 400..499
    "bg-amber-500/15 text-amber-400 ring-1 ring-amber-500/20"
  when 500..599
    "bg-rose-500/15 text-rose-400 ring-1 ring-rose-500/20"
  else
    "bg-zinc-800 text-zinc-400 ring-1 ring-zinc-700"
  end
end