class Xenon::ETag

An Etag, see {tools.ietf.org/html/rfc7232#section-2.3 RFC 7232 § 2.3}.

Attributes

tag[R]

Public Class Methods

new(tag, weak: false) click to toggle source

Initializes a new ETag instance. @param tag [String] The opaque tag. @param weak [true, false] Whether the tag is weak.

# File lib/xenon/etag.rb, line 11
def initialize(tag, weak: false)
  @tag = tag
  @weak = weak
  freeze
end
parse(s) click to toggle source

Parses an ETag string. @param s [String] The ETag string. @return [ETag] An `ETag` object.

# 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

Public Instance Methods

==(other) click to toggle source

An equality function that checks the ETags have the same strength and tag. @return [true, false] `true` if the ETags have the same strength and tag; otherwise `false`.

# File lib/xenon/etag.rb, line 60
def ==(other)
  strong? == other.strong? && @tag == other.tag
end
Also aliased as: eql?
===(other) click to toggle source

A case equality function that uses {strong_eq?} or {weak_eq?} depending on whether the receiving tag is strong or weak, respectively. @return [true, false] `true` if the other ETag matches; otherwise `false`.

# File lib/xenon/etag.rb, line 68
def ===(other)
  strong? ? strong_eq?(other) : weak_eq?(other)
end
Also aliased as: =~
=~(other)
Alias for: ===
eql?(other)
Alias for: ==
freeze() click to toggle source

Prevents further modifications to the ETag. @return [ETag] This method returns self.

Calls superclass method
# File lib/xenon/etag.rb, line 19
def freeze
  @tag.freeze
  super
end
hash() click to toggle source

Returns a hash code based on the ETag state. @return [Fixnum] The ETag hash.

# File lib/xenon/etag.rb, line 75
def hash
  to_s.hash
end
strong?() click to toggle source

Whether the ETag is strong. @return [true, false] `true` if the ETag is strong; otherwise `false`.

# File lib/xenon/etag.rb, line 42
def strong?
  !weak?
end
strong_eq?(other) click to toggle source

The strong equality function, see {tools.ietf.org/html/rfc7232#section-2.3.2 RFC 7232 § 2.3.2}. @return [true, false] `true` if the ETags are both strong and have the same tag; otherwise `false`.

# File lib/xenon/etag.rb, line 48
def strong_eq?(other)
  strong? && other.strong? && @tag == other.tag
end
to_s() click to toggle source

Returns a string representation of the ETag. @return [String] The ETag string.

# File lib/xenon/etag.rb, line 81
def to_s
  strong? ? %("#{@tag}") : %(W/"#{@tag}")
end
weak?() click to toggle source

Whether the ETag is weak. @return [true, false] `true` if the ETag is weak; otherwise `false`.

# File lib/xenon/etag.rb, line 36
def weak?
  @weak
end
weak_eq?(other) click to toggle source

The weak equality function, see {tools.ietf.org/html/rfc7232#section-2.3.2 RFC 7232 § 2.3.2}. @return [true, false] `true` if the ETags have the same tag; otherwise `false`.

# File lib/xenon/etag.rb, line 54
def weak_eq?(other)
  @tag == other.tag
end