Module: GeoLabels::Importer

Defined in:
lib/geo_labels/importer.rb,
lib/geo_labels/importer/csv_data.rb,
lib/geo_labels/importer/yaml_data.rb

Defined Under Namespace

Classes: CsvData, YamlData

Class Method Summary collapse

Class Method Details

.get_type_and_data(subject) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/geo_labels/importer.rb', line 17

def self.get_type_and_data(subject)
  case subject
  when String
    if subject.start_with?('/') or subject.start_with?('./') # is a path spec
      return recognize_path(subject), File.read(subject)
    else
      if subject.start_with? "---\n" or subject.start_with?("labels: |\n")
        return [:yml, subject]
      elsif subject.start_with?('name,labels,')
        return [:csv, subject]
      else
        raise 'The import string could not be recognized as either YAML or CSV'
      end
    end
  when File
    return recognize_path(subject.path), subject.read
  when Pathname
    return recognize_path(subject.to_s), subject.read
  when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
    return recognize_path(subject.original_filename), subject.read
  else
    raise 'Format of import not recognized'
  end
end

.import(subject) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/geo_labels/importer.rb', line 5

def self.import(subject)
  type, data = get_type_and_data(subject)

  if type == :yml
    GeoLabels::Importer::YamlData.import(YAML.safe_load(data))
  elsif type == :csv
    GeoLabels::Importer::CsvData.new.import(data)
  else
    raise 'Argument not recognized'
  end
end

.recognize_path(path) ⇒ Object



42
43
44
45
46
47
# File 'lib/geo_labels/importer.rb', line 42

def self.recognize_path(path)
  return :yml if path.end_with?('.yml')
  return :csv if path.end_with?('.csv')

  raise 'Imported path is neither YAML or CSV file'
end