Class: SvgInlineFileExtractor::InlineImage

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_inline_file_extractor/inline_image.rb

Defined Under Namespace

Classes: UnableToDetermineImageTypeError

Constant Summary collapse

DATA_IMAGE_HEADER_PATTERN =

Regex pattern to match against the beginning of the href string. the type, if found, will be the raw string, eg: jpeg, jpg, png, etc.

/data:image\/(?<type>.+);base64\,/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nokogiri_element) ⇒ InlineImage

Returns a new instance of InlineImage.

Parameters:

  • nokogiri_element (Nokogiri::XML::Attr)

    the href node from the SVG file



15
16
17
18
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 15

def initialize(nokogiri_element)
  self.nokogiri_element = nokogiri_element
  self.href_contents = nokogiri_element.value
end

Instance Attribute Details

#href_contentsObject



12
13
14
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12

def href_contents
  @href_contents
end

#nokogiri_elementObject



12
13
14
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12

def nokogiri_element
  @nokogiri_element
end

Instance Method Details

#binary_imageString

Note:

I’m not sure if memoization is the best here, let’s see what happens in the wild.

Returns Base64 decoded binary image from href.

Returns:

  • (String)

    Base64 decoded binary image from href



59
60
61
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 59

def binary_image
  @binary_image ||= Base64.decode64(without_header)
end

#declared_image_typeString

Returns The image type according to the DATA_IMAGE_HEADER_PATTERN.

Returns:

  • (String)

    The image type according to the DATA_IMAGE_HEADER_PATTERN

Raises:



22
23
24
25
26
27
28
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 22

def declared_image_type
  @declared_image_type ||= begin
    if (match = href_contents.match(DATA_IMAGE_HEADER_PATTERN))
      match[:type]
    end
  end
end

#element_classString | nil

Returns the value of the class attribute in the parent element.

Returns:

  • (String | nil)

    the value of the class attribute in the parent element



40
41
42
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 40

def element_class
  @element_class ||= get_attribute('class')
end

#element_idString | nil

Returns the value of the id attribute in the parent element.

Returns:

  • (String | nil)

    the value of the id attribute in the parent element



35
36
37
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 35

def element_id
  @element_id ||= get_attribute('id')
end

#get_attribute(attribute) ⇒ String | Nil

Returns the value in the specified attribute, or nil if not set.

Parameters:

  • attribute (String)

    an attribute to retreive from the parent element (the entity that this href came from)

Returns:

  • (String | Nil)

    the value in the specified attribute, or nil if not set



46
47
48
49
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 46

def get_attribute(attribute)
  attribute = nokogiri_element.parent.attribute(attribute)
  attribute.value if attribute
end

#inline_image?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 30

def inline_image?
  !!declared_image_type
end

#svg_href_contents=(value) ⇒ Object

Updates the contents of the href that this inline image came from

Parameters:

  • value (String)

    the value to set the href contents to



53
54
55
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 53

def svg_href_contents=(value)
  nokogiri_element.value = value
end