Sha256: 08e0b2a58083e30fc9425e09d61939f38af79b4a21dedf3ba49ebcb1348c0c99

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

require "open-uri"
module SlideHero
  class RemoteImage
    attr_reader :location, :alt_text, :width, :height, :image_class, :as, :destination
    def initialize(location, alt_text="", image_class:Image, width:nil, height:nil, destination: 'images', as:nil)
      @location = location
      @alt_text = alt_text
      @width = width
      @height = height
      @image_class = image_class
      @destination = destination
      @as = as
    end

    def compile
      fetch_remote_file unless file_cached?
      image_class.new(filename, alt_text, width: width, height:height).compile
    end

    private

    def file_cached?
      File.exist?("#{destination}/#{filename}")
    end

    def fetch_remote_file
      open(location) do |f|
        File.open("#{destination}/#{filename}","wb") do |file|
          file.puts f.read
        end
      end
    end

    def filename
      local_filename || remote_filename
    end

    def local_filename
      return nil unless as
      file_extention = URI.parse(location).path.split('.').last
      "#{as}.#{file_extention}"
    end

    def remote_filename
      URI.parse(location).path.split('/').last
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slide_hero-0.0.11 lib/slide_hero/remote_image.rb
slide_hero-0.0.10 lib/slide_hero/remote_image.rb
slide_hero-0.0.9 lib/slide_hero/remote_image.rb
slide_hero-0.0.8 lib/slide_hero/remote_image.rb
slide_hero-0.0.7 lib/slide_hero/remote_image.rb
slide_hero-0.0.6 lib/slide_hero/remote_image.rb