Class: Sidekiq::Belt::Community::TopLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/belt/community/top_label.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ TopLabel

Returns a new instance of TopLabel.



18
19
20
21
# File 'lib/sidekiq/belt/community/top_label.rb', line 18

def initialize(content)
  @content = content
  @top_label = fetch_top_label
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



16
17
18
# File 'lib/sidekiq/belt/community/top_label.rb', line 16

def content
  @content
end

#top_labelObject (readonly)

Returns the value of attribute top_label.



16
17
18
# File 'lib/sidekiq/belt/community/top_label.rb', line 16

def top_label
  @top_label
end

Class Method Details

.use!Object



10
11
12
13
14
# File 'lib/sidekiq/belt/community/top_label.rb', line 10

def self.use!
  Sidekiq::WebActionHelper.change_layout do |content|
    Sidekiq::Belt::Community::TopLabel.new(content).apply
  end
end

Instance Method Details

#applyObject



23
24
25
26
27
# File 'lib/sidekiq/belt/community/top_label.rb', line 23

def apply
  inject_styles_and_label unless top_label.empty?

  content
end

#fetch_top_labelObject



29
30
31
# File 'lib/sidekiq/belt/community/top_label.rb', line 29

def fetch_top_label
  (Sidekiq::Belt.config.top_label || {}).fetch(Sidekiq::Belt.env, {})
end

#inject_styles_and_labelObject



33
34
35
36
# File 'lib/sidekiq/belt/community/top_label.rb', line 33

def inject_styles_and_label
  content.gsub!("</head>", "<style nonce='#{nonce_id}'>#{styles}</style></head>")
  content.sub!(/<body[^>]*>/, "\\0#{top_label_div}")
end

#nonce_idObject



38
39
40
# File 'lib/sidekiq/belt/community/top_label.rb', line 38

def nonce_id
  content[/nonce="([^"]*)"/, 1].to_s
end

#stylesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sidekiq/belt/community/top_label.rb', line 47

def styles
  <<~CSS
    header:first-of-type, .navbar-default {
      margin-top: 20px;
    }

    .top_label {
      background: #{::Rack::Utils.escape_html(top_label.fetch(:background_color, "red"))} !important;
      z-index: 99999;
      text-align: center;
      color: #{::Rack::Utils.escape_html(top_label.fetch(:color, "white"))};
      position: fixed;
      width: 100%;
      top: 0;
      right: 0;
    }
  CSS
end

#top_label_divObject



42
43
44
45
# File 'lib/sidekiq/belt/community/top_label.rb', line 42

def top_label_div
  text = ::Rack::Utils.escape_html(top_label[:text].to_s)
  "<div class='container-fluid top_label'>&nbsp;#{text}&nbsp;</div>"
end