Class: PMC::Efetch

Inherits:
Object
  • Object
show all
Defined in:
lib/pmc/efetch.rb

Constant Summary collapse

BASE_URL =
"https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi"
MAX_SLICE =
150
TOOL =
"buhos"
EMAIL =
"clbustos.2@gmail.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pmid_list) ⇒ Efetch

Returns a new instance of Efetch.



64
65
66
67
68
# File 'lib/pmc/efetch.rb', line 64

def initialize(pmid_list)
  @pmid_list=pmid_list
  @pmid_xml=EfetchXMLSummaries.new
  @processed=false
end

Instance Attribute Details

#pmid_listObject (readonly)

Returns the value of attribute pmid_list.



62
63
64
# File 'lib/pmc/efetch.rb', line 62

def pmid_list
  @pmid_list
end

#pmid_xmlObject (readonly)

Returns the value of attribute pmid_xml.



63
64
65
# File 'lib/pmc/efetch.rb', line 63

def pmid_xml
  @pmid_xml
end

Instance Method Details

#processObject

NCBI request that the users should get 200 or less ids So, we use MAX_SLICE as maximum slice to make requests



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pmc/efetch.rb', line 71

def process
  return true if @processed
  @pmid_list.each_slice(MAX_SLICE) do |slice_pmid|
    process_slice(slice_pmid)

    #while out!=:ok
    #  out=process_slice(slice_pmid)
    #end
  end
  @processed
  #$log.info(@pmid_xml.keys)
end

#process_slice(slice_pmid) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pmc/efetch.rb', line 84

def process_slice(slice_pmid)
  url="#{BASE_URL}?tool=#{TOOL}&email=#{EMAIL}#{PMC.api_key}&db=pubmed&retmode=xml&id=#{slice_pmid.join(',')}"
  #$log.info(url)
  uri = URI(url)
  res = Net::HTTP.get_response(uri)
  #$log.info(res.body)
  begin
    xml_o=Nokogiri::XML(res.body)
  rescue
    xml_o=nil
  end
  #$log.info(res.body)
  if res.code!="200"
    raise EfetchResponseError, "Can't retrieve information for slice #{slice_pmid}. CODE: #{res.code}, Body:#{res.body}"
  else
    if xml_o
      @pmid_xml.push(xml_o)
      :ok
    else
      raise "Problem to get slice #{slice_pmid}"
    end
  end
end