Skip to content
Snippets Groups Projects
Unverified Commit 7555556b authored by Markus Reiter's avatar Markus Reiter Committed by Sam Ford
Browse files

Support block in `PageMatch` strategy.

parent 1857e0eb
No related branches found
No related tags found
No related merge requests found
......@@ -46,21 +46,22 @@ module Homebrew
# @param regex [Regexp] a regex used for matching versions in the
# content
# @return [Array]
def self.page_matches(url, regex)
def self.page_matches(url, regex, &block)
page = Strategy.page_contents(url)
matches = page.scan(regex)
regex_names = regex.names.map(&:to_sym)
if regex_names.count > 1
matches.map do |match|
match_data = regex_names.zip(match).to_h
regex_names.sort.map { |name| match_data[name] }.join(",")
end.uniq
else
matches.map(&:first).uniq
if block
data = { page: page }
case (value = block.call(data))
when String
return [value]
when Array
return value
else
raise TypeError, "Return value of `strategy :page_match` block must be a string or array."
end
end
page.scan(regex).map(&:first).uniq
end
# Checks the content at the URL for new versions, using the provided
......@@ -69,10 +70,10 @@ module Homebrew
# @param url [String] the URL of the content to check
# @param regex [Regexp] a regex used for matching versions in content
# @return [Hash]
def self.find_versions(url, regex)
def self.find_versions(url, regex, &block)
match_data = { matches: {}, regex: regex, url: url }
page_matches(url, regex).each do |match|
page_matches(url, regex, &block).each do |match|
match_data[:matches][match] = Version.new(match)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment