Add page_headers and page_contents helpers.

This commit is contained in:
Markus Reiter 2020-12-12 21:56:07 +01:00 committed by Sam Ford
parent 24046c3182
commit f5d311490c
No known key found for this signature in database
GPG Key ID: 95209E46C7FFDEFE
2 changed files with 18 additions and 5 deletions

View File

@ -80,6 +80,20 @@ module Homebrew
(strategy.const_defined?(:PRIORITY) ? -strategy::PRIORITY : -DEFAULT_PRIORITY) (strategy.const_defined?(:PRIORITY) ? -strategy::PRIORITY : -DEFAULT_PRIORITY)
end end
end end
def self.page_headers(url)
@headers ||= {}
@headers[url] ||= curl_output("--head", "--location", url).stdout
.split("\r\n\r\n", 2).first
.split("\r\n").drop(1)
.map { |header| header.split(/:\s*/, 2) }
.to_h.transform_keys(&:downcase)
end
def self.page_contents(url)
@page_contents ||= {}
@page_contents[url] ||= URI.parse(url).open.read
end
end end
end end
end end

View File

@ -30,8 +30,8 @@ module Homebrew
URL_MATCH_REGEX = %r{^https?://}i.freeze URL_MATCH_REGEX = %r{^https?://}i.freeze
# Whether the strategy can be applied to the provided URL. # Whether the strategy can be applied to the provided URL.
# PageMatch will technically match any HTTP URL but it's only usable # PageMatch will technically match any HTTP URL but is only
# when the formula has a `livecheck` block containing a regex. # usable with a `livecheck` block containing a regex.
# #
# @param url [String] the URL to match against # @param url [String] the URL to match against
# @return [Boolean] # @return [Boolean]
@ -47,9 +47,8 @@ module Homebrew
# content # content
# @return [Array] # @return [Array]
def self.page_matches(url, regex) def self.page_matches(url, regex)
page = URI.parse(url).open.read page = Strategy.page_contents(url)
matches = page.scan(regex) page.scan(regex).map(&:first).uniq
matches.map(&:first).uniq
end end
# Checks the content at the URL for new versions, using the provided # Checks the content at the URL for new versions, using the provided