Skip to content

Instantly share code, notes, and snippets.

@handerson
Created May 15, 2012 16:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save handerson/2703006 to your computer and use it in GitHub Desktop.
Save handerson/2703006 to your computer and use it in GitHub Desktop.
a very basic Pubmed search API in ruby
Copyright (c) 2012 Heath Anderson
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class PubmedApi # a very basic Pubmed API
require "net/http"
require "uri"
require "nokogiri"
def self.find(ids) # can accept an array of ids or a single id or a string of ids separated by commas
param = ids.class == Array ? ids.join(",") : ids
uri = URI.parse("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=#{param}")
response = Net::HTTP.get_response(uri)
documents = []
if response.code_type.to_s == "Net::HTTPOK"
parsedDoc = Nokogiri::XML(response.body)
parsedDoc = parsedDoc.css("eSummaryResult DocSum")
parsedDoc.each do |pd|
doc = {}
doc[:title] =pd.css("Item[Name=Title]")[0].text
doc[:id] = pd.at_css("Item[Name=ArticleIds] Item[Name=pubmed]").text
doc[:date] =pd.css("Item[Name=PubDate]")[0].text
doc[:source] =pd.css("Item[Name=FullJournalName]")[0].text
doc[:url] = "http://www.ncbi.nlm.nih.gov/pubmed/#{doc[:id]}"
doc[:db] = "pubmed"
doc[:authors] = []
pd.css("Item[Name=AuthorList] Item[Name=Author]").each do |author|
doc[:authors] << author.text
end
documents << doc
end
end
documents
end
def self.search(terms)
# see # http://www.ncbi.nlm.nih.gov/books/NBK3827/#pubmedhelp.Search_Field_Descrip for help with terms
# A search query can be build by going to http://www.ncbi.nlm.nih.gov/pubmed and then using the filters provided. The search terms can then be copied from the "Search details" section on the right-hand side and used here.
terms = URI.escape(terms, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
uri = URI.parse("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=#{terms}")
response = Net::HTTP.get_response(uri)
ids = []
if response.code_type.to_s == "Net::HTTPOK"
parsedDoc = Nokogiri::XML(response.body)
parsedDoc = parsedDoc.css("eSearchResult")
if parsedDoc.at_css("Count").text.to_i > 0
parsedDoc.css("IdList Id").each do |id|
ids << id.text
end
end
end
self.find(ids)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment