Module: Philiprehberger::Etag::Matcher

Defined in:
lib/philiprehberger/etag/matcher.rb

Overview

Evaluates If-None-Match and If-Match headers against ETags.

Class Method Summary collapse

Class Method Details

.match?(etag, header) ⇒ Boolean

Performs a weak comparison of an ETag against an If-None-Match header value. Weak comparison ignores the W/ prefix when comparing.

Parameters:

  • etag (String)

    the ETag to compare

  • header (String)

    the If-None-Match header value (may contain multiple ETags)

Returns:

  • (Boolean)

    true if the ETag matches any value in the header



13
14
15
16
17
18
19
# File 'lib/philiprehberger/etag/matcher.rb', line 13

def self.match?(etag, header)
  return false if header.nil? || header.empty?
  return true if header.strip == '*'

  normalized = strip_weak(etag)
  parse_etags(header).any? { |candidate| strip_weak(candidate) == normalized }
end

.modified?(etag, request_headers) ⇒ Boolean

Determines whether a resource has been modified based on request headers. Checks the If-None-Match header using weak comparison.

Parameters:

  • etag (String)

    the current ETag of the resource

  • request_headers (Hash)

    a hash of request headers

Returns:

  • (Boolean)

    true if the resource has been modified (ETag does not match)



42
43
44
45
46
47
# File 'lib/philiprehberger/etag/matcher.rb', line 42

def self.modified?(etag, request_headers)
  if_none_match = request_headers['HTTP_IF_NONE_MATCH'] || request_headers['If-None-Match']
  return true if if_none_match.nil? || if_none_match.empty?

  !match?(etag, if_none_match)
end

.strong_match?(etag, header) ⇒ Boolean

Performs a strong comparison of an ETag against an If-Match header value. Strong comparison requires exact match including the W/ prefix. Weak ETags never match in strong comparison.

Parameters:

  • etag (String)

    the ETag to compare

  • header (String)

    the If-Match header value (may contain multiple ETags)

Returns:

  • (Boolean)

    true if the ETag strongly matches any value in the header



28
29
30
31
32
33
34
# File 'lib/philiprehberger/etag/matcher.rb', line 28

def self.strong_match?(etag, header)
  return false if header.nil? || header.empty?
  return false if weak?(etag)
  return true if header.strip == '*'

  parse_etags(header).any? { |candidate| !weak?(candidate) && candidate == etag }
end