class VitalsImage::Analyzer::UrlAnalyzer

Public Class Methods

accept?(source) click to toggle source
# File lib/vitals_image/analyzer/url_analyzer.rb, line 7
def self.accept?(source)
  source.is_a?(Source)
end

Public Instance Methods

analyze() click to toggle source
# File lib/vitals_image/analyzer/url_analyzer.rb, line 11
def analyze
  file = download
  image = open(file)
  mime  = Marcel::MimeType.for(Pathname.new file.path)

  source.update width: image.width, height: image.height, analyzed: true, content_type: mime
end

Private Instance Methods

download() click to toggle source
# File lib/vitals_image/analyzer/url_analyzer.rb, line 32
def download
  uri = URI.parse(source.key)
  io = uri.open(ssl_verify_mode: ssl_verify_mode)
  downloaded = Tempfile.new([File.basename(uri.path), File.extname(uri.path)])

  if io.is_a?(Tempfile)
    FileUtils.mv io.path, downloaded.path
  else
    # StringIO
    File.write(downloaded.path, io.string)
  end

  downloaded
rescue
  logger.error "Failed to download #{source.key}"
  raise
end
open(file) click to toggle source
# File lib/vitals_image/analyzer/url_analyzer.rb, line 20
def open(file)
  if VitalsImage.image_library == :mini_magick
    MiniMagick::Image.new(file.path).tap do |image|
      raise "Invalid image" unless image.valid?
    end
  else
    Vips::Image.new_from_file(file.path, access: :sequential).tap do |image|
      image.avg
    end
  end
end
ssl_verify_mode() click to toggle source
# File lib/vitals_image/analyzer/url_analyzer.rb, line 50
def ssl_verify_mode
  VitalsImage.skip_ssl_verification ? OpenSSL::SSL::VERIFY_NONE : nil
end