File: etag.rb

package info (click to toggle)
ruby-protocol-http 0.59.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ruby: 7,612; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2025, by Samuel Williams.

module Protocol
	module HTTP
		module Header
			# The `etag` header represents the entity tag for a resource.
			#
			# The `etag` header provides a unique identifier for a specific version of a resource, typically used for cache validation or conditional requests. It can be either a strong or weak validator as defined in RFC 9110.
			class ETag < String
				# Parses a raw header value.
				#
				# @parameter value [String] a raw header value.
				# @returns [ETag] a new instance.
				def self.parse(value)
					self.new(value)
				end
				
				# Coerces a value into a parsed header object.
				#
				# @parameter value [String] the value to coerce.
				# @returns [ETag] a parsed header object.
				def self.coerce(value)
					self.new(value.to_s)
				end
				
				# Replaces the current value of the `etag` header.
				#
				# @parameter value [String] a raw header value for the `etag` header.
				def << value
					replace(value)
				end
				
				# Checks whether the `etag` is a weak validator.
				#
				# Weak validators indicate semantically equivalent content but may not be byte-for-byte identical.
				#
				# @returns [Boolean] whether the `etag` is weak.
				def weak?
					self.start_with?("W/")
				end
				
				# Whether this header is acceptable in HTTP trailers.
				# ETag headers can safely appear in trailers as they provide cache validation metadata.
				# @returns [Boolean] `true`, as ETag headers are metadata that can be computed after response generation.
				def self.trailer?
					true
				end
			end
		end
	end
end