rsolr 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  A simple, extensible Ruby client for Apache Solr.
4
4
 
5
- Notice: This document is only for the the 1.0 in the master. The last pre-1.0 gem release documentation can be found here: http://github.com/mwmitchell/rsolr/tree/v0.12.1
5
+ Notice: This document is only for the the 1.0 in the master. The last pre 1.0 gem release documentation can be found here: http://github.com/mwmitchell/rsolr/tree/v0.12.1
6
6
 
7
7
  ==Documentation
8
- The code docs can be viewed here : http://rdoc.info/projects/mwmitchell/rsolr
8
+ The code docs for the last *release* can be viewed here : http://rdoc.info/projects/mwmitchell/rsolr
9
9
 
10
10
  == Installation:
11
- sudo gem install rsolr --pre
11
+ sudo gem install rsolr
12
12
 
13
13
  == Example:
14
14
  require 'rubygems'
@@ -184,7 +184,12 @@ The default response format is Ruby. When the :wt param is set to :ruby, the res
184
184
  * Send me a pull request. Bonus points for topic branches.
185
185
 
186
186
  ==Contributors
187
+ * Jonathan Rochkind
188
+ * Chris Beer
189
+ * Craig Smith
190
+ * Randy Souza
187
191
  * Colin Steele
192
+ * Peter Kieltyka
188
193
  * Lorenzo Riccucci
189
194
  * Mike Perham
190
195
  * Mat Brown
@@ -193,7 +198,7 @@ The default response format is Ruby. When the :wt param is set to :ruby, the res
193
198
  * Fouad Mardini
194
199
  * Jeremy Hinegardner
195
200
  * Nathan Witmer
196
- * Craig Smith
201
+ * "shima"
197
202
 
198
203
  ==Author
199
204
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/lib/rsolr/char.rb CHANGED
@@ -4,7 +4,7 @@ module RSolr::Char
4
4
  # backslash everything
5
5
  # that isn't a word character
6
6
  def escape value
7
- value.gsub /(\W)/, '\\\\\1'
7
+ value.gsub(/(\W)/, '\\\\\1')
8
8
  end
9
9
 
10
10
  # LUCENE_CHAR_RX = /([\+\-\!\(\)\[\]\^\"\~\*\?\:\\]+)/
data/lib/rsolr/client.rb CHANGED
@@ -3,6 +3,7 @@ class RSolr::Client
3
3
  attr_reader :connection, :uri, :proxy, :options
4
4
 
5
5
  def initialize connection, options = {}
6
+ @proxy = @uri = nil
6
7
  @connection = connection
7
8
  unless false === options[:url]
8
9
  url = options[:url] ? options[:url].dup : 'http://127.0.0.1:8983/solr/'
@@ -15,10 +16,9 @@ class RSolr::Client
15
16
  end
16
17
  end
17
18
  @options = options
18
- extend RSolr::Pagination::Client
19
19
  end
20
20
 
21
- # returns the actual request uri object.
21
+ # returns the request uri object.
22
22
  def base_request_uri
23
23
  base_uri.request_uri if base_uri
24
24
  end
@@ -26,7 +26,7 @@ class RSolr::Client
26
26
  # returns the uri proxy if present,
27
27
  # otherwise just the uri object.
28
28
  def base_uri
29
- @proxy || @uri
29
+ @proxy ? @proxy : @uri
30
30
  end
31
31
 
32
32
  # Create the get, post, and head methods
@@ -38,6 +38,16 @@ class RSolr::Client
38
38
  RUBY
39
39
  end
40
40
 
41
+ # A paginated request method.
42
+ # Converts the page and per_page
43
+ # arguments into "rows" and "start".
44
+ def paginate page, per_page, path, opts = nil
45
+ opts ||= {}
46
+ opts[:params] ||= {}
47
+ raise "'rows' or 'start' params should not be set when using +paginate+" if ["start", "rows"].include?(opts[:params].keys)
48
+ execute build_paginated_request(page, per_page, path, opts)
49
+ end
50
+
41
51
  # POST XML messages to /update with optional params.
42
52
  #
43
53
  # http://wiki.apache.org/solr/UpdateXmlMessages#add.2BAC8-update
@@ -187,10 +197,16 @@ class RSolr::Client
187
197
  opts
188
198
  end
189
199
 
200
+ def build_paginated_request page, per_page, path, opts
201
+ per_page = per_page.to_s.to_i
202
+ page = page.to_s.to_i-1
203
+ page = page < 1 ? 0 : page
204
+ opts[:params]["start"] = page * per_page
205
+ opts[:params]["rows"] = per_page
206
+ build_request path, opts
207
+ end
208
+
190
209
  # A mixin for used by #adapt_response
191
- # This module essentially
192
- # allows the raw response access to
193
- # the original response and request.
194
210
  module Context
195
211
  attr_accessor :request, :response
196
212
  end
@@ -208,20 +224,22 @@ class RSolr::Client
208
224
  def adapt_response request, response
209
225
  raise "The response does not have the correct keys => :body, :headers, :status" unless
210
226
  %W(body headers status) == response.keys.map{|k|k.to_s}.sort
211
- raise RSolr::Error::Http.new request, response unless
212
- [200,302].include? response[:status]
227
+ raise RSolr::Error::Http.new request, response unless [200,302].include? response[:status]
213
228
  result = request[:params][:wt] == :ruby ? evaluate_ruby_response(request, response) : response[:body]
214
229
  result.extend Context
215
- result.request = request
216
- result.response = response
217
- result
230
+ result.request, result.response = request, response
231
+ result.is_a?(Hash) ? result.extend(RSolr::Response) : result
218
232
  end
219
233
 
220
234
  protected
221
235
 
222
236
  # converts the method name for the solr request handler path.
223
237
  def method_missing name, *args
224
- send_and_receive name, *args
238
+ if name.to_s =~ /^paginated?_(.+)$/
239
+ paginate args[0], args[1], $1, *args[2..-1]
240
+ else
241
+ send_and_receive name, *args
242
+ end
225
243
  end
226
244
 
227
245
  # evaluates the response[:body],
@@ -3,7 +3,7 @@ require 'net/https'
3
3
 
4
4
  # The default/Net::Http adapter for RSolr.
5
5
  class RSolr::Connection
6
-
6
+
7
7
  # using the request_context hash,
8
8
  # send a request,
9
9
  # then return the standard rsolr response hash {:status, :body, :headers}
@@ -13,7 +13,8 @@ class RSolr::Connection
13
13
  request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
14
14
  begin
15
15
  response = h.request request
16
- {:status => response.code.to_i, :headers => response.to_hash, :body => response.body}
16
+ charset = response.type_params["charset"]
17
+ {:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
17
18
  # catch the undefined closed? exception -- this is a confirmed ruby bug
18
19
  rescue NoMethodError
19
20
  $!.message == "undefined method `closed?' for nil:NilClass" ?
@@ -21,9 +22,9 @@ class RSolr::Connection
21
22
  raise($!)
22
23
  end
23
24
  end
24
-
25
+
25
26
  protected
26
-
27
+
27
28
  # This returns a singleton of a Net::HTTP or Net::HTTP.Proxy request object.
28
29
  def http uri, proxy = nil
29
30
  @http ||= (
@@ -33,12 +34,12 @@ class RSolr::Connection
33
34
  else
34
35
  Net::HTTP.new uri.host, uri.port
35
36
  end
36
- http.use_ssl = uri.port == 443 || uri.instance_of?(URI::HTTPS)
37
+ http.use_ssl = uri.port == 443 || uri.instance_of?(URI::HTTPS)
37
38
  http
38
39
  )
39
40
  end
40
-
41
- #
41
+
42
+ #
42
43
  def setup_raw_request request_context
43
44
  http_method = case request_context[:method]
44
45
  when :get
@@ -66,5 +67,12 @@ class RSolr::Connection
66
67
  raw_request.initialize_http_header headers
67
68
  raw_request
68
69
  end
69
-
70
- end
70
+
71
+ private
72
+
73
+ def force_charset body, charset
74
+ return body unless charset and body.respond_to?(:force_encoding)
75
+ body.force_encoding(charset)
76
+ end
77
+
78
+ end
@@ -0,0 +1,51 @@
1
+ module RSolr::Response
2
+
3
+ def self.extended base
4
+ if base["response"] && base["response"]["docs"]
5
+ base["response"]["docs"].tap do |d|
6
+ d.extend PaginatedDocSet
7
+ d.per_page = base.request[:params]["rows"]
8
+ d.start = base.request[:params]["start"]
9
+ d.total = base["response"]["numFound"].to_s.to_i
10
+ end
11
+ end
12
+ end
13
+
14
+ # A response module which gets mixed into the solr ["response"]["docs"] array.
15
+ module PaginatedDocSet
16
+
17
+ attr_accessor :start, :per_page, :total
18
+
19
+ # Returns the current page calculated from 'rows' and 'start'
20
+ def current_page
21
+ return 1 if start < 1
22
+ per_page_normalized = per_page < 1 ? 1 : per_page
23
+ @current_page ||= (start / per_page_normalized).ceil + 1
24
+ end
25
+
26
+ # Calcuates the total pages from 'numFound' and 'rows'
27
+ def total_pages
28
+ @total_pages ||= per_page > 0 ? (total / per_page.to_f).ceil : 1
29
+ end
30
+
31
+ # returns the previous page number or 1
32
+ def previous_page
33
+ @previous_page ||= (current_page > 1) ? current_page - 1 : 1
34
+ end
35
+
36
+ # returns the next page number or the last
37
+ def next_page
38
+ @next_page ||= (current_page == total_pages) ? total_pages : current_page+1
39
+ end
40
+
41
+ def has_next?
42
+ current_page < total_pages
43
+ end
44
+
45
+ def has_previous?
46
+ current_page > 1
47
+ end
48
+
49
+ end
50
+
51
+ end
data/lib/rsolr/xml.rb CHANGED
@@ -73,7 +73,6 @@ module RSolr::Xml
73
73
  class Generator
74
74
 
75
75
  def build &block
76
- require 'builder'
77
76
  b = ::Builder::XmlMarkup.new(:indent => 0, :margin => 0, :encoding => 'UTF-8')
78
77
  b.instruct!
79
78
  block_given? ? yield(b) : b
data/lib/rsolr.rb CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
 
5
5
  module RSolr
6
6
 
7
- %W(Char Client Error Connection Pagination Uri Xml).each{|n|autoload n.to_sym, "rsolr/#{n.downcase}"}
7
+ %W(Response Char Client Error Connection Uri Xml).each{|n|autoload n.to_sym, "rsolr/#{n.downcase}"}
8
8
 
9
9
  def self.version
10
10
  @version ||= File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
@@ -1,38 +1,26 @@
1
1
  require 'spec_helper'
2
2
  describe "RSolr::Pagination" do
3
- context "calculate_start_and_rows" do
4
- it "should return an array with 2 ints" do
5
- values = RSolr::Pagination.calculate_start_and_rows 2, 10
6
- values[0].should == 10
7
- values[1].should == 10
8
- end
9
- it "should handle string values" do
10
- values = RSolr::Pagination.calculate_start_and_rows "1", "22"
11
- values[0].should == 0
12
- values[1].should == 22
13
- end
14
- end
15
3
  context "build_paginated_request" do
16
4
  it "should create the proper solr params and query string" do
17
- c = RSolr::Client.new(nil, {}).extend(RSolr::Pagination::Client)
5
+ c = RSolr::Client.new(nil, {})#.extend(RSolr::Pagination::Client)
18
6
  r = c.build_paginated_request 3, 25, "select", {:params => {:q => "test"}}
19
- r[:page].should == 3
20
- r[:per_page].should == 25
21
- r[:params][:start].should == 50
22
- r[:params][:rows].should == 25
7
+ #r[:page].should == 3
8
+ #r[:per_page].should == 25
9
+ r[:params]["start"].should == 50
10
+ r[:params]["rows"].should == 25
23
11
  r[:uri].query.should =~ /rows=25/
24
12
  r[:uri].query.should =~ /start=50/
25
13
  end
26
14
  end
27
15
  context "paginate" do
28
16
  it "should build a paginated request context and call execute" do
29
- c = RSolr::Client.new(nil, {}).extend(RSolr::Pagination::Client)
17
+ c = RSolr::Client.new(nil, {})#.extend(RSolr::Pagination::Client)
30
18
  c.should_receive(:execute).with(hash_including({
31
- :page => 1,
32
- :per_page => 10,
19
+ #:page => 1,
20
+ #:per_page => 10,
33
21
  :params => {
34
- :rows => 10,
35
- :start => 0,
22
+ "rows" => 10,
23
+ "start" => 0,
36
24
  :wt => :ruby
37
25
  }
38
26
  }))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsolr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 2
10
- version: 1.0.2
9
+ - 3
10
+ version: 1.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Mitchell
@@ -25,13 +25,74 @@ autorequire:
25
25
  bindir: bin
26
26
  cert_chain: []
27
27
 
28
- date: 2011-06-06 00:00:00 -04:00
29
- default_executable:
28
+ date: 2011-11-09 00:00:00 Z
30
29
  dependencies:
31
30
  - !ruby/object:Gem::Dependency
31
+ requirement: &id001 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - "="
35
+ - !ruby/object:Gem::Version
36
+ hash: 7
37
+ segments:
38
+ - 3
39
+ - 0
40
+ - 0
41
+ version: 3.0.0
42
+ version_requirements: *id001
32
43
  name: builder
33
44
  prerelease: false
34
- requirement: &id001 !ruby/object:Gem::Requirement
45
+ type: :runtime
46
+ - !ruby/object:Gem::Dependency
47
+ requirement: &id002 !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - "="
51
+ - !ruby/object:Gem::Version
52
+ hash: 7
53
+ segments:
54
+ - 1
55
+ - 6
56
+ - 4
57
+ version: 1.6.4
58
+ version_requirements: *id002
59
+ name: jeweler
60
+ prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ requirement: &id003 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - "="
67
+ - !ruby/object:Gem::Version
68
+ hash: 63
69
+ segments:
70
+ - 0
71
+ - 9
72
+ - 2
73
+ version: 0.9.2
74
+ version_requirements: *id003
75
+ name: rake
76
+ prerelease: false
77
+ type: :development
78
+ - !ruby/object:Gem::Dependency
79
+ requirement: &id004 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - "="
83
+ - !ruby/object:Gem::Version
84
+ hash: 43
85
+ segments:
86
+ - 3
87
+ - 9
88
+ - 4
89
+ version: 3.9.4
90
+ version_requirements: *id004
91
+ name: rdoc
92
+ prerelease: false
93
+ type: :development
94
+ - !ruby/object:Gem::Dependency
95
+ requirement: &id005 !ruby/object:Gem::Requirement
35
96
  none: false
36
97
  requirements:
37
98
  - - ">="
@@ -42,8 +103,10 @@ dependencies:
42
103
  - 1
43
104
  - 2
44
105
  version: 2.1.2
106
+ version_requirements: *id005
107
+ name: builder
108
+ prerelease: false
45
109
  type: :runtime
46
- version_requirements: *id001
47
110
  description: RSolr aims to provide a simple and extensible library for working with Solr
48
111
  email: goodieboy@gmail.com
49
112
  executables: []
@@ -63,7 +126,7 @@ files:
63
126
  - lib/rsolr/client.rb
64
127
  - lib/rsolr/connection.rb
65
128
  - lib/rsolr/error.rb
66
- - lib/rsolr/pagination.rb
129
+ - lib/rsolr/response.rb
67
130
  - lib/rsolr/uri.rb
68
131
  - lib/rsolr/xml.rb
69
132
  - spec/api/char_spec.rb
@@ -78,7 +141,6 @@ files:
78
141
  - Rakefile
79
142
  - tasks/spec.rake
80
143
  - tasks/rdoc.rake
81
- has_rdoc: true
82
144
  homepage: http://github.com/mwmitchell/rsolr
83
145
  licenses: []
84
146
 
@@ -108,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
170
  requirements: []
109
171
 
110
172
  rubyforge_project:
111
- rubygems_version: 1.6.2
173
+ rubygems_version: 1.8.10
112
174
  signing_key:
113
175
  specification_version: 3
114
176
  summary: A Ruby client for Apache Solr
@@ -1,122 +0,0 @@
1
- module RSolr::Pagination
2
-
3
- # Calculates the "start" and "rows" Solr params
4
- # by inspecting the :per_page and :page params.
5
- def self.calculate_start_and_rows page, per_page
6
- per_page = per_page.to_s.to_i
7
- page = page.to_s.to_i-1
8
- page = page < 1 ? 0 : page
9
- start = page * per_page
10
- [start, per_page]
11
- end
12
-
13
- # A mixin module for RSolr::Client
14
- # -- note, this must mixed-in via
15
- # "extend" on a RSolr::Client instance.
16
- module Client
17
-
18
- # A paginated request method.
19
- def paginate page, per_page, path, opts = nil
20
- @warned ||= begin
21
- warn "DEPRECATION WARNING: RSolr::Pagination / pagination functionality will be removed in 1.1.0."
22
- true
23
- end
24
- request_context = build_paginated_request page, per_page, path, opts
25
- execute request_context
26
- end
27
-
28
- # Just like RSolr::Client #build_request
29
- # but converts the page and per_page
30
- # arguments into :rows and :start.
31
- def build_paginated_request page, per_page, path, opts = nil
32
- opts ||= {}
33
- opts[:page] = page
34
- opts[:per_page] = per_page
35
- opts[:params] ||= {}
36
- values = RSolr::Pagination.calculate_start_and_rows(page, per_page)
37
- opts[:params][:start] = values[0]
38
- opts[:params][:rows] = values[1]
39
- build_request path, opts
40
- end
41
-
42
- protected
43
-
44
- # Checks if the called method starts
45
- # with "paginate_*" and
46
- # converts the * to the solr
47
- # request path. It then calls paginate
48
- # with the appropriate arguments.
49
- # If the called method doesn't
50
- # start with "paginate_",
51
- # the original/super
52
- # RSolr::Client #method_missing
53
- # method is called.
54
- def method_missing name, *args
55
- if name.to_s =~ /^paginated?_(.+)$/
56
- paginate args[0], args[1], $1, *args[2..-1]
57
- else
58
- super name, *args
59
- end
60
- end
61
-
62
- # Overrides the RSolr::Client #evaluate_ruby_response method.
63
- # Calls the original/super
64
- # RSolr::Client #evaluate_ruby_response method.
65
- # Mixes in the PaginatedDocSet if
66
- # the request[:page] and request[:per_page]
67
- # opts are set.
68
- def evaluate_ruby_response request, response
69
- result = super request, response
70
- if request[:page] && request[:per_page] && result["response"] && result["response"]["docs"]
71
- d = result['response']['docs'].extend PaginatedDocSet
72
- d.per_page = request[:per_page]
73
- d.start = request[:params][:start]
74
- d.total = result["response"]["numFound"].to_s.to_i
75
- end
76
- result
77
- end
78
-
79
- end
80
-
81
- # A response module which gets mixed into the solr ["response"]["docs"] array.
82
- module PaginatedDocSet
83
-
84
- attr_accessor :start, :per_page, :total
85
-
86
- # Returns the current page calculated from 'rows' and 'start'
87
- # WillPaginate hook
88
- def current_page
89
- return 1 if start < 1
90
- per_page_normalized = per_page < 1 ? 1 : per_page
91
- @current_page ||= (start / per_page_normalized).ceil + 1
92
- end
93
-
94
- # Calcuates the total pages from 'numFound' and 'rows'
95
- # WillPaginate hook
96
- def total_pages
97
- @total_pages ||= per_page > 0 ? (total / per_page.to_f).ceil : 1
98
- end
99
-
100
- # returns the previous page number or 1
101
- # WillPaginate hook
102
- def previous_page
103
- @previous_page ||= (current_page > 1) ? current_page - 1 : 1
104
- end
105
-
106
- # returns the next page number or the last
107
- # WillPaginate hook
108
- def next_page
109
- @next_page ||= (current_page == total_pages) ? total_pages : current_page+1
110
- end
111
-
112
- def has_next?
113
- current_page < total_pages
114
- end
115
-
116
- def has_previous?
117
- current_page > 1
118
- end
119
-
120
- end
121
-
122
- end