Class: Xenon::ETag
- Inherits:
-
Object
- Object
- Xenon::ETag
- Defined in:
- lib/xenon/etag.rb
Overview
An Etag, see RFC 7232 § 2.3.
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Class Method Summary collapse
-
.parse(s) ⇒ ETag
Parses an ETag string.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
An equality function that checks the ETags have the same strength and tag.
-
#===(other) ⇒ true, false
(also: #=~)
A case equality function that uses #strong_eq? or #weak_eq? depending on whether the receiving tag is strong or weak, respectively.
-
#freeze ⇒ ETag
Prevents further modifications to the ETag.
-
#hash ⇒ Fixnum
Returns a hash code based on the ETag state.
-
#initialize(tag, weak: false) ⇒ ETag
constructor
Initializes a new ETag instance.
-
#strong? ⇒ true, false
Whether the ETag is strong.
-
#strong_eq?(other) ⇒ true, false
The strong equality function, see RFC 7232 § 2.3.2.
-
#to_s ⇒ String
Returns a string representation of the ETag.
-
#weak? ⇒ true, false
Whether the ETag is weak.
-
#weak_eq?(other) ⇒ true, false
The weak equality function, see RFC 7232 § 2.3.2.
Constructor Details
#initialize(tag, weak: false) ⇒ ETag
Initializes a new ETag instance.
11 12 13 14 15 |
# File 'lib/xenon/etag.rb', line 11 def initialize(tag, weak: false) @tag = tag @weak = weak freeze end |
Instance Attribute Details
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
6 7 8 |
# File 'lib/xenon/etag.rb', line 6 def tag @tag end |
Class Method Details
.parse(s) ⇒ ETag
Parses an ETag string.
27 28 29 30 31 32 |
# File 'lib/xenon/etag.rb', line 27 def self.parse(s) tree = Parsers::ETag.new.etag.parse(s) Parsers::ETagHeaderTransform.new.apply(tree) rescue Parslet::ParseFailed raise Xenon::ParseError.new("Invalid ETag (#{s}).") end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
An equality function that checks the ETags have the same strength and tag.
60 61 62 |
# File 'lib/xenon/etag.rb', line 60 def ==(other) strong? == other.strong? && @tag == other.tag end |
#===(other) ⇒ true, false Also known as: =~
A case equality function that uses #strong_eq? or #weak_eq? depending on whether the receiving tag is strong or weak, respectively.
68 69 70 |
# File 'lib/xenon/etag.rb', line 68 def ===(other) strong? ? strong_eq?(other) : weak_eq?(other) end |
#freeze ⇒ ETag
Prevents further modifications to the ETag.
19 20 21 22 |
# File 'lib/xenon/etag.rb', line 19 def freeze @tag.freeze super end |
#hash ⇒ Fixnum
Returns a hash code based on the ETag state.
75 76 77 |
# File 'lib/xenon/etag.rb', line 75 def hash to_s.hash end |
#strong? ⇒ true, false
Whether the ETag is strong.
42 43 44 |
# File 'lib/xenon/etag.rb', line 42 def strong? !weak? end |
#strong_eq?(other) ⇒ true, false
The strong equality function, see RFC 7232 § 2.3.2.
48 49 50 |
# File 'lib/xenon/etag.rb', line 48 def strong_eq?(other) strong? && other.strong? && @tag == other.tag end |
#to_s ⇒ String
Returns a string representation of the ETag.
81 82 83 |
# File 'lib/xenon/etag.rb', line 81 def to_s strong? ? %("#{@tag}") : %(W/"#{@tag}") end |
#weak? ⇒ true, false
Whether the ETag is weak.
36 37 38 |
# File 'lib/xenon/etag.rb', line 36 def weak? @weak end |
#weak_eq?(other) ⇒ true, false
The weak equality function, see RFC 7232 § 2.3.2.
54 55 56 |
# File 'lib/xenon/etag.rb', line 54 def weak_eq?(other) @tag == other.tag end |