Class: Homebrew::Livecheck::Strategy::PageMatch

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/livecheck/strategy/page_match.rb

Overview

The PageMatch strategy fetches content at a URL and scans it for matching text using the provided regex.

This strategy can be used in a livecheck block when no specific strategies apply to a given URL. Though PageMatch will technically match any HTTP URL, the strategy also requires a regex to function.

The PageMatch#find_versions method is also used within other strategies, to handle the process of identifying version text in content.

Constant Summary collapse

NICE_NAME =
"Page match"
PRIORITY =

A priority of zero causes livecheck to skip the strategy. We do this for PageMatch so we can selectively apply the strategy only when a regex is provided in a livecheck block.

0
URL_MATCH_REGEX =

The Regexp used to determine if the strategy applies to the URL.

%r{^https?://}i.freeze

Class Method Summary collapse

Class Method Details

.find_versions(url, regex) ⇒ Hash

Checks the content at the URL for new versions, using the provided regex for matching.

Parameters:

  • url (String)

    the URL of the content to check

  • regex (Regexp)

    a regex used for matching versions in content

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
# File 'brew/Library/Homebrew/livecheck/strategy/page_match.rb', line 58

def self.find_versions(url, regex)
  match_data = { matches: {}, regex: regex, url: url }

  page_matches(url, regex).each do |match|
    match_data[:matches][match] = Version.new(match)
  end

  match_data
end

.match?(url) ⇒ Boolean

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.

Parameters:

  • url (String)

    the URL to match against

Returns:

  • (Boolean)


37
38
39
# File 'brew/Library/Homebrew/livecheck/strategy/page_match.rb', line 37

def self.match?(url)
  URL_MATCH_REGEX.match?(url)
end

.page_matches(url, regex) ⇒ Array

Fetches the content at the URL, uses the regex to match text, and returns an array of unique matches.

Parameters:

  • url (String)

    the URL of the content to check

  • regex (Regexp)

    a regex used for matching versions in the content

Returns:

  • (Array)


47
48
49
50
51
# File 'brew/Library/Homebrew/livecheck/strategy/page_match.rb', line 47

def self.page_matches(url, regex)
  page = URI.open(url).read
  matches = page.scan(regex)
  matches.map(&:first).uniq
end