class OfacSdnLoader

Public Class Methods

load_current_sdn_file() { |"Downloading OFAC data from http://treas.gov/offices/enforcement/ofac/sdn"| ... } click to toggle source

Loads the most recent file from www.treas.gov/offices/enforcement/ofac/sdn/delimit/index.shtml

# File lib/ofac/models/ofac_sdn_loader.rb, line 18
def self.load_current_sdn_file
  puts "Reloading OFAC sdn data"
  puts "Downloading OFAC data from http://www.treas.gov/offices/enforcement/ofac/sdn"
  yield "Downloading OFAC data from http://www.treas.gov/offices/enforcement/ofac/sdn" if block_given?
  #get the 3 data files
  sdn = Tempfile.new('sdn')
  uri = URI.parse('http://www.treasury.gov/ofac/downloads/sdn.pip')

  proxy_addr, proxy_port = ENV['http_proxy'].gsub("http://", "").split(/:/) if ENV['http_proxy']

  bytes = sdn.write(Net::HTTP::Proxy(proxy_addr, proxy_port).get(uri))
  sdn.rewind
  if bytes == 0 || convert_line_to_array(sdn.readline).size != 12
    puts "Trouble downloading file.  The url may have changed."
    yield "Trouble downloading file.  The url may have changed." if block_given?
    return
  else
    sdn.rewind
  end
  address = Tempfile.new('sdn')
  address.write(Net::HTTP::Proxy(proxy_addr, proxy_port).get(URI.parse('http://www.treasury.gov/ofac/downloads/add.pip')))
  address.rewind
  alt = Tempfile.new('sdn')
  alt.write(Net::HTTP::Proxy(proxy_addr, proxy_port).get(URI.parse('http://www.treasury.gov/ofac/downloads/alt.pip')))
  alt.rewind

  if OfacSdn.connection.kind_of?(ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter) || OfacSdn.connection.kind_of?(ActiveRecord::ConnectionAdapters::JdbcAdapter)
    puts "Converting file to csv format for Mysql import.  This could take several minutes."
    yield "Converting file to csv format for Mysql import.  This could take several minutes." if block_given?

    csv_file = convert_to_flattened_csv(sdn, address, alt) do |status|
      yield status if block_given?
    end

    bulk_mysql_update(csv_file) do |status|
      yield status if block_given?
    end
  else
    active_record_file_load(sdn, address, alt) do |status|
      yield status if block_given?
    end
  end

  sdn.close
  @address.close
  @alt.close
end