Class: JustInform::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/just_inform/downloader.rb

Class Method Summary collapse

Class Method Details

.downloadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/just_inform/downloader.rb', line 20

def self.download
  Thread.new do
    thread = Thread.current
    body = thread[:body] = []

    file = File.open(File.join('/tmp',self.current_filename), 'w+b')

    Net::HTTP.new('www.reginfo.gov').request_get('/public/do/PRAXML?type=inventory') do |response|
      length = thread[:length] = response['Content-Length'].to_i
      file_size_in_mb = response['Content-Length'].to_i/1024/1024
      puts "Downloading ~#{file_size_in_mb} MB of XML data and parsing with Nokogiri. This may take a few minutes depending on your processor and internet connection"
      
      response.read_body do |fragment|
        file.write(fragment)
        thread[:done] = (thread[:done] || 0) + fragment.length
        thread[:progress] = thread[:done].quo(length) * 100
      end
      file.close
    end
  end
end

.get_latestObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/just_inform/downloader.rb', line 7

def self.get_latest
  if self.current_file?
    puts "Using downloaded file at #{File.join('/tmp', self.current_filename)} and parsing with Nokogiri"

    latest = File.open(File.join('/tmp', self.current_filename), 'r')
  else
    thread = download

    print "%.2f%% Complete\r" % thread[:progress].to_f until thread.join 1
    latest = File.open(File.join('/tmp', self.current_filename), 'r')
  end
end