Class: Pubmedly::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pubmedly/client.rb

Overview

The Client class provides methods for accessing the NCBI’s eutils Pubmed API that return HTTP objects.

The NCBI eutils API provides access to many databases, but these out of the scope of pubmedly:

https://www.ncbi.nlm.nih.gov/books/NBK25497/table/chapter2.T._entrez_unique_identifiers_ui/?report=objectonly

Constant Summary collapse

BASE_URL =
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
NCBI_DB =
"pubmed"

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



15
16
17
# File 'lib/pubmedly/client.rb', line 15

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#fetch(*ids, **kwargs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pubmedly/client.rb', line 43

def fetch(*ids, **kwargs)
  url = URI.parse("#{BASE_URL}efetch.fcgi")

  params = kwargs.merge(
    db:      NCBI_DB,
    api_key: @api_key,
    id:      ids.join(",")
  )

  url.query = URI.encode_www_form(params)

  http         = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  http.request(Net::HTTP::Get.new(url.request_uri))
end

#search(term, **kwargs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pubmedly/client.rb', line 25

def search(term, **kwargs)
  url = URI.parse("#{BASE_URL}esearch.fcgi")

  params = kwargs.merge({
    db:      NCBI_DB,
    api_key: @api_key,
    term:    term,
  })

  url.query = URI.encode_www_form(params)

  http         = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  http.request(Net::HTTP::Get.new(url.request_uri))
end