entrez 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module Entrez
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
data/lib/entrez.rb CHANGED
@@ -21,9 +21,12 @@ class Entrez
21
21
  end
22
22
 
23
23
  # E.g. Entrez.ESearch('genomeprj', {WORD: 'hapmap', SEQS: 'inprogress'}, retmode: :xml)
24
+ # returns response. For convenience, response.ids() returns array of ID integers from result set.
24
25
  def ESearch(db, search_terms = {}, params = {})
25
26
  params[:term] = convert_search_term_hash(search_terms)
26
- perform '/esearch.fcgi', db, params
27
+ response = perform '/esearch.fcgi', db, params
28
+ parse_ids_and_extend response if response[:retmode].nil? || response[:retmode] == :xml
29
+ response
27
30
  end
28
31
 
29
32
  # E.g. Entrez.ESummary('snp', id: 123, retmode: :xml)
@@ -64,6 +67,15 @@ class Entrez
64
67
  end.join('+AND+')
65
68
  end
66
69
 
70
+ # Define ids() method which will parse and return the IDs from the XML response.
71
+ def parse_ids_and_extend(response)
72
+ response.instance_eval do
73
+ def ids
74
+ @ids ||= self['eSearchResult']['IdList']['Id'].map(&:to_i)
75
+ end
76
+ end
77
+ end
78
+
67
79
  end
68
80
 
69
81
  end
@@ -5,8 +5,11 @@ QueryStringNormalizer = proc do |query_hash|
5
5
  when Array
6
6
  value.join(',')
7
7
  else
8
- value
8
+ # If value is a string, it will be frozen, so dup it.
9
+ value.to_s.dup
9
10
  end
11
+ # Escape spaces.
12
+ value_string.gsub!(' ', '%20')
10
13
  "#{key}=#{value_string}"
11
14
  end.join('&')
12
15
  end
data/spec/entrez_spec.rb CHANGED
@@ -27,6 +27,11 @@ describe Entrez do
27
27
  response.body.should include('<Name>RS</Name>')
28
28
  end
29
29
 
30
+ it '#ESearch response returns IDs for convenience' do
31
+ response = Entrez.ESearch('genomeprj', {WORD: 'hapmap', SEQS: 'inprogress'}, retmode: :xml)
32
+ response.ids.should == [60153, 29429, 28911, 48101, 59851, 59849, 59847, 59845, 59839, 59835, 59833, 59831, 51895, 59829, 59827, 60835, 59811, 60831, 60819, 33895]
33
+ end
34
+
30
35
  it 'should respect query limit' do
31
36
  requests = proc { 4.times { Entrez.EFetch('taxonomy', id: 9606) } }
32
37
  requests.should take_longer_than(1.0)
@@ -2,9 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe QueryStringNormalizer do
4
4
 
5
- it 'does not escape' do
5
+ it 'does not escape square brackets' do
6
6
  query = {term: '[asdf]', db: 'snp'}
7
7
  QueryStringNormalizer.call(query).should == 'term=[asdf]&db=snp'
8
8
  end
9
9
 
10
+ it 'escapes spaces' do
11
+ query = {term: 'a b c'}
12
+ QueryStringNormalizer.call(query).should == 'term=a%20b%20c'
13
+ end
14
+
10
15
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: entrez
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.0
5
+ version: 0.5.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jared Ning
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-28 00:00:00 -05:00
13
+ date: 2011-05-29 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency