Class: SvgInlineFileExtractor::InlineImage
- Inherits:
-
Object
- Object
- SvgInlineFileExtractor::InlineImage
- Defined in:
- lib/svg_inline_file_extractor/inline_image.rb
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
-
#binary_image ⇒ String
Base64 decoded binary image from href.
-
#declared_image_type ⇒ String|Nil
The image type according to the DATA_IMAGE_HEADER_PATTERN, nil if pattern not matched.
-
#element_class ⇒ String | nil
The value of the class attribute in the parent element.
-
#element_id ⇒ String | nil
The value of the id attribute in the parent element.
-
#get_attribute(attribute) ⇒ String | Nil
The value in the specified attribute, or nil if not set.
-
#initialize(nokogiri_element) ⇒ InlineImage
constructor
A new instance of InlineImage.
-
#inline_image? ⇒ Boolean
True if declared_image_type is not nil.
-
#set_binary_image_from_uri! ⇒ Boolean
If the image href is set to a URI, attempt to download/open and convert to an base64 encoded inline image.
Constructor Details
#initialize(nokogiri_element) ⇒ InlineImage
Returns a new instance of InlineImage.
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_contents ⇒ Object
12 13 14 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12 def href_contents @href_contents end |
#nokogiri_element ⇒ Object
12 13 14 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 12 def nokogiri_element @nokogiri_element end |
Instance Method Details
#binary_image ⇒ String
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.
61 62 63 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 61 def binary_image @binary_image ||= Base64.decode64(without_header) end |
#declared_image_type ⇒ String|Nil
Returns The image type according to the DATA_IMAGE_HEADER_PATTERN, nil if pattern not matched.
21 22 23 24 25 26 27 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 21 def declared_image_type @declared_image_type ||= begin if (match = href_contents.match(DATA_IMAGE_HEADER_PATTERN)) match[:type] end end end |
#element_class ⇒ String | nil
Returns 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_id ⇒ String | nil
Returns 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.
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 true if declared_image_type is not nil.
30 31 32 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 30 def inline_image? !!declared_image_type end |
#set_binary_image_from_uri! ⇒ Boolean
If the image href is set to a URI, attempt to download/open and convert to an
base64 encoded inline image
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 69 def set_binary_image_from_uri! unless SvgInlineFileExtractor.use_mini_magick? raise MiniMagickMissing, '#set_binary_image_from_uri! requires the MiniMagick gem to be installed.' end SvgInlineFileExtractor.with_temp_image(nokogiri_element.value) do |temp_image| format = SvgInlineFileExtractor.identify_image(temp_image).to_s.downcase nokogiri_element.value = "data:image/#{format};base64,#{encode(temp_image)}" nokogiri_element.name = 'xlink:href' end true end |