class Onebox::Engine::PubmedOnebox

Private Instance Methods

authors() click to toggle source
# File lib/onebox/engine/pubmed_onebox.rb, line 20
def authors
  initials = xml.css("Initials").map { |x| x.content }
  last_names = xml.css("LastName").map { |x| x.content }
  author_list = (initials.zip(last_names)).map { |i, l| i + " " + l }
  if author_list.length > 1 then
    author_list[-2] = author_list[-2] + " and " + author_list[-1]
    author_list.pop
  end
  author_list.join(", ")
end
data() click to toggle source
# File lib/onebox/engine/pubmed_onebox.rb, line 43
def data
  {
    title: xml.css("ArticleTitle").text,
    authors: authors,
    journal: xml.css("Title").text,
    abstract: xml.css("AbstractText").text,
    date: date,
    link: @url,
    pmid: match[:pmid]
  }
end
date() click to toggle source
# File lib/onebox/engine/pubmed_onebox.rb, line 31
def date
  xml.css("PubDate")
    .children
    .map { |x| x.content }
    .select { |s| !s.match(/^\s+$/) }
    .map { |s| s.split }
    .flatten
    .sort
    .reverse
    .join(" ") # Reverse sort so month before year.
end
match() click to toggle source
# File lib/onebox/engine/pubmed_onebox.rb, line 55
def match
  @match ||= @url.match(%r{www\.ncbi\.nlm\.nih\.gov/pubmed/(?<pmid>[0-9]+)})
end
xml() click to toggle source
# File lib/onebox/engine/pubmed_onebox.rb, line 13
def xml
  return @xml if defined?(@xml)
  doc = Nokogiri::XML(URI.open(URI.join(@url, "?report=xml&format=text")))
  pre = doc.xpath("//pre")
  @xml = Nokogiri::XML("<root>" + pre.text + "</root>")
end