Module: Daengine::TeamsiteMetadataProcessor

Defined in:
lib/daengine/teamsite_metadata_processor.rb

Class Method Summary collapse

Class Method Details

.add_file_attributes(assets, last_read) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/daengine/teamsite_metadata_processor.rb', line 52

def self.add_file_attributes(assets, last_read)
  results = {}
  assets.each do |key, asset|
    begin
puts "----- add_file #{key} -----"
      file_name = asset_file_name(asset)
      asset.mark_for_deletion unless File.file?(file_name)

      set_asset_file_attributes(file_name, asset, last_read) unless asset.delete?

      results[key] = asset
    rescue => ex
      Daengine.log("***** Error processing asset with file name = #{asset.path} - #{ex.inspect}", 'error')
    end
  end
  results
end

.add_fund_codes(assets) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/daengine/teamsite_metadata_processor.rb', line 70

def self.add_fund_codes(assets)
  results = {}
  assets.each do |key, asset|
puts "----- add_fund_codes #{key} -----"
    fund_codes = []
    asset.product_ids.each do |product_id|
      fund_code = DigitalAssetLookupService.fund_code_from_id(product_id)
      fund_codes << fund_code.strip.rjust(5, '0') unless fund_code.blank?
    end
    asset.fund_codes = fund_codes unless fund_codes.empty?
    results[key] = asset
  end
  results
end

.asset_file_name(asset) ⇒ Object



136
137
138
139
140
# File 'lib/daengine/teamsite_metadata_processor.rb', line 136

def self.asset_file_name(asset)
  name = File.join(file_directory, asset.path)
  name = File.join(file_directory, asset.file_name) unless File.exists?(name)
  name
end

.call_service(assets) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/daengine/teamsite_metadata_processor.rb', line 85

def self.call_service(assets)
  results = {:errors => 0, :updated => 0, :deleted => 0}
  assets.each_value do |asset|
    begin
puts "----- call_service #{asset.digital_asset_id} -----"
      if asset.delete?
        path = "#{service_uri}/#{asset.digital_asset_id}"
        options = {:method => :delete, :headers => header}
        operation = :deleted
      else
        path = "#{service_uri}"
        options = {:method => :post,
                   :query => asset.as_hash,
                   :headers => header}
        operation = :updated
      end

      response = Daengine::HTTP::Client.call(path, options)
      results[operation] += 1 if response.success?
      results[:errors] += 1 unless response.success?
    rescue => ex
      Daengine.log("***** Error calling service for #{asset.inspect} - #{ex.inspect}", 'error')
      results[:errors] += 1
    end
  end
  results
end

.file_directoryObject



179
180
181
# File 'lib/daengine/teamsite_metadata_processor.rb', line 179

def self.file_directory
  Daengine.config[:digital_assets_file_directory]
end

.finra_path(list) ⇒ Object



131
132
133
134
# File 'lib/daengine/teamsite_metadata_processor.rb', line 131

def self.finra_path(list)
  finra = list.find { |value| value.finra? }
  finra.try(:path)
end

.headerObject



169
170
171
172
173
# File 'lib/daengine/teamsite_metadata_processor.rb', line 169

def self.header
  {'Accept' => 'application/json',
   'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
   'User-Agent' => 'Ruby'}
end

.most_recent_non_expired(list) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/daengine/teamsite_metadata_processor.rb', line 120

def self.most_recent_non_expired(list)
  list.inject do |previous, current|
    prev_published_at = previous.default_blank_time(:published_at)
    current_published_at = current.default_blank_time(:published_at)
    !current.expired? &&
        !current.manifest_file? &&
        !current.finra? &&
        current_published_at >= prev_published_at ? current : previous
  end
end

.parse_file(file) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/daengine/teamsite_metadata_processor.rb', line 32

def self.parse_file(file)
puts "----- Parsing the file -----"
  document = Document.new
  Nokogiri::XML::SAX::Parser.new(document).parse(file)
  Daengine.log("Nokogiri Parser complete...", "info")
  document.assets
end

.process_tuple_file(file, last_read = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/daengine/teamsite_metadata_processor.rb', line 8

def self.process_tuple_file(file, last_read = nil)
  verify_pre_conditions

  time do
    assets = parse_file(file)
    assets = select_1_asset_per_id(assets)
    assets = add_file_attributes(assets, last_read) unless Daengine.config[:disable_file_check]
    assets = add_fund_codes(assets)
    summary = call_service(assets)
    Daengine.log("***** Summary = #{summary.inspect}", 'info')
  end
rescue => ex
  Daengine.log("Error processing XML file - #{ex.inspect}", 'error')
  Daengine.log(ex.backtrace.join, 'error')
end

.select_1_asset_per_id(assets) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daengine/teamsite_metadata_processor.rb', line 40

def self.select_1_asset_per_id(assets)
  results = {}
  assets.each do |key, values|
puts "----- select_1 #{key} -----"
    list = values.find_all { |v| v.effective? }
    asset = most_recent_non_expired(list)
    asset.finra_path = finra_path(list) unless asset.nil?
    results[key] = asset unless asset.nil?
  end
  results
end

.service_uriObject



175
176
177
# File 'lib/daengine/teamsite_metadata_processor.rb', line 175

def self.service_uri
  Daengine.config[:digital_asset_service_url]
end

.set_asset_file_attributes(file_name, asset, last_read) ⇒ Object



142
143
144
145
146
# File 'lib/daengine/teamsite_metadata_processor.rb', line 142

def self.set_asset_file_attributes(file_name, asset, last_read)
  if File.mtime(file_name) > last_read
    update_asset_file_attributes(file_name, asset)
  end
end

.timeObject



113
114
115
116
117
# File 'lib/daengine/teamsite_metadata_processor.rb', line 113

def self.time
  start = Time.now
  yield
  Daengine.log("***** Elapsed time was #{Time.now - start}", 'info')
end

.update_asset_file_attributes(file_name, asset) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/daengine/teamsite_metadata_processor.rb', line 148

def self.update_asset_file_attributes(file_name, asset)
  exifdata = ::MiniExiftool.new(file_name)
  pages = exifdata.pagecount
  pages = exifdata.pages if pages.blank?
  pages = exifdata.slides if pages.blank?
  asset.pages = pages
  asset.size = exifdata.filesize
  asset.mime_type = exifdata.mimetype
  asset.author = exifdata.author
  if exifdata.keywords.is_a? Enumerable
    asset.keywords = exifdata.keywords
  else
    asset.keywords = exifdata.keywords.gsub(';', ',').gsub(':', ',').split(',') unless exifdata.keywords.nil?
  end
  if exifdata.description.is_a? Enumerable
    asset.subject = exifdata.description.join(' ') unless exifdata.description.nil?
  else
    asset.subject = exifdata.description.gsub(':', '') unless exifdata.description.nil?
  end
end

.verify_pre_conditionsObject



24
25
26
27
28
29
30
# File 'lib/daengine/teamsite_metadata_processor.rb', line 24

def self.verify_pre_conditions
  path = Daengine.config[:digital_assets_file_directory]
  raise "Unable to locate digital assets at #{path}" unless File.directory?(path) || Daengine.config[:disable_file_check]

  service_uri = Daengine.config[:digital_asset_service_url]
  raise 'No digital asset service URL set' if service_uri.blank?
end