Class: Xenon::ETag

Inherits:
Object
  • Object
show all
Defined in:
lib/xenon/etag.rb

Overview

An Etag, see RFC 7232 § 2.3.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, weak: false) ⇒ ETag

Initializes a new ETag instance.

Parameters:

  • tag (String)

    The opaque tag.

  • weak (true, false) (defaults to: false)

    Whether the tag is weak.



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

#tagObject (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.

Parameters:

  • s (String)

    The ETag string.

Returns:

  • (ETag)

    An ‘ETag` object.



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.

Returns:

  • (true, false)

    ‘true` if the ETags have the same strength and tag; otherwise `false`.



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.

Returns:

  • (true, false)

    ‘true` if the other ETag matches; otherwise `false`.



68
69
70
# File 'lib/xenon/etag.rb', line 68

def ===(other)
  strong? ? strong_eq?(other) : weak_eq?(other)
end

#freezeETag

Prevents further modifications to the ETag.

Returns:

  • (ETag)

    This method returns self.



19
20
21
22
# File 'lib/xenon/etag.rb', line 19

def freeze
  @tag.freeze
  super
end

#hashFixnum

Returns a hash code based on the ETag state.

Returns:

  • (Fixnum)

    The ETag hash.



75
76
77
# File 'lib/xenon/etag.rb', line 75

def hash
  to_s.hash
end

#strong?true, false

Whether the ETag is strong.

Returns:

  • (true, false)

    ‘true` if the ETag is strong; otherwise `false`.



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.

Returns:

  • (true, false)

    ‘true` if the ETags are both strong and have the same tag; otherwise `false`.



48
49
50
# File 'lib/xenon/etag.rb', line 48

def strong_eq?(other)
  strong? && other.strong? && @tag == other.tag
end

#to_sString

Returns a string representation of the ETag.

Returns:

  • (String)

    The ETag string.



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.

Returns:

  • (true, false)

    ‘true` if the ETag is weak; otherwise `false`.



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.

Returns:

  • (true, false)

    ‘true` if the ETags have the same tag; otherwise `false`.



54
55
56
# File 'lib/xenon/etag.rb', line 54

def weak_eq?(other)
  @tag == other.tag
end