Class: Pubmed

Inherits:
Service show all
Defined in:
lib/service_adaptors/pubmed.rb

Overview

FIXME This service is not working For starters it needs the method service_types_generated()

Constant Summary

Constant Summary

Constants inherited from Service

Service::LinkOutFilterTask, Service::StandardTask

Instance Attribute Summary (collapse)

Attributes inherited from Service

#name, #priority, #request, #service_id, #session_id, #status, #task, #url

Instance Method Summary (collapse)

Methods inherited from Service

#display_name, #handle_wrapper, #initialize, #link_out_filter, #preempted_by, required_config_params, #response_to_view_data, #response_url, #service_types_generated, #session, #update_session, #view_data_from_service_type

Constructor Details

This class inherits a constructor from Service

Instance Attribute Details

- (Object) atitle (readonly)

Returns the value of attribute atitle



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

def atitle
  @atitle
end

- (Object) aufirst (readonly)

Returns the value of attribute aufirst



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

def aufirst
  @aufirst
end

- (Object) auinit (readonly)

Returns the value of attribute auinit



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

def auinit
  @auinit
end

- (Object) aulast (readonly)

Returns the value of attribute aulast



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

def aulast
  @aulast
end

- (Object) date (readonly)

Returns the value of attribute date



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

def date
  @date
end

- (Object) description (readonly)

Returns the value of attribute description



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

def description
  @description
end

- (Object) eissn (readonly)

Returns the value of attribute eissn



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

def eissn
  @eissn
end

- (Object) issn (readonly)

Returns the value of attribute issn



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

def issn
  @issn
end

- (Object) issue (readonly)

Returns the value of attribute issue



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

def issue
  @issue
end

- (Object) jtitle (readonly)

Returns the value of attribute jtitle



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

def jtitle
  @jtitle
end

- (Object) pages (readonly)

Returns the value of attribute pages



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

def pages
  @pages
end

- (Object) stitle (readonly)

Returns the value of attribute stitle



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

def stitle
  @stitle
end

- (Object) subjects (readonly)

Returns the value of attribute subjects



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

def subjects
  @subjects
end

- (Object) volume (readonly)

Returns the value of attribute volume



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

def volume
  @volume
end

Instance Method Details

- (Boolean) can_resolve?(req)

Only request for things with PMIDs



21
22
23
24
25
26
27
# File 'lib/service_adaptors/pubmed.rb', line 21

def can_resolve?(req)    
  req.referent.referent_values.find(:all, :conditions=>['key_name = ?', 'identifier']).each do | val |
    # PMIDs can come in with either of these prefixes
    return val.value if val.value.match(/^(info:pmid\/)|(pmid:)/)          
  end    
  return false
end

- (Object) fetch_record(id)

Do the request. Takes the PMID as inputs



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/service_adaptors/pubmed.rb', line 30

def fetch_record(id)    
  id.sub!(/^(info:pmid\/)|(pmid:)/, "")
  pmid_url = self.url + "?db=pubmed&retmode=xml&rettype=full&id="+id
  begin
    response = Net::HTTP.get_response(URI.parse(pmid_url))
  rescue
    return false
  end
  return false if response.body.match("<ERROR>Empty id list - nothing todo</ERROR>")
  return response.body
end

- (Object) handle(request)



11
12
13
14
15
16
17
18
# File 'lib/service_adaptors/pubmed.rb', line 11

def handle(request)
  return request.dispatched(self, true) unless id = self.can_resolve?(request)
  unless request.dispatched?(self)
    return request.dispatched(self, false) unless response = self.fetch_record(id)
    self.parse_record(response, request)
    return request.dispatched(self, true)
  end
end

- (Object) parse_record(body, request)

Pull everything useful out of the Pubmed record



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/service_adaptors/pubmed.rb', line 43

def parse_record(body, request)   
  require 'hpricot'
  doc = Hpricot(body)
  return unless cite = (doc/"/PubmedArticleSet/PubmedArticle/MedlineCitation") # Nothing of interest here
  
  # Get the MeSH subject headings
  @subjects = {}
  (cite/'/MeshHeadingList/MeshHeading').each do | mesh |
    @subjects["mesh"] = [] unless @subjects["mesh"]
    subjects = []
    major = ''
    if dn = (mesh/'/DescriptorName').first
      subjects << dn.inner_html
      major = '*' if dn.attributes['majortopicyn'] == "Y"                              
    end
    if qn = (mesh/'/QualifierName').first
      subjects << qn.inner_html
      major = '*' if qn.attributes['majortopicyn'] == "Y"                      
    end  
    @subjects["mesh"] << subjects.join("/")+major 
    request.add_service_response({:service=>self,:key=>'subject',:value_string=>subjects.join("/")+major,:value_alt_string=>'mesh'}, ['subject'])             
  end
  
  return unless article = (cite/"/Article").first # No more useful metadata   
  if abstract = (article/"/Abstract/AbstractText").first
    @description = abstract.inner_html 
  end
  request.add_service_response({:service=>self,:key=>'abstract',:value_text=>@description}, ['abstract']) unless @description.blank?
  if journal = (article/"/journal").first
    if issn = (journal/'/ISSN').first
      if issn.attributes['issntype']=="Print"                  
        request.referent.enhance_referent('issn', issn.inner_html, true, false)
      else 
        request.referent.enhance_referent('eissn', issn.inner_html, true, false)        
      end
    end
    if jrnlissue = (journal/'/JournalIssue').first
      if (jrnlissue/'/Volume').first
        request.referent.enhance_referent('volume', (jrnlissue/'/Volume').first.inner_html, true, false)
      end
      if (jrnlissue/'/Issue').first
        request.referent.enhance_referent('issue', (jrnlissue/'/Issue').first.inner_html, true, false)
      end   
      if (jrnlissue/'/PubDate').first
        if (jrnlissue/'/PubDate/Year').first
          request.referent.enhance_referent('date', (jrnlissue/'/PubDate/Year').first.inner_html, true, false)
        end
      end              
    end
    
    if (journal/'/Title').first
      request.referent.enhance_referent('jtitle', (journal/'/Title').first.inner_html, true, false)          
    end
    if (journal/'/ISOAbbreviation').first
      request.referent.enhance_referent('stitle', (journal/'/ISOAbbreviation').first.inner_html, true, false)
    end         
    if (journal/'/ArticleTitle').first
      request.referent.enhance_referent('atitle', (journal/'/ArticleTitle').first.inner_html, true, false)
    end   
    
    if (article/'/Pagination/MedlinePgn').first
      request.referent.enhance_referent('pages', (article/'/Pagination/MedlinePgn').first.inner_html, true, false)        
    end                

    if (article/'/AuthorList/Author').first
      if (article/'/AuthorList/Author/LastName').first
        request.referent.enhance_referent('aulast', (article/'/AuthorList/Author/LastName').first.inner_html, true, false)
      end
      if (article/'/AuthorList/Author/ForeName').first
        request.referent.enhance_referent('aufirst', (article/'/AuthorList/Author/ForeName').first.inner_html, true, false)
      end          
      if (article/'AuthorList/Author/Initials').first
        request.referent.enhance_referent('auinit', (article/'AuthorList/Author/Initials').first.inner_html, true, false)
      end          
    end   
    request.referent.save
    request.save     
  end      

end