Class: FlakyTestTracker::Serializers::HTMLCommentSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/flaky_test_tracker/serializers/html_comment_serializer.rb

Overview

HTML comment serializer.

It encodes the value in base64 because an HTML comment content can't:

  • start with: ">" nor "->";
  • contain: "--".

Instance Method Summary collapse

Instance Method Details

#deserialize(value) ⇒ String

Returns the value to deserialized.

Parameters:

  • value (String)

    HTML comment representing the value serialized.

Returns:

  • (String)

    the value to deserialized.

Raises:

  • FlakyTestTracker::Error::DeserializeError



30
31
32
33
34
35
36
37
38
39
# File 'lib/flaky_test_tracker/serializers/html_comment_serializer.rb', line 30

def deserialize(value)
  parser = Oga::XML::Parser.new(value, html: true)
  document = parser.parse
  comment = document.xpath(".//comment()").first
  raise FlakyTestTracker::Error::DeserializeError unless comment

  Base64.strict_decode64(comment.text.strip)
rescue ArgumentError, LL::ParserError
  raise FlakyTestTracker::Error::DeserializeError
end

#serialize(value) ⇒ String

Returns the value to serialized as HTML comment.

Parameters:

  • value (String)

    the value to serialize.

Returns:

  • (String)

    the value to serialized as HTML comment.



18
19
20
21
22
23
24
25
# File 'lib/flaky_test_tracker/serializers/html_comment_serializer.rb', line 18

def serialize(value)
  # We need to base64 encode since comment:
  # - can't start with: `>` or `->`
  # - can't include: `--`
  content = Base64.strict_encode64(value)
  comment = Oga::XML::Comment.new(text: content)
  comment.to_xml
end