Module: PicFisher::Sanitizer

Defined in:
lib/picfisher/sanitizer.rb

Overview

Sanitizes an image url

Class Method Summary collapse

Class Method Details

.sanitize_image_url(url) ⇒ String

Sanitizes an image url

Examples:

PicFisher::Sanitizer.sanitize_image_url("https://example.com/image_in_internet.png")

Parameters:

  • url (String)

Returns:

  • (String)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/picfisher/sanitizer.rb', line 11

def self.sanitize_image_url(url)
  filename_extension = File.extname(url)
  filename_extension_escaped = Regexp.escape(filename_extension) # to escape the dot "."
  result =
    url
    .sub(/#{filename_extension_escaped}$/, "")
    .gsub(/[^\w\s-]+/, "_")
    .gsub(/(^|\b\s)\s+($|\s?\b)/, "\\1\\2")
    .gsub(/\s+/, "_")

  result = "#{result}#{filename_extension}"
  PicFisher::Log.debug("Sanitizing url '#{url}': #{result}")
  result
end