Class: Pubmedly::Parsers::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/pubmedly/parsers/article.rb

Overview

The Article class provides methods to access Article data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Article

Returns a new instance of Article.



12
13
14
# File 'lib/pubmedly/parsers/article.rb', line 12

def initialize(xml)
  @xml = xml
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



10
11
12
# File 'lib/pubmedly/parsers/article.rb', line 10

def xml
  @xml
end

Instance Method Details

#abstractObject



33
34
35
# File 'lib/pubmedly/parsers/article.rb', line 33

def abstract
  @xml.xpath(".//Abstract").text
end

#authorsObject



37
38
39
40
41
# File 'lib/pubmedly/parsers/article.rb', line 37

def authors
  @xml.xpath(".//Author").map do |author|
    "#{author.xpath(".//LastName").text}, #{author.xpath(".//ForeName").text}"
  end
end

#first_authorObject



43
44
45
# File 'lib/pubmedly/parsers/article.rb', line 43

def first_author
  authors.first
end

#last_authorObject



47
48
49
# File 'lib/pubmedly/parsers/article.rb', line 47

def last_author
  authors.last
end

#pmidObject



51
52
53
54
# File 'lib/pubmedly/parsers/article.rb', line 51

def pmid
  id = @xml.xpath(".//PMID").text.to_i
  id.zero? ? nil : id
end

#publication_dateObject



24
25
26
27
28
29
30
31
# File 'lib/pubmedly/parsers/article.rb', line 24

def publication_date
  date_str = @xml.xpath(".//PubDate").text
  begin
    date_str.empty? ? "" : Date.parse(date_str)
  rescue Date::Error
    Date.strptime(date_str.strip, "%Y")
  end
end

#titleObject



20
21
22
# File 'lib/pubmedly/parsers/article.rb', line 20

def title
  @xml.xpath(".//ArticleTitle").text
end

#to_sObject



16
17
18
# File 'lib/pubmedly/parsers/article.rb', line 16

def to_s
  "#{authors.first.split(",").first} (#{publication_date.year}) #{title}"
end