Class: GovukWebBanners::UpratingBanner

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_web_banners/uprating_banner.rb

Constant Summary collapse

"../../config/govuk_web_banners/uprating_banners.yml".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes:) ⇒ UpratingBanner

Returns a new instance of UpratingBanner.



23
24
25
26
27
28
29
# File 'lib/govuk_web_banners/uprating_banner.rb', line 23

def initialize(attributes:)
  @name = attributes["name"]
  @text = attributes["text"]
  @page_paths = attributes["page_paths"]
  @start_date = attributes["start_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["start_date"]) : Time.at(0)
  @end_date = attributes["end_date"] ? ActiveSupport::TimeZone[GovukWebBanners::TIME_ZONE].parse(attributes["end_date"]) : Time.now + 10.years
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



21
22
23
# File 'lib/govuk_web_banners/uprating_banner.rb', line 21

def end_date
  @end_date
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/govuk_web_banners/uprating_banner.rb', line 21

def name
  @name
end

#page_pathsObject (readonly)

Returns the value of attribute page_paths.



21
22
23
# File 'lib/govuk_web_banners/uprating_banner.rb', line 21

def page_paths
  @page_paths
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



21
22
23
# File 'lib/govuk_web_banners/uprating_banner.rb', line 21

def start_date
  @start_date
end

#textObject (readonly)

Returns the value of attribute text.



21
22
23
# File 'lib/govuk_web_banners/uprating_banner.rb', line 21

def text
  @text
end

Class Method Details

.active_bannersObject



11
12
13
# File 'lib/govuk_web_banners/uprating_banner.rb', line 11

def self.active_banners
  all_banners.select(&:active?)
end

.all_bannersObject



15
16
17
18
19
# File 'lib/govuk_web_banners/uprating_banner.rb', line 15

def self.all_banners
  uprating_banners_urls_file_path = Rails.root.join(__dir__, BANNER_CONFIG_FILE)
  uprating_banners_data = YAML.load_file(uprating_banners_urls_file_path)
  uprating_banners_data["banners"].map { |attributes| UpratingBanner.new(attributes:) }
end

.for_path(path) ⇒ Object



5
6
7
8
9
# File 'lib/govuk_web_banners/uprating_banner.rb', line 5

def self.for_path(path)
  active_banners.find do |banner|
    return banner if banner.page_paths.include?(path)
  end
end

Instance Method Details

#active?Boolean

NB: .between? is inclusive. To make it exclude the end date, we set the end range as

1 second earlier.

Returns:

  • (Boolean)


33
34
35
# File 'lib/govuk_web_banners/uprating_banner.rb', line 33

def active?
  Time.zone.now.between?(start_date, end_date - 1.second)
end