Class: DeathMasterFileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/ssn_validator/models/death_master_file_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_url, file_as_of) ⇒ DeathMasterFileLoader

path_or_url is the full path to the file to load on disk, or the url of an update file. as_of is a string in the formatt YYYY-MM-DD for which the file data is accurate.



11
12
13
14
15
# File 'lib/ssn_validator/models/death_master_file_loader.rb', line 11

def initialize(path_or_url,file_as_of)
  @file_path_or_url = path_or_url
  @file_as_of = file_as_of
  valid?
end

Class Method Details

.load_update_files_from_webObject

Loads all the update files from dmf.ntis.gov. It starts with the last file loaded, and loads each missing file in sequence up to the current file.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ssn_validator/models/death_master_file_loader.rb', line 67

def self.load_update_files_from_web
  max_as_of = DeathMasterFile.maximum(:as_of)
  run_file_date = max_as_of.beginning_of_month.next_month
  last_file_date = Date.today.beginning_of_month
  while run_file_date <= last_file_date
    url = "https://dmf.ntis.gov/dmldata/monthly/MA#{run_file_date.strftime("%y%m%d")}"
    puts "Loading file #{url}"
    DeathMasterFileLoader.new(url,run_file_date.strftime("%Y-%m-%d")).load_file
    run_file_date += 1.month
  end
end

Instance Method Details

#get_file_from_webObject

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ssn_validator/models/death_master_file_loader.rb', line 46

def get_file_from_web
  uri = URI.parse(@file_path_or_url)

  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(SsnValidator::Ntis.user_name,SsnValidator::Ntis.password)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.port == 443)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http.request(request)

  raise(ArgumentError, "Invalid URL: #{@file_path_or_url}") if response.kind_of?(Net::HTTPNotFound)
  raise(ArgumentError, "Authorization Required: Invalid username or password.  Set the variables SsnValidator::Ntis.user_name and SsnValidator::Ntis.password in your environment.rb file.") if response.kind_of?(Net::HTTPUnauthorized)

  return response.body
end

#load_fileObject



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

def load_file

  if DeathMasterFile.connection.kind_of?(ActiveRecord::ConnectionAdapters::MysqlAdapter)
    puts "Converting file to csv format for Mysql import.  This could take several minutes."

    csv_file = convert_file_to_csv

    bulk_mysql_update(csv_file)
  else
    active_record_file_load
  end

end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ssn_validator/models/death_master_file_loader.rb', line 17

def valid?
  raise(ArgumentError, "path_or_url not specified") unless @file_path_or_url
  raise(ArgumentError, "as_of not specified") unless @file_as_of
  max_as_of = DeathMasterFile.maximum(:as_of)
  raise(ArgumentError, "A more recent file has already been processed.  DB as_of date #{max_as_of}") if  max_as_of && (max_as_of >= @file_as_of.to_date)

  if File.exists?(@file_path_or_url)
    @download_file = File.open(@file_path_or_url)
  elsif URI.parse(@file_path_or_url).kind_of?(URI::HTTP)
    @download_file = get_file_from_web
  else
    raise(Errno::ENOENT, @file_path_or_url)
  end
end