class PubmedAPI::Interface

Constants

DEFAULT_OPTIONS
URI_TEMPLATE
WAIT_TIME

Public Class Methods

convert_odd_journal_ids(id) click to toggle source

Some journals have odd NLMIDs that need to be searched for rarther than accessed directly.

# File lib/pubmed_api.rb, line 121
def convert_odd_journal_ids(id)
  
  new_id = nil
  results = search(id, {:database => 'db=nlmcatalog'})
  if results.pmids.length ==1
    new_id = results.pmids[0]
  else
    puts "failed to convert " + id.to_s
  end
  new_id.to_s
end
fetch_journals(nlmids) click to toggle source
# File lib/pubmed_api.rb, line 68
def fetch_journals(nlmids)
  #Change the ids of those wierd journals
  nlmids = nlmids.map { |e|  ((e.include? 'R') ? convert_odd_journal_ids(e) : e ) }
  xml = fetch_records(nlmids, {:verb => 'fetch',:database => 'db=nlmcatalog'})
  parser = XMLParser.new
  parser.parse_journals(xml)       
end
fetch_papers(ids) click to toggle source
# File lib/pubmed_api.rb, line 53
def fetch_papers(ids)
  xml = fetch_records(ids, {:verb => 'fetch',:database => 'db=pubmed'})
  parser = XMLParser.new
  papers = parser.parse_papers(xml)
  lookup_hash = get_fulltext_links(ids)
  
  papers.each do |p|
    if p.nil?
       next
    else
      p.url =  lookup_hash[p.pmid].first.url 
    end
  end
end
fetch_records(ids, opts={}) click to toggle source
# File lib/pubmed_api.rb, line 87
def fetch_records(ids, opts={})

  xml_records = []
  
  options = DEFAULT_OPTIONS.merge(opts)

  #dice array into reasonable length chunks for download
  n_length = 500
  # TODO paralellise?
  ids.each_slice(n_length) do |slice|

    #Turn string to something html friendly
    id_string = slice.join(",")
    doc = make_api_request(options.merge({ :query => 'id='+id_string}))
    records = doc.xpath('./*/*')
    xml_records += records

  end

  xml_records
end
get_journal_id_from_issn(issn) click to toggle source
# File lib/pubmed_api.rb, line 134
def get_journal_id_from_issn(issn)
  
  id = nil
  term = issn + "[ISSN]+AND+ncbijournals[filter]"

  results = search(term, {:database => 'db=nlmcatalog'})
  if results.pmids.length ==1
    id = results.pmids[0]
  else
    puts "failed to find " + issn.to_s
  end
  
  id.to_s
end
make_api_request(options) click to toggle source

Maked the HTTP request and return the responce TODO handle failures

# File lib/pubmed_api.rb, line 114
def make_api_request(options)
    url = expand_uri(URI_TEMPLATE, options)
    Nokogiri::XML( open url )
end
wait() click to toggle source

300ms minimum wait.

# File lib/pubmed_api.rb, line 154
def wait
  sleep WAIT_TIME 
end

Private Class Methods

expand_uri(uri, options) click to toggle source
# File lib/pubmed_api.rb, line 161
def expand_uri(uri, options)
  uri.gsub(/\{(.*?)\}/) { URI.encode( (options[$1] || options[$1.to_sym] || '').to_s ) rescue '' }
end