Module: Puree::XMLExtractor::Collection

Defined in:
lib/puree/xml_extractor/collection.rb

Overview

Resource collection XML extractor.

Class Method Summary collapse

Class Method Details

.count(xml) ⇒ Object

Records available for a resource



115
116
117
118
119
# File 'lib/puree/xml_extractor/collection.rb', line 115

def self.count(xml)
  doc = Nokogiri::XML xml
  doc.remove_namespaces!
  doc.xpath('/result/count').text.to_i
end

.datasets(xml) ⇒ Array<Puree::Model::Dataset>

Get models from any multi-record dataset XML response

Parameters:

  • xml (String)

Returns:



13
14
15
# File 'lib/puree/xml_extractor/collection.rb', line 13

def self.datasets(xml)
  models :dataset, xml, '/dataSet'
end

.events(xml) ⇒ Array<Puree::Model::Event>

Get models from any multi-record event XML response

Parameters:

  • xml (String)

Returns:



21
22
23
# File 'lib/puree/xml_extractor/collection.rb', line 21

def self.events(xml)
  models :event, xml, '/event'
end

.external_organisations(xml) ⇒ Array<Puree::Model::ExternalOrganisation>

Get models from any multi-record external organisation XML response

Parameters:

  • xml (String)

Returns:



29
30
31
# File 'lib/puree/xml_extractor/collection.rb', line 29

def self.external_organisations(xml)
  models :external_organisation, xml, '/externalOrganisation'
end

.journals(xml) ⇒ Array<Puree::Model::Journal>

Get models from any multi-record journal XML response

Parameters:

  • xml (String)

Returns:



37
38
39
# File 'lib/puree/xml_extractor/collection.rb', line 37

def self.journals(xml)
  models :journal, xml, '/journal'
end

.make_xml_extractor(resource_type, xml) ⇒ Object



140
141
142
143
# File 'lib/puree/xml_extractor/collection.rb', line 140

def self.make_xml_extractor(resource_type, xml)
  resource_class = "Puree::XMLExtractor::#{Puree::Util::String.titleize(resource_type)}"
  Object.const_get(resource_class).new xml
end

.models(resource_type, xml, xpath_root) ⇒ Array<Puree::Model::Resource subclass>

Get models from any multi-record resource XML response

Parameters:

  • xml (String)

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/puree/xml_extractor/collection.rb', line 127

def self.models(resource_type, xml, xpath_root)
  doc = Nokogiri::XML xml
  doc.remove_namespaces!
  path_from_root = File.join 'result', xpath_root
  xpath_result = doc.xpath path_from_root
  data = []
  xpath_result.each do |i|
    xml_extractor = make_xml_extractor resource_type, i.to_s
    data << xml_extractor.model
  end
  data
end

.organisational_units(xml) ⇒ Array<Puree::Model::OrganisationalUnit>

Get models from any multi-record organisation XML response

Parameters:

  • xml (String)

Returns:



45
46
47
# File 'lib/puree/xml_extractor/collection.rb', line 45

def self.organisational_units(xml)
  models :organisational_unit, xml, '/organisationalUnit'
end

.persons(xml) ⇒ Array<Puree::Model::Person>

Get models from any multi-record person XML response

Parameters:

  • xml (String)

Returns:



61
62
63
# File 'lib/puree/xml_extractor/collection.rb', line 61

def self.persons(xml)
  models :person, xml, '/person'
end

.projects(xml) ⇒ Array<Puree::Model::Project>

Get models from any multi-record project XML response

Parameters:

  • xml (String)

Returns:



53
54
55
# File 'lib/puree/xml_extractor/collection.rb', line 53

def self.projects(xml)
  models :project, xml, '/project'
end

.publishers(xml) ⇒ Array<Puree::Model::Publisher>

Get models from any multi-record person XML response

Parameters:

  • xml (String)

Returns:



69
70
71
# File 'lib/puree/xml_extractor/collection.rb', line 69

def self.publishers(xml)
  models :publisher, xml, '/publisher'
end

.research_outputs(xml) ⇒ Hash{Symbol => Array<Puree::Model::ResearchOutput class/subclass>}

Get models from any multi-record Research output XML response

Parameters:

  • xml (String)

Returns:



77
78
79
80
81
82
83
84
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/puree/xml_extractor/collection.rb', line 77

def self.research_outputs(xml)
  path_from_root = File.join 'result', '/*'
  doc = Nokogiri::XML xml
  doc.remove_namespaces!
  xpath_result = doc.xpath path_from_root
  data = {
      journal_articles: [],
      conference_papers: [],
      theses: [],
      other: []
  }
  xpath_result.each do |research_output|
    type = research_output.xpath('type').text.strip
    unless type.empty?
      case type
        when 'Journal article'
          extractor = Puree::XMLExtractor::JournalArticle.new research_output.to_s
          data[:journal_articles] << extractor.model
        when 'Conference paper'
          extractor = Puree::XMLExtractor::ConferencePaper.new research_output.to_s
          data[:conference_papers] << extractor.model
        when 'Doctoral Thesis'
          extractor = Puree::XMLExtractor::Thesis.new research_output.to_s
          data[:theses] << extractor.model
        when "Master's Thesis"
          extractor = Puree::XMLExtractor::Thesis.new research_output.to_s
          data[:theses] << extractor.model
        else
          extractor = Puree::XMLExtractor::ResearchOutput.new research_output.to_s
          data[:other] << extractor.model
      end
    end
  end
  data
end