Class: ULS::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/uls/database.rb

Overview

Used to interact with the FCC ‘database’ files. These files are compressed files containing several individual pipe-delimited data files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Database

Use to construct an interface to the Database zipfile/directory retrieved from the FCC. The filename may either be a zip file, in which case it will be extracted to the temporary directory, or may be an already extract directory containing the various ‘database’ files.



14
15
16
17
18
19
20
21
# File 'lib/uls/database.rb', line 14

def initialize(filename)
  path = Pathname.new(filename)
  if path.directory?
    @extracted_path = filename
  else
    @compressed_file = filename
  end
end

Instance Attribute Details

#compressed_fileObject

Returns the value of attribute compressed_file.



7
8
9
# File 'lib/uls/database.rb', line 7

def compressed_file
  @compressed_file
end

Instance Method Details

#cleanObject



58
59
60
# File 'lib/uls/database.rb', line 58

def clean
  File.rmdir(extracted_path)
end

#deleteObject



62
63
64
# File 'lib/uls/database.rb', line 62

def delete
  File.delete(compressed_file)
end

#delete!Object



66
67
68
69
70
# File 'lib/uls/database.rb', line 66

def delete!
  clean if extracted?

  delete
end

#extractObject



49
50
51
52
53
54
55
56
# File 'lib/uls/database.rb', line 49

def extract
  FileUtils.mkdir_p(extracted_path)
  Zip::File.open(compressed_file) do |opened_file|
    opened_file.each do |entry|
      entry.extract("#{extracted_path}/#{entry.name}")
    end
  end
end

#extracted?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/uls/database.rb', line 45

def extracted?
  Dir.exist?(extracted_path)
end

#extracted_pathObject



41
42
43
# File 'lib/uls/database.rb', line 41

def extracted_path
  @extracted_path ||= "#{ULS.configuration.tmpdir}/uls_#{Time.now.to_i}"
end

#form_primaryObject



29
30
31
32
33
# File 'lib/uls/database.rb', line 29

def form_primary
  extract unless extracted?

  DatFile.new("#{extracted_path}/HD.dat")
end

#form_secondaryObject



35
36
37
38
39
# File 'lib/uls/database.rb', line 35

def form_secondary
  extract unless extracted?

  DatFile.new("#{extracted_path}/AD.dat")
end

#name_and_addressesObject



23
24
25
26
27
# File 'lib/uls/database.rb', line 23

def name_and_addresses
  extract unless extracted?

  DatFile.new("#{extracted_path}/EN.dat")
end