Class: RelatonBipm::RawdataBipmMetrologia::Affiliations
- Inherits:
-
Object
- Object
- RelatonBipm::RawdataBipmMetrologia::Affiliations
- Defined in:
- lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb
Instance Attribute Summary collapse
-
#affiliations ⇒ Object
readonly
Returns the value of attribute affiliations.
Class Method Summary collapse
-
.parse(dir) ⇒ RelatonBipm::RawdataBipmMetrologia::Affiliations
Parse affiliations.
- .parse_address(aff) ⇒ Object
-
.parse_affiliation(aff) ⇒ RelatonBib::Affiliation
Parse affiliation organization github.com/relaton/relaton-data-bipm/issues/17#issuecomment-1367035444.
- .parse_division(aff) ⇒ Object
- .parse_elements(aff) ⇒ Object
Instance Method Summary collapse
-
#find(text) ⇒ RelatonBib::Affiliation
Find affiliation by organization name.
-
#initialize(affiliations) ⇒ Affiliations
constructor
Initialize parser.
Constructor Details
#initialize(affiliations) ⇒ Affiliations
Initialize parser
11 12 13 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 11 def initialize(affiliations) @affiliations = affiliations end |
Instance Attribute Details
#affiliations ⇒ Object (readonly)
Returns the value of attribute affiliations.
4 5 6 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 4 def affiliations @affiliations end |
Class Method Details
.parse(dir) ⇒ RelatonBipm::RawdataBipmMetrologia::Affiliations
Parse affiliations
20 21 22 23 24 25 26 27 28 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 20 def self.parse(dir) affiliations = Dir["#{dir}/*.xml"].each_with_object([]) do |path, m| doc = Nokogiri::XML(File.read(path, encoding: "UTF-8")) doc.xpath("//aff").each do |aff| m << parse_affiliation(aff) if aff.at("institution") end end.uniq { |a| a.organization.name.first.content } new affiliations end |
.parse_address(aff) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 71 def self.parse_address(aff) address = [] addr = aff.xpath("text()[preceding-sibling::institution]").text.gsub(/^\W*|\W*$/, "") address << addr unless addr.empty? country = aff.at('country') address << country.text if country && !country.text.empty? address = address.join(", ") return [] if address.empty? [RelatonBib::Address.new(formatted_address: address)] end |
.parse_affiliation(aff) ⇒ RelatonBib::Affiliation
Parse affiliation organization github.com/relaton/relaton-data-bipm/issues/17#issuecomment-1367035444
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 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 38 def self.parse_affiliation(aff) text = aff.at("text()").text return if text.include? "Permanent address:" || text.include?("1005 Southover Lane") || text == "Germany" || text.starts_with?("Guest") || text.starts_with?("Deceased") || text.include?("Author to whom any correspondence should be addressed") args = {} institution = aff.at('institution') if institution name = institution.text return if name == "1005 Southover Lane" args[:subdivision] = parse_division(aff) args[:contact] = parse_address(aff) else # div, name, city, country = aff.xpath("text()").text.strip.split(", ") # div, name = name, div if name.nil? # args[:subdivision] = [RelatonBib::LocalizedString.new(div)] if div # args[:contact] = [RelatonBib::Address.new(city: city, country: country)] if city && country name = aff.text end args[:name] = [RelatonBib::LocalizedString.new(name)] org = RelatonBib::Organization.new(**args) RelatonBib::Affiliation.new(organization: org) end |
.parse_division(aff) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 64 def self.parse_division(aff) div = aff.xpath("text()[following-sibling::institution]").text.gsub(/^\W*|\W*$/, "") return [] if div.empty? [RelatonBib::LocalizedString.new(div)] end |
.parse_elements(aff) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 83 def self.parse_elements(aff) elements = aff.xpath("text()").text.strip.split(", ") case elements.size when 1 then { name: RelatonBib::LocalizedString.new(elements[0]) } when 2 # name, country { name: RelatonBib::LocalizedString.new(elements[0]), contact: [RelatonBib::Address.new(formatted_address: elements[1])] } when 3 # it can be name, country, city or name, city, country # so use formatted_address instead of city and country { name: RelatonBib::LocalizedString.new(elements[0]), contact: RelatonBib::Address.new(formatted_address: elements[1, 2].join(", ")) } end end |
Instance Method Details
#find(text) ⇒ RelatonBib::Affiliation
Find affiliation by organization name
106 107 108 |
# File 'lib/relaton_bipm/rawdata_bipm_metrologia/affiliations.rb', line 106 def find(text) @affiliations.select { |a| text.include?(a.organization.name[0].content) }.sort.last end |