Class: Homebrew::Livecheck::Strategy::HeaderMatch Private

Inherits:
Object
  • Object
show all
Defined in:
livecheck/strategy/header_match.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

The HeaderMatch strategy follows all URL redirections and scans the resulting headers for matching text using the provided regex.

This strategy is not applied automatically and it's necessary to use strategy :header_match in a livecheck block to apply it.

Constant Summary collapse

NICE_NAME =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Header match"
PRIORITY =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

A priority of zero causes livecheck to skip the strategy. We do this for Homebrew::Livecheck::Strategy::HeaderMatch so we can selectively apply it when appropriate.

0
URL_MATCH_REGEX =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

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

%r{^https?://}i
DEFAULT_HEADERS_TO_CHECK =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

The header fields to check when a strategy block isn't provided.

T.let(["content-disposition", "location"].freeze, T::Array[String])

Class Method Summary collapse

Class Method Details

.find_versions(url:, regex: nil, homebrew_curl: false, **_unused, &block) ⇒ Hash{Symbol => T.untyped}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Checks the final URL for new versions after following all redirections, using the provided regex for matching.

Parameters:

  • url (String)

    the URL to fetch

  • regex (Regexp, nil) (defaults to: nil)

    a regex used for matching versions

  • homebrew_curl (Boolean) (defaults to: false)

    whether to use brewed curl with the URL

  • _unused (T.untyped)
  • block (Proc, nil)

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'livecheck/strategy/header_match.rb', line 81

def self.find_versions(url:, regex: nil, homebrew_curl: false, **_unused, &block)
  match_data = { matches: {}, regex:, url: }

  headers = Strategy.page_headers(url, homebrew_curl:)

  # Merge the headers from all responses into one hash
  merged_headers = headers.reduce(&:merge)
  return match_data if merged_headers.blank?

  versions_from_headers(merged_headers, regex, &block).each do |version_text|
    match_data[:matches][version_text] = Version.new(version_text)
  end

  match_data
end

.match?(url) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Whether the strategy can be applied to the provided URL.

Parameters:

  • url (String)

    the URL to match against

Returns:

  • (Boolean)


30
31
32
# File 'livecheck/strategy/header_match.rb', line 30

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

.versions_from_headers(headers, regex = nil, &block) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Identify versions from HTTP headers.

Parameters:

  • headers (Hash{String => String})

    a hash of HTTP headers to check for versions

  • regex (Regexp, nil) (defaults to: nil)

    a regex for matching versions

  • block (Proc, nil)

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'livecheck/strategy/header_match.rb', line 46

def self.versions_from_headers(headers, regex = nil, &block)
  if block
    block_return_value = regex.present? ? yield(headers, regex) : yield(headers)
    return Strategy.handle_block_return(block_return_value)
  end

  DEFAULT_HEADERS_TO_CHECK.filter_map do |header_name|
    header_value = headers[header_name]
    next if header_value.blank?

    if regex
      header_value[regex, 1]
    else
      v = Version.parse(header_value, detected_from_url: true)
      v.null? ? nil : v.to_s
    end
  end.uniq
end