Class: Nppes::UpdatePack::Pack

Inherits:
Object
  • Object
show all
Defined in:
lib/nppes/update_pack/pack.rb

Class Method Summary collapse

Class Method Details

.background_check_updates(continious = false) ⇒ Object



63
64
65
# File 'lib/nppes/update_pack/pack.rb', line 63

def background_check_updates(continious = false)
  Delayed::Job.enqueue(Nppes::Jobs::SearcherJob.new((Nppes.get_time_period if continious)))
end

.background_init_baseObject



37
38
39
# File 'lib/nppes/update_pack/pack.rb', line 37

def background_init_base
  Delayed::Job.enqueue(Nppes::Jobs::IniterJob.new)
end

.background_proceed_updatesObject



31
32
33
34
35
# File 'lib/nppes/update_pack/pack.rb', line 31

def background_proceed_updates
  Nppes::NpUpdateCheck.proc_needed.each do |update|
    Delayed::Job.enqueue(Nppes::Jobs::UpdaterJob.new(update))
  end
end

.check_updatesObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nppes/update_pack/pack.rb', line 51

def check_updates
  Nppes.logger.warn 'find updates'
  doc = Nokogiri::HTML(open(Nppes.updates_url))
  signature = Nppes.weekly ? Nppes.weekly_signature : Nppes.monthly_signature

  doc.css('a').each do |link|
    Nppes::NpUpdateCheck.where(file_link: link['href']).first_or_create if link['href'] =~ signature
  end

  proceed_updates
end

.init_baseObject

Raises:

  • (Exception)


41
42
43
44
45
46
47
48
49
# File 'lib/nppes/update_pack/pack.rb', line 41

def init_base
  Nppes.logger.warn 'find init file'
  doc = Nokogiri::HTML(open(Nppes.updates_url))
  link = doc.css('a').detect do |link|
    link['href'] =~ Nppes.initiate_signature
  end
  raise Exception.new('Initial file not found') unless link
  proceed(prepare_file(link['href']))
end

.proceed(zip_file) ⇒ Object

Raises:

  • (Exception)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nppes/update_pack/pack.rb', line 9

def proceed(zip_file)
  Nppes.logger.warn 'proceed file'
  zip = Zip::File.open(zip_file)

  data = zip.entries.detect {|entry| entry.name =~ /npidata_\d+-\d+\.csv/}
  #head = zip.entries.detect {|entry| entry.name =~ /npidata_\d+-\d+FileHeader\.csv/}

  raise Exception.new('head or data not found') unless data || head

  #header = UpdatePack::Header.new(head.get_input_stream)

  data = UpdatePack::Data.new(data.get_input_stream)
  Nppes.logger.warn 'proceed data'
  data.proceed
end

.proceed_update(update) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/nppes/update_pack/pack.rb', line 67

def proceed_update(update)
  begin
    proceed(prepare_file(update.file_link))
  rescue
    Nppes.logger.error $!
    Nppes.logger.error $@
    update.update_attribute(:done, false)
  else
    update.update_attribute(:done, true)
  end
end

.proceed_updatesObject



25
26
27
28
29
# File 'lib/nppes/update_pack/pack.rb', line 25

def proceed_updates
  Nppes::NpUpdateCheck.proc_needed.each do |update|
    proceed_update(update)
  end
end