Sha256: 79d357629500ec4ee962a3b8ea7a925bf5f947e1d6caeb8bb824acda53cad93a

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

require_relative './csv_methods'
require "open-uri"

module EasySeeds
  class Images

    def self.attach_images(class_image_names)
      begin
        Dir.chdir('../seed_image_files')
      rescue
        Dir.chdir(Dir.pwd + '/db/seed_image_files')
      end

      Dir.glob("*").each_with_index do |seed_file, i|
        headers, data = CSVLoader.unpack_csvs(seed_file)
        class_image_name = class_image_names[i]
        puts "Attaching to #{class_image_name}..."

        data.each_with_index do |row|
          object_id, url, filename = row
          class_instance = class_image_name.find_by_id(object_id)

          begin
            class_instance.image.attach(io: URI.open(url), filename: filename)
            puts "Attached to #{filename}"
          rescue OpenURI::HTTPError
            puts('Waiting 30 seconds before seeding the next row of data, please be patient')
            sleep(30.second)
            class_instance.image.attach(io: URI.open(url), filename: filename)
            puts "Attached to #{filename}"
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
easy_seeds-1.0.6 lib/easy_seeds/images.rb
easy_seeds-1.0.5 lib/easy_seeds/images.rb
easy_seeds-1.0.4 lib/easy_seeds/images.rb
easy_seeds-1.0.3 lib/easy_seeds/images.rb
easy_seeds-1.0.2 lib/easy_seeds/images.rb
easy_seeds-1.0.0 lib/easy_seeds/images.rb