Class: SvgInlineFileExtractor::InlineImage
- Inherits:
-
Object
- Object
- SvgInlineFileExtractor::InlineImage
- 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
-
#binary_image ⇒ String
Base64 decoded binary image from href.
-
#declared_image_type ⇒ String
The image type according to the DATA_IMAGE_HEADER_PATTERN.
-
#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
-
#svg_href_contents=(value) ⇒ Object
Updates the contents of the href that this inline image came from.
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.
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_type ⇒ String
Returns The image type according to the DATA_IMAGE_HEADER_PATTERN.
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_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
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
53 54 55 |
# File 'lib/svg_inline_file_extractor/inline_image.rb', line 53 def svg_href_contents=(value) nokogiri_element.value = value end |