Sha256: d79d6eba868d96b6bac1d080a2f9a187c8b6d4557601838e22b89133d680f906

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

module LooksGood
  class Image

    attr_accessor :file_name, :path, :image

    attr_reader :type

    def initialize image, file_name
      @file_name = file_name
      @image = image
    end

    def save(type = :reference)
      save_path = path(type)
      FileUtils::mkdir_p(File.dirname(save_path)) unless File.exists?(save_path)
      @image.write save_path
      save_path
    end

    def exists?
      File.exists?(path)
    end

    def path(type = :reference)
      @path = File.join(LooksGood::Configuration.path(type), @file_name)
    end

  end

  class ImageFromElement < Image

    attr_accessor :image
    
    def initialize(element, file_name)
      super(image, file_name)
      @element = element
      @image = capture_image
    end

    def verify_and_save
      LooksGood::Configuration.max_no_tries.times do
        comparable = capture_image
        matches = LooksGood::Comparison.new(self,Image.new(comparable,@file_name)).matches?
        if matches
          self.save
          puts "Saved #{self.path} as reference"
          return()
        else
          @image = comparable  
        end  
      end
      raise 'Could not save a stable image. This could be due to animations or page load times. Saved a reference image, delete it to re-try'
    end

    private
    def capture_image
      LooksGood::CaptureElement.capture(@element)
    end

    #TODO: make save a relevant subclass method
  end

  class ImageFromFile < Image

    def initialize(file_name)
      super(image, file_name)

      @image = Magick::Image.read(path).first
    end

    #TODO: make save a relevant subclass method

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
looks_good-1.1.7 lib/looks_good/image.rb
looks_good-1.1.6 lib/looks_good/image.rb
looks_good-1.1.5 lib/looks_good/image.rb
looks_good-1.1.4 lib/looks_good/image.rb
looks_good-1.1.3 lib/looks_good/image.rb
looks_good-1.1.2 lib/looks_good/image.rb
looks_good-1.1.1 lib/looks_good/image.rb
looks_good-1.1.0 lib/looks_good/image.rb