Sha256: 0597368512e8ff84da2bec1d49b4688764cece62a42c419a55395e90872e99f9

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class PubmedOnebox
      include Engine
      include LayoutSupport

      matches_regexp(/^https?:\/\/(?:(?:\w)+\.)?(www.ncbi.nlm.nih)\.gov(?:\/)?\/pubmed\/\d+/)

      private

      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

      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

      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

      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

      def match
        @match ||= @url.match(%r{www\.ncbi\.nlm\.nih\.gov/pubmed/(?<pmid>[0-9]+)})
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
onebox-2.2.19 lib/onebox/engine/pubmed_onebox.rb
onebox-2.2.18 lib/onebox/engine/pubmed_onebox.rb
onebox-2.2.17 lib/onebox/engine/pubmed_onebox.rb
onebox-2.2.16 lib/onebox/engine/pubmed_onebox.rb
onebox-2.2.15 lib/onebox/engine/pubmed_onebox.rb