rsolr 0.12.0 → 1.0.3

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.
metadata CHANGED
@@ -1,19 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsolr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 3
10
+ version: 1.0.3
5
11
  platform: ruby
6
12
  authors:
7
13
  - Matt Mitchell
14
+ - Jeremy Hinegardner
15
+ - Mat Brown
16
+ - Mike Perham
17
+ - Nathan Witmer
18
+ - Peter Kieltyka
19
+ - Randy Souza
20
+ - shairon toledo
21
+ - shima
22
+ - Chris Beer
23
+ - Jonathan Rochkind
8
24
  autorequire:
9
25
  bindir: bin
10
26
  cert_chain: []
11
27
 
12
- date: 2010-02-03 00:00:00 -05:00
13
- default_executable:
14
- dependencies: []
15
-
16
- description: RSolr is a Ruby gem for working with Apache Solr!
28
+ date: 2011-11-09 00:00:00 Z
29
+ dependencies:
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
43
+ name: builder
44
+ prerelease: false
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
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 15
101
+ segments:
102
+ - 2
103
+ - 1
104
+ - 2
105
+ version: 2.1.2
106
+ version_requirements: *id005
107
+ name: builder
108
+ prerelease: false
109
+ type: :runtime
110
+ description: RSolr aims to provide a simple and extensible library for working with Solr
17
111
  email: goodieboy@gmail.com
18
112
  executables: []
19
113
 
@@ -22,23 +116,31 @@ extensions: []
22
116
  extra_rdoc_files:
23
117
  - LICENSE
24
118
  - README.rdoc
25
- - CHANGES.txt
26
119
  files:
27
- - CHANGES.txt
28
- - lib/rsolr/client.rb
29
- - lib/rsolr/connection/net_http.rb
30
- - lib/rsolr/connection/requestable.rb
31
- - lib/rsolr/connection/utils.rb
32
- - lib/rsolr/connection.rb
33
- - lib/rsolr/message/document.rb
34
- - lib/rsolr/message/field.rb
35
- - lib/rsolr/message/generator.rb
36
- - lib/rsolr/message.rb
37
- - lib/rsolr.rb
38
120
  - LICENSE
39
121
  - README.rdoc
40
- - rsolr.gemspec
41
- has_rdoc: true
122
+ - VERSION
123
+ - lib/rsolr-direct.rb
124
+ - lib/rsolr.rb
125
+ - lib/rsolr/char.rb
126
+ - lib/rsolr/client.rb
127
+ - lib/rsolr/connection.rb
128
+ - lib/rsolr/error.rb
129
+ - lib/rsolr/response.rb
130
+ - lib/rsolr/uri.rb
131
+ - lib/rsolr/xml.rb
132
+ - spec/api/char_spec.rb
133
+ - spec/api/client_spec.rb
134
+ - spec/api/connection_spec.rb
135
+ - spec/api/error_spec.rb
136
+ - spec/api/pagination_spec.rb
137
+ - spec/api/rsolr_spec.rb
138
+ - spec/api/uri_spec.rb
139
+ - spec/api/xml_spec.rb
140
+ - spec/spec_helper.rb
141
+ - Rakefile
142
+ - tasks/spec.rake
143
+ - tasks/rdoc.rake
42
144
  homepage: http://github.com/mwmitchell/rsolr
43
145
  licenses: []
44
146
 
@@ -48,23 +150,40 @@ rdoc_options: []
48
150
  require_paths:
49
151
  - lib
50
152
  required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
51
154
  requirements:
52
155
  - - ">="
53
156
  - !ruby/object:Gem::Version
157
+ hash: 3
158
+ segments:
159
+ - 0
54
160
  version: "0"
55
- version:
56
161
  required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
57
163
  requirements:
58
164
  - - ">="
59
165
  - !ruby/object:Gem::Version
166
+ hash: 3
167
+ segments:
168
+ - 0
60
169
  version: "0"
61
- version:
62
170
  requirements: []
63
171
 
64
172
  rubyforge_project:
65
- rubygems_version: 1.3.5
173
+ rubygems_version: 1.8.10
66
174
  signing_key:
67
175
  specification_version: 3
68
176
  summary: A Ruby client for Apache Solr
69
- test_files: []
70
-
177
+ test_files:
178
+ - spec/api/char_spec.rb
179
+ - spec/api/client_spec.rb
180
+ - spec/api/connection_spec.rb
181
+ - spec/api/error_spec.rb
182
+ - spec/api/pagination_spec.rb
183
+ - spec/api/rsolr_spec.rb
184
+ - spec/api/uri_spec.rb
185
+ - spec/api/xml_spec.rb
186
+ - spec/spec_helper.rb
187
+ - Rakefile
188
+ - tasks/spec.rake
189
+ - tasks/rdoc.rake
data/CHANGES.txt DELETED
@@ -1,282 +0,0 @@
1
- 0.12.0 - February 3, 2010
2
- Removed adapters for xml and connections (these will be provided via separate gems)
3
- - default xml generator is Builder
4
- - default http connection is Net::Http
5
- Removed JRuby/Direct connection stuff (this will be in a new library)
6
- Updated specs
7
-
8
- 0.11.0 - November 17, 2009
9
- Removed pagination feature yet again... keeping it in RSolr::Ext until a better API can be thought up.
10
- Updated Xout with fixed version - thanks to Mat Brown
11
- - bug in escaping consecutive special characters
12
- - moved regexp patterns out of loop, into "static" variables instead
13
-
14
- 0.10.1 - November 13, 2009
15
- Same changes as 0.10.0 -- but fixing version number problems
16
-
17
- 0.10.0 - November 13, 2009
18
- Changed RSolr.connect to return Http connection only:
19
- RSolr.connect :url=>'xxx'
20
- Added RSolr.direct_connect to handle the creation of SolrDirectConnections
21
- rsolr = RSolr.direct_connect :home_dir=>''
22
-
23
- # can also use a block so the connection will get closed automatically:
24
- RSolr.direct_connect :home_dir=>'' do |rsolr|
25
- # query...
26
- end
27
- Added paginate method to RSolr::Client
28
- - This method requires 2 new arguments: current page and per page:
29
- solr.paginate 1, 10, :q=>''
30
- - handler paths can be set:
31
- solr.paginate 1, 10, '/music', :q=>''
32
- - handler paths can also be set by method names:
33
- solr.paginate_music 1, 10
34
- Removed :dist_dir option from Direct connection options -- you gotta load your own java libs. See examples/direct.rb
35
- Updated specs
36
-
37
- 0.9.7.2 - November 6, 2009
38
- fixed gem spec, added client.rb
39
-
40
- 0.9.7.1 - November 5, 2009
41
- added support for multibyte url characters in RSolr::Connection::Utils
42
- - this is because Ruby 1.9's String size method is different than 1.8
43
-
44
- 0.9.7 - October 23, 2009
45
- Removed XML message builders - using pure Ruby generator instead
46
- - benchmarks show generation speed is a little faster than libxml
47
- - minimal xml escaping so binary posting to Solr should no longer be a problem.
48
- Changed response.adapter_resonse to response.raw
49
- Removed HTTP adapters - sticking with NetHTTP
50
- Removed builder dependency in gemspec
51
- Removed Adapter and HTTPClient modules
52
- Moved HTTPCLient::Util to Connection::Utils
53
- Updated all tests
54
-
55
- 0.9.6 - September 9, 2009
56
- Added ability to create direct connections from existing Java::OrgApacheSolrCore::SolrCore
57
- Added ability to send queries using POST
58
- - solr.request '/select', :q=>'*:*', :method=>:post
59
-
60
- 0.9.5 - September 4, 2009
61
- Removed Array #extract_options!
62
- Removed the Connection #send_request method (now #request)
63
- Removed the Connection #select method -- still works, but this now ends up calling Connection #method_missing
64
- Removed HTTPClient::Connector
65
- - HTTPClient now uses a method called #connect (like RSolr.connect)
66
- - This method accepts 1 or 2 args, see the #connect method comments
67
- Changed the way the HTTP client adapter is set. Now just pass in when connecting:
68
- - RSolr.connect(:http, :adapter=>curb, :url=>'http://solr.com')
69
- Moved Message::Builders to Message::Adapter
70
- Made Connection a module and moved class methods to "Base" class
71
- Made HTTPClient a module and moved class methods to "Base" class
72
- Removed the "adapter.rb" files -- Adapter modules are defined in the parent module file.
73
- Moved Message module/singleton functionality to Message::Builder class
74
- XML message adapter API change: no longer a singleton, must instantiate Message::Builder
75
- Made RSolr::Connection #message public -- can change the adapter as needed; connection.message.adapter = RSolr::Message::Adapter::LibXML.new
76
- More tests for HTTPClient::Util
77
- Simplified url/query building in HTTPClient::Util
78
- Updated tests accordingly
79
-
80
- 0.9.1 - July 22, 2009
81
- Added LibXml builder support (Thanks to Mat Brown - github.com/outoftime)
82
-
83
- 0.9.0 - July 17, 2009
84
- Added ability to use DirectSolrConnection instance when connecting
85
- Loading Java classes using full package name
86
-
87
- 0.8.9 - June 30, 2009
88
- Deprecated hash syntax fix from outoftime/rsolr (Mat Brown)
89
-
90
- 0.8.8 - May 6, 2009
91
- Added method :request to RSolr::Connection
92
- - this method is an alias for send_request
93
-
94
- 0.8.7 - May 6, 2009
95
- Added method_missing to RSolr::Connection, which sets the request handler path,
96
- based on the method name:
97
- # this sends a request like: /music_library?q=>coltrane
98
- solr.music_library :q=>'coltrane'
99
- Removed the core_ext file, put the Array extension code directly into rsolr.rb
100
-
101
- 0.8.6 - April 25, 2009
102
- Removed the Mash stuff -- which means the response keys from solr are only accessable using String based keys.
103
- Use RSolr::Ext if you want more magic.
104
-
105
- 0.8.5 - April 7, 2009
106
- The RSolr::Message #add method now accepts a single Message::Document or an array of Message::Document objects
107
- The Message::Document class has a new method: #add_field
108
- Tests added for both changes
109
- Permissions of files changed to non-executable
110
- -- the above changes were made by Mat Brown, thank you Mat!
111
- Added CannedSolrResponse module for mocks in the specs... not used yet
112
-
113
- 0.8.4 - April 3, 2009
114
- New test suite using RSpec and mocks coming. Run "rake spec"
115
- - added specs for RSolr and RSolr::Connection
116
-
117
- 0.8.3 - April 3, 2009
118
- RSolr::Connection
119
- - removed the block functionality of send_request and related methods
120
- - this was used to gain access to the raw adapter response
121
- - added #adapter_response method to all response objects
122
- - this is used to get access to the original adapter response:
123
- response = rsolr.select(:q=>'test')
124
- response.adapter_response[:status_code]
125
-
126
- 0.8.2 - March 24, 2009
127
- Changed RSolr.connect method to accept one options hash argument
128
- - This hash gets passed to the Connection object and the adapter
129
- Updated tests, examples and README.rdoc to reflect this change
130
- Bumped the version up in gemspec and the RSolr module
131
-
132
- 0.8.1 - March 12, 2009
133
- Added RSolr.escape and RSolr::Connection.new.escape
134
- - tests in rsolr_test
135
- Added ability to set doc and field attributes when adding documents via Message.add
136
-
137
- 0.8.0 - March 6, 2009
138
- Removed all response wrapper classes (now returning a simple hash for ruby responses)
139
- Removed RSolr::Query - this library needs an external partner lib, RSolrExt etc..
140
- changed query method to select
141
- added send_request method to Connection for custom requests:
142
- send_request '/my-handler', {:start=>0}, post_data=nil
143
- moved Connection::Base to Connection
144
- moved Connection::Adapter::* to Adapter::*
145
-
146
- 0.7.1 - February 27, 2009
147
- Added simple query helper module -> RSolr::Query
148
- Added tests for RSolr::Query
149
- Modified Test::Unit::TestCase in test/test_helpers.rb
150
-
151
- 0.7.0 - February 20, 2009
152
- Removed all param mapping behavior, code and tests
153
- - this stuff just gunks up rsolr and should be in an extension of some sort
154
- Can now specify the request handler in all RSolr::Connection::Base methods as the first argument:
155
- - solr.query 'select', :q=>'ipod'
156
- - solr.query 'catalog', :q=>'humphry'
157
- - solr.query :q=>'big' # defaults to the /select handler
158
-
159
- 0.6.9 - January 29, 2009
160
- Simplified facet response methods
161
- Main facet method is called #facets
162
- - returns an array of Response::Facet instances
163
- - each Facet instance has field and values attributes
164
- -- the values attribute is an array with FacetValue instances which have @value and @hits
165
- Added ability to set Connection::Base @param_adapters using :dismax or :standard
166
- instead of full class constant
167
- updated comments for #search method
168
- Updated tests
169
- Bumped up version
170
-
171
- 0.6.8 - January 28, 2009
172
- New method added to RSolr::Connection::Base - #find_values_for_facet
173
- This method searches for facet values only, and sets the :rows param to 0
174
- - returns an RSolr::Response::Query::Base instance
175
- Example:
176
- search_params[:facets][:offset]=0
177
- search_params[:facets][:limit]=5
178
- response = solr.search_facet_by_name(:language_facet, search_params)
179
-
180
- 0.6.7 - January 27, 2009
181
- The Symbol extension in core_ext.rb was cause for some REALLY painful debuging - so I removed it :(
182
- This means no more :q.alt or :facet.field until RSolr gets a really nice query-builder module happening.
183
-
184
- 0.6.6 - January 26, 2009
185
- Added #get method helper to RSolr::Response::Query::DocExt
186
- # doc.get(key, opts)
187
- # key is the name of the field
188
- # opts is a hash with the following valid keys:
189
- # - :sep - a string used for joining multivalued field values
190
- # - :default - a value to return when the key doesn't exist
191
- # if :sep is nil and the field is a multivalued field, the array is returned
192
-
193
- 0.6.5 - January 26, 2009
194
- Removed to_mash everywhere, except for usage in RSolr::Response
195
- Added a #close method to the Direct adapter
196
- - this closes the connection and sets the @connection variable to nil
197
- Removed to_mash in RSolr::Connection::Base
198
- Changed RSolr::Response::Query::Doc to DocExt
199
- - this no longer extends Mash
200
- - each doc in the response now uses the #extend method to mixin the new DocExt module
201
- Added #teardown method in direct connection test, this method closes the connection
202
- Updated the connection test methods a bit
203
-
204
- 0.6.4 - January 26, 2009
205
- Updated the mapping output for the :filters and :phrase_filters (when using the #search method)
206
- - now sending multiple fq's instead of one
207
- Updated mapping tests
208
-
209
- 0.6.3 - January 21, 2009
210
- Added a new param mapping module: RSolr::Connection::ParamMapping
211
- Mapping only for fields that need conversion/escaping or nested (facet.*) etc.
212
- This new module can be activated by using the #search method
213
- New tests for ParamMapping
214
-
215
- 0.6.2 - January 14, 2009
216
- Removed mapping and indexer modules -- seems to me that a general purpose mapping library
217
- would be more valuable than an embedded module in RSolr ( RMapper ?)
218
- This helps lighten RSolr a bit too
219
-
220
- 0.6.1 - January 13, 2009
221
- Removed SearchExt and mapping until this library gets some real use
222
- The only param mapping now is for :page and :per_page via the #search method
223
- The Connection::Base #query method does NO mapping now
224
-
225
- 0.6.0 - January 9, 2009
226
- Removed first argument from RSolr.connect, the "adapter_name"
227
- The adapter is now set using the :adapter key when passing in options to RSolr.connect:
228
- s = RSolr.connect(:adapter=>:direct)
229
-
230
- 0.5.9 - January 7, 2009
231
- Finally brought in the ExtLib Mash classes for incoming params and response hashes from solr
232
- - this gives the ability to access/set values for hashes using strings OR symbols (HashWithIndifferentAccess)
233
- Organized response tests
234
- Added more response tests
235
- Simplified the RSolr::Response::Base class by supporting only raw/string ruby input
236
- Added method to Response::IndexInfo for grabbing a Solr field list
237
-
238
- 0.5.7 - January 5, 2009
239
- Changed name from Solr to RSolr, changed all references to Solr to RSolr
240
- Added new tests for RSolr::Mapper and RSolr::Message
241
-
242
- 0.5.6 - December 30, 2008
243
- solr.gemspec cleanedup thanks to shairontoledo on github! :)
244
- Added Solr::Response::Query::Facet module with helpers from the delsolr project
245
- Also added test stub in test/connection/search_ext_test_methods.rb
246
- Fixed pagination math errors
247
- Added new SearchExt helper field: :phrase_filters
248
- This will add quoted values to the :filters (fq solr param) hash for doing easier facet requests
249
-
250
- Be sure to check out the new demo app: http://github.com/mwmitchell/consuminator/tree/master
251
-
252
- 0.5.5 - December 29, 2008
253
- Fixed bug where accessing a field by method name failed:
254
- docs.each do |doc|
255
- doc.timestamp
256
- end
257
- Fixed bug where using the #has? method on a doc failed:
258
- docs.each do |doc|
259
- doc.has?('timestamp')
260
- end
261
- Removed invalid autoload in Solr module
262
- Fixed spelling error in Solr::Connection::SearchExt (thanks to matthewrudy)
263
-
264
- 0.5.4 - December 29, 2008
265
- Re-organized the main Solr adapters, they're now in Solr::Connection::Adapter instead of Solr::Adapter
266
- All responses from HTTPClient and Connection::Adapter::Direct return a hash with the following keys:
267
- :status_code
268
- :body
269
- :params
270
- :url
271
- :path
272
- :headers
273
- :data
274
- This hash is now available in the solr response objects as #source - this will be useful in testing and debugging by allowing you to see the generated params and queries... example:
275
- response = Solr.query(:q=>'*:*')
276
- response.source[:params]
277
- response.source[:body]
278
- response.source[:url]
279
- Added MultiValue field support in Solr::Message, thanks to Fouad Mardini
280
- Bug in Solr::Connection::SearchExt where the :q params was not getting generated - fixed by Fouad Mardini
281
- Organized tests a bit, moved connection tests into test/connection
282
- Fixed a bug in Solr::Connection::Adapter::HTTP where invalid HTTP POST headers were being generated
@@ -1,48 +0,0 @@
1
- require 'net/http'
2
-
3
- #
4
- # Connection for standard HTTP Solr server
5
- #
6
- class RSolr::Connection::NetHttp
7
-
8
- include RSolr::Connection::Requestable
9
-
10
- def connection
11
- @connection ||= Net::HTTP.new(@uri.host, @uri.port)
12
- end
13
-
14
- def get path, params={}
15
- url = self.build_url path, params
16
- net_http_response = self.connection.get url
17
- create_http_context net_http_response, url, path, params
18
- end
19
-
20
- def post path, data, params={}, headers={}
21
- url = self.build_url path, params
22
- net_http_response = self.connection.post url, data, headers
23
- create_http_context net_http_response, url, path, params, data, headers
24
- end
25
-
26
- def create_http_context net_http_response, url, path, params, data=nil, headers={}
27
- full_url = "#{@uri.scheme}://#{@uri.host}"
28
- full_url += @uri.port ? ":#{@uri.port}" : ''
29
- full_url += url
30
- {
31
- :status_code=>net_http_response.code.to_i,
32
- :url=>full_url,
33
- :body=> encode_utf8(net_http_response.body),
34
- :path=>path,
35
- :params=>params,
36
- :data=>data,
37
- :headers=>headers,
38
- :message => net_http_response.message
39
- }
40
- end
41
-
42
- # accepts a path/string and optional hash of query params
43
- def build_url path, params={}
44
- full_path = @uri.path + path
45
- super full_path, params, @uri.query
46
- end
47
-
48
- end
@@ -1,43 +0,0 @@
1
- # A module that defines the interface and top-level logic for http based connection classes.
2
- module RSolr::Connection::Requestable
3
-
4
- include RSolr::Connection::Utils
5
-
6
- attr_reader :opts, :uri
7
-
8
- # opts can have:
9
- # :url => 'http://localhost:8080/solr'
10
- def initialize opts={}
11
- opts[:url] ||= 'http://127.0.0.1:8983/solr'
12
- @opts = opts
13
- @uri = URI.parse opts[:url]
14
- end
15
-
16
- # send a request to the connection
17
- # request '/select', :q=>'*:*'
18
- #
19
- # request '/update', {:wt=>:xml}, '</commit>'
20
- #
21
- # force a post where the post body is the param query
22
- # request '/update', "<optimize/>", :method=>:post
23
- #
24
- def request path, params={}, *extra
25
- opts = extra[-1].kind_of?(Hash) ? extra.pop : {}
26
- data = extra[0]
27
- # force a POST, use the query string as the POST body
28
- if opts[:method] == :post and data.to_s.empty?
29
- http_context = self.post(path, hash_to_query(params), {}, {'Content-Type' => 'application/x-www-form-urlencoded'})
30
- else
31
- if data
32
- # standard POST, using "data" as the POST body
33
- http_context = self.post(path, data, params, {"Content-Type" => 'text/xml; charset=utf-8'})
34
- else
35
- # standard GET
36
- http_context = self.get(path, params)
37
- end
38
- end
39
- raise RSolr::RequestError.new("Solr Response: #{http_context[:message]}") unless http_context[:status_code] == 200
40
- http_context
41
- end
42
-
43
- end
@@ -1,73 +0,0 @@
1
- # Helpful utility methods for building queries to a Solr server
2
- # This includes helpers that the Direct connection can use.
3
- module RSolr::Connection::Utils
4
-
5
- # Performs URI escaping so that you can construct proper
6
- # query strings faster. Use this rather than the cgi.rb
7
- # version since it's faster. (Stolen from Rack).
8
- def escape(s)
9
- s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
10
- #'%'+$1.unpack('H2'*$1.size).join('%').upcase
11
- '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
12
- }.tr(' ', '+')
13
- end
14
-
15
- # encodes the string as utf-8 in Ruby 1.9
16
- # returns the unaltered string in Ruby 1.8
17
- def encode_utf8 string
18
- (string.respond_to?(:force_encoding) and string.respond_to?(:encoding)) ?
19
- string.force_encoding(Encoding::UTF_8) : string
20
- end
21
-
22
- # Return the bytesize of String; uses String#length under Ruby 1.8 and
23
- # String#bytesize under 1.9.
24
- if ''.respond_to?(:bytesize)
25
- def bytesize(string)
26
- string.bytesize
27
- end
28
- else
29
- def bytesize(string)
30
- string.size
31
- end
32
- end
33
-
34
- # creates and returns a url as a string
35
- # "url" is the base url
36
- # "params" is an optional hash of GET style query params
37
- # "string_query" is an extra query string that will be appended to the
38
- # result of "url" and "params".
39
- def build_url url='', params={}, string_query=''
40
- queries = [string_query, hash_to_query(params)]
41
- queries.delete_if{|i| i.to_s.empty?}
42
- url += "?#{queries.join('&')}" unless queries.empty?
43
- url
44
- end
45
-
46
- # converts a key value pair to an escaped string:
47
- # Example:
48
- # build_param(:id, 1) == "id=1"
49
- def build_param(k,v)
50
- "#{escape(k)}=#{escape(v)}"
51
- end
52
-
53
- #
54
- # converts hash into URL query string, keys get an alpha sort
55
- # if a value is an array, the array values get mapped to the same key:
56
- # hash_to_query(:q=>'blah', :fq=>['blah', 'blah'], :facet=>{:field=>['location_facet', 'format_facet']})
57
- # returns:
58
- # ?q=blah&fq=blah&fq=blah&facet.field=location_facet&facet.field=format.facet
59
- #
60
- # if a value is empty/nil etc., it is not added
61
- def hash_to_query(params)
62
- mapped = params.map do |k, v|
63
- next if v.to_s.empty?
64
- if v.class == Array
65
- hash_to_query(v.map { |x| [k, x] })
66
- else
67
- build_param k, v
68
- end
69
- end
70
- mapped.compact.join("&")
71
- end
72
-
73
- end