Class: AssembleMarcFiles

Inherits:
Object
  • Object
show all
Defined in:
app/runners/assemble_marc_files.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

BOX_KBART_FOLDER =
'/My Box Notes/cronuts/download/kbart'
BOX_MARC_FOLDER =
'/My Box Notes/cronuts/download/marc'
BOX_CATALOG_FOLDER =
'/My Box Notes/cronuts/download/catalog'
BOX_UPLOAD_FOLDER =
'/My Box Notes/cronuts/upload/marc'
TMP_DIR =
Rails.root.join('tmp')
TMP_ASSEMBLE_MARC_FILES_ROOT =
File.join(TMP_DIR, 'assemble_marc_files')
TMP_DOWNLOAD_DIR =
File.join(TMP_ASSEMBLE_MARC_FILES_ROOT, 'download')
TMP_KBART_DOWNLOAD_DIR =
File.join(TMP_DOWNLOAD_DIR, 'kbart')
TMP_MARC_DOWNLOAD_DIR =
File.join(TMP_DOWNLOAD_DIR, 'marc')
TMP_CATALOGING_DOWNLOAD_DIR =
File.join(TMP_DOWNLOAD_DIR, 'cataloging')
TMP_UPLOAD_DIR =
File.join(TMP_ASSEMBLE_MARC_FILES_ROOT, 'upload')
TMP_KBART_UPLOAD_DIR =
File.join(TMP_UPLOAD_DIR, 'kbart')
TMP_MARC_UPLOAD_DIR =
File.join(TMP_UPLOAD_DIR, 'marc')
TMP_CATALOGING_UPLOAD_DIR =
File.join(TMP_UPLOAD_DIR, 'cataloging')

Instance Method Summary collapse

Instance Method Details

#downloadObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/runners/assemble_marc_files.rb', line 28

def download # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  FileUtils.rm_rf(TMP_ASSEMBLE_MARC_FILES_ROOT) if Dir.exist?(TMP_ASSEMBLE_MARC_FILES_ROOT)
  Dir.mkdir(TMP_ASSEMBLE_MARC_FILES_ROOT)
  Dir.mkdir(TMP_DOWNLOAD_DIR)
  Dir.mkdir(TMP_KBART_DOWNLOAD_DIR)
  Dir.chdir(TMP_KBART_DOWNLOAD_DIR) do
    Box::Service.new.folder(BOX_KBART_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
  Dir.mkdir(TMP_MARC_DOWNLOAD_DIR)
  Dir.chdir(TMP_MARC_DOWNLOAD_DIR) do
    Box::Service.new.folder(BOX_MARC_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
  Dir.mkdir(TMP_CATALOGING_DOWNLOAD_DIR)
  Dir.chdir(TMP_CATALOGING_DOWNLOAD_DIR) do
    Box::Service.new.folder(BOX_CATALOG_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
  Dir.mkdir(TMP_UPLOAD_DIR)
  Dir.mkdir(TMP_KBART_UPLOAD_DIR)
  Dir.chdir(TMP_KBART_UPLOAD_DIR) do
    Box::Service.new.folder(BOX_KBART_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
  Dir.mkdir(TMP_MARC_UPLOAD_DIR)
  Dir.chdir(TMP_MARC_UPLOAD_DIR) do
    Box::Service.new.folder(BOX_MARC_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
  Dir.mkdir(TMP_CATALOGING_UPLOAD_DIR)
  Dir.chdir(TMP_CATALOGING_UPLOAD_DIR) do
    Box::Service.new.folder(BOX_CATALOG_FOLDER).files.each do |file|
      File.open(file.name, "wb") do |f|
        f.write(file.content)
        f.close
      end
    end
  end
end

#run(rebuild = false) ⇒ Object


22
23
24
25
26
# File 'app/runners/assemble_marc_files.rb', line 22

def run(rebuild = false)
  download
  update(rebuild)
  upload
end

#update(rebuild) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/runners/assemble_marc_files.rb', line 89

def update(rebuild) # rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
  product_names = []
  mm = format("%02d", Time.now.month)

  Dir.chdir(TMP_KBART_DOWNLOAD_DIR) do # rubocop:disable Metrics/BlockLength
    Dir.foreach('.') do |item| # rubocop:disable Metrics/BlockLength
      next unless File.file?(item)

      product = Kbart::Filename.new(item)
      product_names << product.name
      next unless rebuild || product.modified_today?

      new_mrc_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{product.base}.mrc"), "w")
      new_xml_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{product.base}.xml"), "w")
      if product.year?
        new_mrc_mm_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{product.base}_#{mm}.mrc"), "a")
        new_xml_mm_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{product.base}_#{mm}.xml"), "a")
      end
      catalog = Cataloging::Cataloging.new(product)
      kbart = Kbart::Kbart.new(File.join(TMP_KBART_DOWNLOAD_DIR, item))
      kbart.dois.each do |doi|
        marc = catalog.marc(doi)
        next unless marc

        new_mrc_file << marc.to_mrc
        new_xml_file << marc.to_xml
        next unless product.year?

        unless marc.in_file?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}.mrc"))
          if File.exist?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}_#{mm}.mrc"))
            new_mrc_mm_file << marc.to_mrc unless marc.in_file?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}_#{mm}.mrc"))
          else
            new_mrc_mm_file << marc.to_mrc
          end
        end
        unless marc.in_file?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}.xml"))
          if File.exist?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}_#{mm}.xml"))
            new_xml_mm_file << marc.to_xml unless marc.in_file?(File.join(TMP_MARC_DOWNLOAD_DIR, "#{product.base}_#{mm}.xml"))
          else
            new_xml_mm_file << marc.to_xml
          end
        end
      end
      new_mrc_file.close
      new_xml_file.close
      if product.year?
        new_mrc_mm_file.close
        new_xml_mm_file.close
      end
    end
    Dir.chdir(TMP_MARC_UPLOAD_DIR) do
      product_names.uniq.each do |name|
        complete_mrc_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{name}_Complete.mrc"), "w")
        complete_xml_file = File.open(File.join(TMP_MARC_UPLOAD_DIR, "#{name}_Complete.xml"), "w")
        Dir.foreach('.') do |item|
          next unless File.file?(item)

          product = Marc::Filename.new(item)
          next unless /^#{name}$/i.match?(product.name)
          next if product.open_access?
          next if product.monthly?
          next if product.complete?

          puts product.inspect
          if product.mrc?
            complete_mrc_file.write(File.read(item))
          elsif product.xml?
            complete_xml_file.write(File.read(item))
          end
        end
        complete_mrc_file.close
        complete_xml_file.close
      end
    end
  end
end

#uploadObject


166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/runners/assemble_marc_files.rb', line 166

def upload
  box_upload_folder = Box::Service.new.folder(BOX_UPLOAD_FOLDER)
  Dir.chdir(TMP_MARC_UPLOAD_DIR) do
    Dir.foreach('.') do |item|
      next unless File.file?(item)

      if File.exist?(File.join(TMP_MARC_DOWNLOAD_DIR, item))
        next if File.size?(item) == File.size?(File.join(TMP_MARC_DOWNLOAD_DIR, item))
      end

      box_upload_folder.upload(File.join(TMP_MARC_UPLOAD_DIR, item))
    end
  end
end