From f5d311490c79f8e76352711ad8cc9302b6a52ab9 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 12 Dec 2020 21:56:07 +0100 Subject: [PATCH] Add `page_headers` and `page_contents` helpers. --- Library/Homebrew/livecheck/strategy.rb | 14 ++++++++++++++ Library/Homebrew/livecheck/strategy/page_match.rb | 9 ++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy.rb b/Library/Homebrew/livecheck/strategy.rb index 4115432951..f368479843 100644 --- a/Library/Homebrew/livecheck/strategy.rb +++ b/Library/Homebrew/livecheck/strategy.rb @@ -80,6 +80,20 @@ module Homebrew (strategy.const_defined?(:PRIORITY) ? -strategy::PRIORITY : -DEFAULT_PRIORITY) 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 diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index 49961eff59..478fa2bcda 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -30,8 +30,8 @@ module Homebrew URL_MATCH_REGEX = %r{^https?://}i.freeze # Whether the strategy can be applied to the provided URL. - # PageMatch will technically match any HTTP URL but it's only usable - # when the formula has a `livecheck` block containing a regex. + # PageMatch will technically match any HTTP URL but is only + # usable with a `livecheck` block containing a regex. # # @param url [String] the URL to match against # @return [Boolean] @@ -47,9 +47,8 @@ module Homebrew # content # @return [Array] def self.page_matches(url, regex) - page = URI.parse(url).open.read - matches = page.scan(regex) - matches.map(&:first).uniq + page = Strategy.page_contents(url) + page.scan(regex).map(&:first).uniq end # Checks the content at the URL for new versions, using the provided