Class: TaxPub::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/taxpub/reference.rb

Class Method Summary collapse

Class Method Details

.authors_to_string(auths) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/taxpub/reference.rb', line 58

def self.authors_to_string(auths)
  authors = auths.dup
  return "" if authors.empty?
  first = authors.first.values.join(", ")
  authors.shift
  remaining = authors.map{|a| a.values.reverse.join(" ")}.join(", ")
  [first, remaining].reject(&:empty?).join(", ")
end

.parse(ref) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/taxpub/reference.rb', line 4

def self.parse(ref)
  ele = ref.at_xpath("element-citation") || ref.at_xpath("mixed-citation")

  auths = []
  ele.xpath("person-group/name").each do |name|
    auths << { 
      surname: name.xpath("surname").text,
      given_names: name.xpath("given-names").text
    }
  end

  institution = ele.xpath("institution").text
  year = ele.xpath("year").text
  title = ele.xpath("article-title").text.chomp(".")
  source = ele.xpath("source").text.chomp(".")
  volume = ele.xpath("volume").text
  pages = [ele.xpath("fpage"), ele.xpath("lpage")].reject(&:empty?).join("")

  if ref.at_xpath("element-citation")
    doi = Utils.expand_doi(ele.xpath("pub-id[@pub-id-type='doi']").text)
    uri = ele.xpath("uri").text
  end

  if ref.at_xpath("mixed-citation")
    doi = Utils.expand_doi(ele.xpath("ext-link[@ext-link-type='doi']").text)
    uri = ele.xpath("ext-link[@ext-link-type='uri']").text
  end

  link = !doi.empty? ? doi : uri

  {
    title: title,
    institution: institution,
    authors: auths,
    year: year,
    source: source,
    volume: volume,
    pages: pages,
    doi: doi,
    uri: uri,
    full_citation: [
      institution,
      self.authors_to_string(auths),
      year,
      title,
      [
        source,
        [volume, pages].reject(&:empty?).join(": ")
      ].reject(&:empty?).join(" "), 
      link
    ].reject(&:empty?).join(". ")
  }
end