Add FollowRedirection livecheck strategy.
This commit is contained in:
parent
f5d311490c
commit
b293acc89b
@ -110,5 +110,6 @@ require_relative "strategy/launchpad"
|
||||
require_relative "strategy/npm"
|
||||
require_relative "strategy/page_match"
|
||||
require_relative "strategy/pypi"
|
||||
require_relative "strategy/follow_redirection"
|
||||
require_relative "strategy/sourceforge"
|
||||
require_relative "strategy/xorg"
|
||||
|
||||
47
Library/Homebrew/livecheck/strategy/follow_redirection.rb
Normal file
47
Library/Homebrew/livecheck/strategy/follow_redirection.rb
Normal file
@ -0,0 +1,47 @@
|
||||
# typed: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "page_match"
|
||||
|
||||
module Homebrew
|
||||
module Livecheck
|
||||
module Strategy
|
||||
# The {FollowRedirection} strategy follows all URL redirections and scans
|
||||
# the final URL for matching text using the provided regex.
|
||||
#
|
||||
# @api private
|
||||
class FollowRedirection
|
||||
extend T::Sig
|
||||
|
||||
NICE_NAME = "Follow HTTP Redirection"
|
||||
|
||||
# We set the priority to zero since this cannot
|
||||
# be detected automatically.
|
||||
PRIORITY = 0
|
||||
|
||||
# Whether the strategy can be applied to the provided URL.
|
||||
# FollowRedirection will technically match any HTTP URL but is
|
||||
# only usable with a `livecheck` block containing a regex.
|
||||
sig { params(url: String).returns(T::Boolean) }
|
||||
def self.match?(url)
|
||||
url.match?(%r{^https?://})
|
||||
end
|
||||
|
||||
# Checks the final URL for new versions after following all redirections,
|
||||
# using the provided regex for matching.
|
||||
sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) }
|
||||
def self.find_versions(url, regex)
|
||||
raise ArgumentError, "A regular expression is required for the #{NICE_NAME} strategy." if regex.nil?
|
||||
|
||||
match_data = { matches: {}, regex: regex, url: url }
|
||||
|
||||
if (location = Strategy.page_headers(url)["location"]) && (match = location[regex, 1])
|
||||
match_data[:matches][match] = Version.new(match)
|
||||
end
|
||||
|
||||
match_data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user