Module: PicFisher::URLExtractor

Defined in:
lib/picfisher/url_extractor.rb

Overview

Extracts image urls from a string

Constant Summary collapse

URL_REGEX =

NOTE: using URI.regexp([“http”, “https”]) didn’t work for me

%r{https?://[^\s,]+}
IMAGE_EXTENSITONS =
%w[.jpg .jpeg .png .gif .webp .bmp .ico .svg .tiff or .tif .psd .raw .cr2 .nrw .arw .dng .nef
.orf .sr2 .raf .tif .tiff .djvu].freeze

Class Method Summary collapse

Class Method Details

.extract(string) ⇒ Array<String>

Extracts image urls from a string

Examples:

PicFisher::URLExtractor.extract("https://example.com/image_in_internet.png, https://example.com/another.png")

Parameters:

  • string (String)

Returns:

  • (Array<String>)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/picfisher/url_extractor.rb', line 18

def self.extract(string)
  result =
    string
    .scan(URL_REGEX)
    .select { |url| url.end_with?(*IMAGE_EXTENSITONS) }
    .uniq

  PicFisher::Log.debug("Extracted urls: #{result.join("|")}")

  result
end