Class: Jetel::Modules::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/jetel/modules/module.rb

Direct Known Subclasses

Alexa, Gadm, Geolite, Ip, Iso3166, Nga, Sfpd, Tiger, Wifileaks

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModule

Returns a new instance of Module.



67
68
69
# File 'lib/jetel/modules/module.rb', line 67

def initialize
  @downloader = Downloader.new
end

Instance Attribute Details

#downloaderObject (readonly)

Returns the value of attribute downloader.



33
34
35
# File 'lib/jetel/modules/module.rb', line 33

def downloader
  @downloader
end

Class Method Details

.download_dir(modul, source, opts) ⇒ Object



42
43
44
# File 'lib/jetel/modules/module.rb', line 42

def download_dir(modul, source, opts)
  Module.target_dir(modul, source, opts, 'downloaded')
end

.downloaded_file(modul, source, opts) ⇒ Object



54
55
56
# File 'lib/jetel/modules/module.rb', line 54

def downloaded_file(modul, source, opts)
  File.join(download_dir(modul, source, opts), source[:filename_downloaded] || source[:url].split('/').last)
end

.extract_dir(modul, source, opts) ⇒ Object



46
47
48
# File 'lib/jetel/modules/module.rb', line 46

def extract_dir(modul, source, opts)
  Module.target_dir(modul, source, opts, 'extracted')
end

.extracted_file(modul, source, opts) ⇒ Object



58
59
60
# File 'lib/jetel/modules/module.rb', line 58

def extracted_file(modul, source, opts)
  File.join(extract_dir(modul, source, opts), source[:filename_extracted] || source[:url].split('/').last)
end

.target_dir(modul, source, dir, *path) ⇒ Object



36
37
38
39
40
# File 'lib/jetel/modules/module.rb', line 36

def target_dir(modul, source, dir, *path)
  klass = modul.class.name.split('::').last
  source_name = Helper.sanitize(source[:name])
  File.join(dir.kind_of?(Hash) ? dir['download_dir'] : dir || Config[:DATA_DIRECTORY], klass, source[:flat] ? '' : source_name, path)
end

.transform_dir(modul, source, opts) ⇒ Object



50
51
52
# File 'lib/jetel/modules/module.rb', line 50

def transform_dir(modul, source, opts)
  Module.target_dir(modul, source, opts, 'transformed')
end

.transformed_file(modul, source, opts) ⇒ Object



62
63
64
# File 'lib/jetel/modules/module.rb', line 62

def transformed_file(modul, source, opts)
  File.join(transform_dir(modul, source, opts), source[:filename_transformed] || source[:url].split('/').last)
end

Instance Method Details

#download_dir(source, opts) ⇒ Object



83
84
85
# File 'lib/jetel/modules/module.rb', line 83

def download_dir(source, opts)
  Module.download_dir(self, source, opts)
end

#download_source(source, opts) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/jetel/modules/module.rb', line 71

def download_source(source, opts)
  downloaded_file = downloaded_file(source, opts)

  unless File.exists?(downloaded_file)
    downloader.download(source[:url], {:dir => download_dir(source, opts), :filename => source[:filename_downloaded]})
  end
end

#downloaded_file(source, opts) ⇒ Object



95
96
97
# File 'lib/jetel/modules/module.rb', line 95

def downloaded_file(source, opts)
  Module.downloaded_file(self, source, opts)
end

#extract_dir(source, opts) ⇒ Object



87
88
89
# File 'lib/jetel/modules/module.rb', line 87

def extract_dir(source, opts)
  Module.extract_dir(self, source, opts)
end

#extracted_file(source, opts) ⇒ Object



99
100
101
# File 'lib/jetel/modules/module.rb', line 99

def extracted_file(source, opts)
  Module.extracted_file(self, source, opts)
end

#load(global_options, options, args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jetel/modules/module.rb', line 107

def load(global_options, options, args)
  sources = self.class.sources
  if args.length > 0
    args = args.map(&:downcase)
    sources = sources.select do |source|
      args.index(source[:name].downcase)
    end
  end

  sources.map do |source|
    opts = global_options.merge(options)

    transformed_file = transformed_file(source, opts)

    loader = Helper.get_loader(opts['data_loader'])

    Dir.glob(transformed_file).each do |one_file|
      puts "Loading file #{one_file}"
      loader.load(self, source, one_file, opts)
    end
  end
end

#sources(global_options, options, _args) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jetel/modules/module.rb', line 130

def sources(global_options, options, _args)
  opts = global_options.merge(options)

  res = self.class.sources

  if opts['format'] == 'json'
    puts MultiJson.dump(res, :pretty => true)
  else
    pp res
  end

end

#target_dir(source, opts, *path) ⇒ Object



79
80
81
# File 'lib/jetel/modules/module.rb', line 79

def target_dir(source, opts, *path)
  Module.target_dir(self, source, opts['download_dir'], *path)
end

#transform_dir(source, opts) ⇒ Object



91
92
93
# File 'lib/jetel/modules/module.rb', line 91

def transform_dir(source, opts)
  Module.transform_dir(self, source, opts)
end

#transformed_file(source, opts) ⇒ Object



103
104
105
# File 'lib/jetel/modules/module.rb', line 103

def transformed_file(source, opts)
  Module.transformed_file(self, source, opts)
end

#unzip(source, options = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jetel/modules/module.rb', line 143

def unzip(source, options = {})
  downloaded_file = downloaded_file(source, options)
  dest_dir = extract_dir(source, options)

  FileUtils.mkdir_p(dest_dir)

  ::Zip::File.open(downloaded_file) do |zip_file|
    # Handle entries one by one
    zip_file.each do |entry|
      # Extract to file/directory/symlink
      puts "Extracting #{entry.name}"
      dest_file = File.join(dest_dir, entry.name.split('/').last)
      FileUtils.rm_rf(dest_file)
      entry.extract(dest_file)
    end
  end
end