Class: PubMed

Inherits:
Citation::Article show all
Defined in:
lib/pubmed.rb

Overview

given the html page where the display is specified as xml extracts out the requested pieces

Instance Attribute Summary collapse

Attributes inherited from Citation::Article

#issue, #month, #pages, #title, #vol, #year

Attributes included from JournalLike

#journal_full, #journal_iso, #journal_medline

Attributes inherited from Citation

#abstract, #authors, #bibtype, #ident, #quotes

Instance Method Summary collapse

Methods inherited from Citation::Article

#==, #pages_full

Methods included from JournalLike

#has_journal?, #set_journal_from_medline

Methods inherited from Citation

#to_hash, #to_yaml

Constructor Details

#initialize(pmid = nil, identifier = nil) ⇒ PubMed

also takes pmid=hash of values to set



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
# File 'lib/pubmed.rb', line 13

def initialize(pmid=nil, identifier=nil)
  @quotes = []
  if pmid.is_a? Hash
    ########## THIS WHOLE MESS SHOULD BE ENCAPSULATED/INHERITED! but can't get
    #inheritance with authors= working for some reason
    @authors = []
    pmid.each do |k,v|
      if k == 'authors'
        v.each do |auth|
        if auth.is_a? String
          authors.push( Citation::Author.from_s(auth) )
        else
          authors.push( auth )
        end
        end
      else
        send("#{k}=".to_sym, v)
      end
    end
    ############ <-- END MESS
  else
    @authors = []
    @pmid = pmid
    @bibtype = :article
    if pmid
      begin
        url = query_builder(pmid)
        xml_string = get_xml(url)
        extract_attrs_from_xml(xml_string)
      end
    end
    if identifier
      @ident = identifier
    else
      if pmid
        @ident = create_id
      end
    end
  end
end

Instance Attribute Details

#pmidObject

Returns the value of attribute pmid.



10
11
12
# File 'lib/pubmed.rb', line 10

def pmid
  @pmid
end

Instance Method Details

#create_idObject

first author’s last name + year collapsing any spaces



64
65
66
# File 'lib/pubmed.rb', line 64

def create_id
  (@authors[0].last.to_s + @year.to_s).sub(/\s+/,'')
end

#get_xml(query) ⇒ Object

returns xml from online (parses html output). No internet connection gives nil



56
57
58
59
60
61
# File 'lib/pubmed.rb', line 56

def get_xml(query)
  handle = open(query)
  xml = handle.read
  handle.close
  xml
end

#inspectObject



68
69
70
71
72
73
74
# File 'lib/pubmed.rb', line 68

def inspect
  st = "<#{self.class}:##{self.__id__} "
  st << ( %w(authors ident quotes abstract journal_medline title year month vol issue pages).reject{|v| (v == :authors || v == :url)}.push(:bibtype).map {|v| ":#{v}=>#{send(v).inspect}"}.join(", ") )
  st << " @authors=[#{authors.map{|g| g.inspect }.join(", ")}]"
  st << ">"
  st
end