class Healthicons::Icon
Constants
- VARIANTS
Attributes
Public Class Methods
Generate a new instance of Healthicons
and render the SVG icon
@param icon [String] The icon image name @param options [Hash](optional) @return [String] the rendered icons SVG/XML contents
# File lib/healthicons/icon.rb, line 13 def self.call(icon, options = {}) new(icon, options).render end
Instantize an icons attributes
@param icon [String] The icon image name @param options [Hash](optional)
# File lib/healthicons/icon.rb, line 21 def initialize(icon, options = {}) @variant = variant_check(options[:variant]) @name = icon.to_s @options = options end
Public Instance Methods
Return the SVG icons contents
@return [String]
# File lib/healthicons/icon.rb, line 30 def render return '' if svg_file_contents.blank? Healthicons::Transform.call(svg_file_contents, @options) end
Private Instance Methods
Generate the icons file path
@return [String]
# File lib/healthicons/icon.rb, line 64 def file_path if solid? File.join(Healthicons::SOLID_ICON_PATH, safe_filename) else File.join(Healthicons::OUTLINE_ICON_PATH, safe_filename) end end
Used to determine if the icons variant is outline
@return [Boolean]
# File lib/healthicons/icon.rb, line 86 def outline? @variant == :outline end
Ensure that no filenames escaping the current director can be used In order to prevents someone from forcing the application to display contents of file in other folder, such as database.yml
@return [String]
# File lib/healthicons/icon.rb, line 52 def safe_filename @_safe_filename ||= if @name.blank? '' else # Removes filenames that start with `.svg`, `./`svg, '../svg', or '/svg' "#{@name&.gsub(%r{^[.\/]+/?}, '')}.svg" end end
Used to determine if the icons variant is solid
@return [Boolean]
# File lib/healthicons/icon.rb, line 93 def solid? @variant == :solid end
Returm the SVG file contents from the file
@return [String] SVG/XML file contents
# File lib/healthicons/icon.rb, line 75 def svg_file_contents @_svg_file_contents ||= if File.exist?(file_path) File.open(file_path).read.force_encoding('UTF-8') else raise Healthicons::Errors::UnknownIcon, "Icon '#{@name}' could not be found." end end
Verify a valid variant has been supplied
@param variant [String, Symbol] @return [String, Symbol]
# File lib/healthicons/icon.rb, line 42 def variant_check(variant = :outline) return variant.to_sym if variant && Healthicons::Icon::VARIANTS.include?(variant.to_sym) :outline end