BathHacked/energy-sparks

View on GitHub
app/services/alerts/fetch_content.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
module Alerts
  class FetchContent
    def initialize(alert)
      @alert = alert
    end

    def content_versions(scope:, today: Time.zone.today)
      rating = @alert.rating
      return [] if rating.blank?

      alert_type_ratings = AlertTypeRating.
        for_rating(rating.to_f.round(1)).
        where(alert_type: @alert.alert_type).
        where(:"#{scope}_active" => true)

      current_content = alert_type_ratings.map(&:current_content).compact
      current_content.select {|content| content.meets_timings?(scope: scope, today: today)}
    end

    def content_versions_with_priority(scope:, today: Time.zone.today)
      content_versions(scope: scope, today: today).map do |content_version|
        [content_version, calculate_score(content_version, scope)]
      end
    end


    private

    def calculate_score(content_version, scope)
      ((11 - @alert.rating) * content_version.read_attribute(:"#{scope}_weighting") * @alert.priority_data.fetch('time_of_year_relevance') {5.0}) / 1000
    end
  end
end