Browse files

Merge pull request #20 from Mange/master

Proper encodings in Ruby 1.9
  • Loading branch information...
2 parents a463b24 + 57e84d5 commit 2df8a46ba67e3082e4f5515ec03edafc3c65c5aa @mwmitchell mwmitchell committed Jul 29, 2011
Showing with 17 additions and 9 deletions.
  1. +17 −9 lib/rsolr/connection.rb
View
26 lib/rsolr/connection.rb
@@ -3,7 +3,7 @@
# The default/Net::Http adapter for RSolr.
class RSolr::Connection
-
+
# using the request_context hash,
# send a request,
# then return the standard rsolr response hash {:status, :body, :headers}
@@ -13,17 +13,18 @@ def execute client, request_context
request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
begin
response = h.request request
- {:status => response.code.to_i, :headers => response.to_hash, :body => response.body}
+ charset = response.type_params["charset"]
+ {:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
raise(Errno::ECONNREFUSED.new) :
raise($!)
end
end
-
+
protected
-
+
# This returns a singleton of a Net::HTTP or Net::HTTP.Proxy request object.
def http uri, proxy = nil
@http ||= (
@@ -33,12 +34,12 @@ def http uri, proxy = nil
else
Net::HTTP.new uri.host, uri.port
end
- http.use_ssl = uri.port == 443 || uri.instance_of?(URI::HTTPS)
+ http.use_ssl = uri.port == 443 || uri.instance_of?(URI::HTTPS)
http
)
end
-
- #
+
+ #
def setup_raw_request request_context
http_method = case request_context[:method]
when :get
@@ -66,5 +67,12 @@ def setup_raw_request request_context
raw_request.initialize_http_header headers
raw_request
end
-
-end
+
+ private
+
+ def force_charset body, charset
+ return body unless charset and body.respond_to?(:force_encoding)
+ body.force_encoding(charset)
+ end
+
+end

0 comments on commit 2df8a46

Please sign in to comment.