require 'date'

module CompileExtensions
  class EolDeprecations
    def initialize(manifest)
      @manifest = manifest
    end

    def deprecation(dependency)
      @eol_dates ||= Array(@manifest['dependency_deprecation_dates']).map do |d|
        Deprecation.new(d)
      end
      @eol_dates.detect { |d| d.match?(dependency) }
    end

    class Deprecation
      attr_reader :name, :version_line, :date, :link
      def initialize(hash)
        @name = hash['name']
        @match = Regexp.new(hash['match'])
        @version_line = hash['version_line']
        @date = hash['date']
        @link = hash['link']
      end

      def match?(dependency)
        return false unless dependency
        @name == dependency['name'] && @match.match(dependency['version'])
      end

      def warning?
        return false unless @date
        date = @date.is_a?(String) ? Date.parse(@date, false) : @date
        (date - Date.today).to_i <= 30
      end
    end
  end
end

require 'date'

module CompileExtensions
  class EolDeprecations
    def initialize(manifest)
      @manifest = manifest
    end

    def deprecation(dependency)
      @eol_dates ||= Array(@manifest['dependency_deprecation_dates']).map do |d|
        Deprecation.new(d)
      end
      @eol_dates.detect { |d| d.match?(dependency) }
    end

    class Deprecation
      attr_reader :name, :version_line, :date, :link
      def initialize(hash)
        @name = hash['name']
        @match = Regexp.new(hash['match'] || "")
        @version_line = hash['version_line']
        @date = hash['date']
        @link = hash['link']
      end

      def match?(dependency)
        return false unless dependency
        @name == dependency['name'] && @match.match(dependency['version'])
      end

      def warning?
        return false unless @date
        date = @date.is_a?(String) ? Date.parse(@date, false) : @date
        (date - Date.today).to_i <= 30
      end
    end
  end
end

Add default empty string for eol_extensions if no match
Add default empty string for eol_extensions if no match

Co-authored-by: David Freilich <0106e5ee349bb58d7c51800b6acf5b57cf80aef6@pivotal.io>
