Sha256: 600854cb8362497f22d92fc9d3fac6d2a6341320770548fbfea7bd13a2f91ed1

Contents?: true

Size: 1.6 KB

Versions: 62

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class PubmedOnebox
      include Engine
      include LayoutSupport

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

      private

      def get_xml
        doc = Nokogiri::XML(open(URI.join(@url, "?report=xml&format=text")))
        pre = doc.xpath("//pre")
        Nokogiri::XML("<root>" + pre.text + "</root>")
      end

      def authors_of_xml(xml)
        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_of_xml(xml)
        date_arr = (xml.css("PubDate").children).map { |x| x.content }
        date_arr = date_arr.select { |s| !s.match(/^\s+$/) }
        date_arr = (date_arr.map { |s| s.split }).flatten
        date_arr.sort.reverse.join(" ") # Reverse sort so month before year.
      end

      def data
        xml = get_xml()
        {
          title: xml.css("ArticleTitle").text,
          authors: authors_of_xml(xml),
          journal: xml.css("Title").text,
          abstract: xml.css("AbstractText").text,
          date: date_of_xml(xml),
          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

62 entries across 62 versions & 1 rubygems

Version Path
onebox-1.9.28.2 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.28.1 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.28 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.27.2 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.27.1 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.26 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.25 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.24 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.23 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.22 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.21 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.20 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.19 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.18 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.17 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.16 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.15 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.14 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.13 lib/onebox/engine/pubmed_onebox.rb
onebox-1.9.12 lib/onebox/engine/pubmed_onebox.rb