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

Inherits:
Object
  • Object
show all
Extended by:
Homebrew::Livecheck::Strategic
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

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

Methods included from Homebrew::Livecheck::Strategic

find_versions, match?

Class Method Details

.find_versions(url:, regex: nil, content: nil, options: Options.new, &block) ⇒ Hash{Symbol => T.anything}

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 for matching versions

  • content (String, nil) (defaults to: nil)

    content to check instead of fetching

  • options (Options) (defaults to: Options.new)

    options to modify behavior

  • block (Proc, nil)

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'livecheck/strategy/header_match.rb', line 97

def self.find_versions(url:, regex: nil, content: nil, options: Options.new, &block)
  match_data = { matches: {}, regex:, url: }
  match_data[:cached] = true if content
  return match_data if url.blank?

  content = if content
    Json.parse_json(content)
  else
    match_data[:content] = Strategy.page_headers(url, options:)
  end
  return match_data if content.blank?

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

  require "json"
  match_data[:content] = JSON.generate(match_data[:content]) unless match_data[:cached]
  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)


32
33
34
# File 'livecheck/strategy/header_match.rb', line 32

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

.versions_from_content(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 (Array<Hash{String => String}>)

    an array of response HTTP header hashes to check for versions

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

    a regex for matching versions in content

  • block (Proc, nil)

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'livecheck/strategy/header_match.rb', line 49

def self.versions_from_content(headers, regex = nil, &block)
  # Merge the last value of each header from all responses into one hash
  # for convenience
  merged_headers = T.cast(headers.reduce(&:merge), T::Hash[String, String])

  if block
    block_return_value = case block.parameters[0]
    when [:opt, :headers], [:req, :headers], [:rest], [:req]
      regex.present? ? yield(merged_headers, regex) : yield(merged_headers)
    when [:opt, :all_headers], [:req, :all_headers]
      regex.present? ? yield(headers, regex) : yield(headers)
    else
      raise ArgumentError,
            "First argument of #{Utils.demodulize(name)} `strategy` block must be `headers` or `all_headers`"
    end
    return Strategy.handle_block_return(block_return_value)
  end

  DEFAULT_HEADERS_TO_CHECK.filter_map do |header_name|
    header_value = merged_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