Class: Darjeelink::ShortLinkImporter

Inherits:
Object
  • Object
show all
Defined in:
app/importers/darjeelink/short_link_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_to_csv) ⇒ ShortLinkImporter

Returns a new instance of ShortLinkImporter.



5
6
7
8
# File 'app/importers/darjeelink/short_link_importer.rb', line 5

def initialize(path_to_csv)
  @path_to_csv = path_to_csv
  @duplicates = []
end

Instance Method Details

#importObject



10
11
12
13
14
15
16
17
18
# File 'app/importers/darjeelink/short_link_importer.rb', line 10

def import
  CSV.foreach(path_to_csv, headers: true).each do |row|
    url, auto_generated_key, custom_key = row.fields
    create_short_link(url, auto_generated_key, custom_key)
  end

  duplicate_string = duplicates.join("\n")
  Rails.logger.warn("Duplicates:\n#{duplicate_string}") unless duplicates.empty?
end