# typed: true # frozen_string_literal: true require "bundle_version" module Homebrew module Livecheck module Strategy # The {Sparkle} strategy fetches content at a URL and parses # it as a Sparkle appcast in XML format. # # @api private class Sparkle extend T::Sig # A priority of zero causes livecheck to skip the strategy. We only # apply {Sparkle} using `strategy :sparkle` in a `livecheck` block, # as we can't automatically determine when this can be successfully # applied to a URL without fetching the content. PRIORITY = 0 # The `Regexp` used to determine if the strategy applies to the URL. URL_MATCH_REGEX = %r{^https?://}i.freeze # Whether the strategy can be applied to the provided URL. # The strategy will technically match any HTTP URL but is # only usable with a `livecheck` block containing a regex # or block. sig { params(url: String).returns(T::Boolean) } def self.match?(url) URL_MATCH_REGEX.match?(url) end # @api private Item = Struct.new( # @api public :title, # @api private :pub_date, # @api public :url, # @api private :bundle_version, keyword_init: true, ) do extend T::Sig extend Forwardable # @api public delegate version: :bundle_version # @api public delegate short_version: :bundle_version end sig { params(content: String).returns(T.nilable(Item)) } def self.item_from_content(content) require "rexml/document" parsing_tries = 0 xml = begin REXML::Document.new(content) rescue REXML::UndefinedNamespaceException => e undefined_prefix = e.to_s[/Undefined prefix ([^ ]+) found/i, 1] raise if undefined_prefix.blank? # Only retry parsing once after removing prefix from content parsing_tries += 1 raise if parsing_tries > 1 # When an XML document contains a prefix without a corresponding # namespace, it's necessary to remove the the prefix from the # content to be able to successfully parse it using REXML content = content.gsub(%r{(