Class: Libis::Metadata::Downloader

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

Constant Summary collapse

NO_DECL =
Nokogiri::XML::Node::SaveOptions::FORMAT + Nokogiri::XML::Node::SaveOptions::NO_DECLARATION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDownloader

Returns a new instance of Downloader.



23
24
25
26
27
28
# File 'lib/libis/metadata/downloader.rb', line 23

def initialize
  @service = nil
  @target_dir = nil
  @config = nil
  @mapper_class = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/libis/metadata/downloader.rb', line 21

def config
  @config
end

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



21
22
23
# File 'lib/libis/metadata/downloader.rb', line 21

def mapper_class
  @mapper_class
end

#serviceObject (readonly)

Returns the value of attribute service.



21
22
23
# File 'lib/libis/metadata/downloader.rb', line 21

def service
  @service
end

Instance Method Details

#configure(config) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/libis/metadata/downloader.rb', line 30

def configure(config)
   = config[:metadata]
  case 
  when 'alma'
    @service ||= Libis::Services::Alma::SruService.new
    @mapper_class = Libis::Metadata::Mappers::Kuleuven
  when 'scope'
    @service = ::Libis::Services::Scope::Search.new
    @mapper_class = Libis::Metadata::Mappers::Scope
    @service.connect(config[:password], config[:password], config[:database])
  else
    raise RuntimeError, "Service '#{service}' unknown"
  end
  @target_dir = config[:target_dir]
  @config = config
rescue Exception => e
  raise RuntimeError "failed to configure metadata service: #{e.message} @ #{e.backtrace.first}"
end

#download(term, filename, pid = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/libis/metadata/downloader.rb', line 49

def download(term, filename, pid = nil)
  record = search(term)
  return nil unless record

  record = md_update_xml(pid, record) if pid

  record.save File.join(@target_dir, filename)

  filename
end

#md_update_xml(pid, record) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/libis/metadata/downloader.rb', line 100

def md_update_xml(pid, record)
  Libis::Tools::XmlDocument.parse <<EO_XML
<updateMD xmlns="http://com/exlibris/digitool/repository/api/xmlbeans">
  <PID>#{pid}</PID>
  <metadata>
    <type>descriptive</type>
    <subType>dc</subType>
    <content>
<![CDATA[#{record.document.to_xml(save_with: NO_DECL)}]]>
    </content>
  </metadata>
</updateMD>
EO_XML
end

#save(record, filename) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/libis/metadata/downloader.rb', line 90

def save(record, filename)
  return false unless record

  record.save File.join(@target_dir, filename)

  true
end

#search(term) ⇒ Libis::Metadata::DublinCoreRecord



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
88
# File 'lib/libis/metadata/downloader.rb', line 61

def search(term)
  record = case service
           when ::Libis::Services::Alma::SruService
             result = service.search(config[:field], CGI.escape("\"#{term}\""), config[:library])
             raise RuntimeError "Multiple records found for #{config[:field]}=#{term}" if result.size > 1
             result.empty? ? nil : ::Libis::Metadata::Marc21Record.new(result.first.root)

           when ::Libis::Services::Scope::Search
             service.query(term, type: config[:field])
             service.next_record do |doc|
               ::Libis::Metadata::DublinCoreRecord.new(doc.to_xml)
             end

           else
             raise RuntimeError "Service '#{service}' unknown"

           end

  unless record
    raise RuntimeError, "No record found for #{config[:field]} = '#{term}'"
  end

  record.extend mapper_class
  record.to_dc

rescue Exception => e
  raise RuntimeError, "Search request failed: #{e.message}"
end