Module: ImportSupport

Included in:
IndexManager
Defined in:
lib/import_support.rb

Instance Method Summary collapse

Instance Method Details

#appendCategories(file_items, dataset_name, categories, dataspec, doc_class) ⇒ Object

Append categories to file items and create


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/import_support.rb', line 32

def appendCategories(file_items, dataset_name, categories, dataspec, doc_class)
  count = 0

  # Loop through all items if not nill or empty
  if file_items != nil && !file_items.empty?
    file_items.each do |i|
      i.merge!(dataset_name: dataset_name, categories: categories)
      i.merge!(data_source: getDatasource(dataspec, i)) if @importer.first[1].size >1
      createItem(processItem(i, dataspec), dataset_name.gsub(" ", "")+count.to_s, dataspec, doc_class)
      count += 1
    end
  end
end

#getDatasetName(file) ⇒ Object

Get dataset name (file name) - for import from dir


14
15
16
# File 'lib/import_support.rb', line 14

def getDatasetName(file)
  return file.split("/").last.gsub("_", " ").gsub(".json", "")
end

#getDatasource(dataspec, i) ⇒ Object

Append dataspec if importer greater than two


47
48
49
# File 'lib/import_support.rb', line 47

def getDatasource(dataspec, i)
  return dataspec.index_name.gsub("_", " ").capitalize
end

#getNameCategories(file, dataset_name, dataspec) ⇒ Object

Get categories (dir name) - for import from dir


19
20
21
22
23
# File 'lib/import_support.rb', line 19

def getNameCategories(file, dataset_name, dataspec)
  categories = file.gsub(dataspec.data_path, "").gsub(file.split("/").last, "").split("/").reject(&:empty?)
  categories.push(dataset_name)
  return categories
end

#importFileInDir(file, dataspec, doc_class) ⇒ Object

Handles the processing of each item in a file in a directory import


52
53
54
55
56
57
58
59
# File 'lib/import_support.rb', line 52

def importFileInDir(file, dataspec, doc_class)
  # Get dataset name and categories
  dataset_name = getDatasetName(file)
  categories = getNameCategories(file, dataset_name, dataspec)

  # Append categories to items and create
  appendCategories(openFile(file), dataset_name, categories, dataspec, doc_class)
end

#importFromFile(dataspec) ⇒ Object

Gets data from file and imports it


8
9
10
# File 'lib/import_support.rb', line 8

def importFromFile(dataspec)
  createFromFile(JSON.parse(File.read(dataspec.data_path), symbolize_names: true), dataspec)
end

#importFromURL(datspec) ⇒ Object

Gets data from a link and imports it


3
4
5
# File 'lib/import_support.rb', line 3

def importFromURL(datspec)
  createFromFile(JSON.parse(URI.parse(dataspec.data_path).read, symbolize_names: true), dataspec)
end

#openFile(file) ⇒ Object

Open file for dir import (if not null)


26
27
28
29
# File 'lib/import_support.rb', line 26

def openFile(file)
  file_text = File.read(file)
  return JSON.parse(file_text) if file_text != "null"
end