From 332dcbe948340632c9422fa003996336f583f589 Mon Sep 17 00:00:00 2001
From: Tim Robertson <timaro@gmail.com>
Date: Tue, 29 Jan 2013 01:47:41 -0800
Subject: [PATCH] read and apply charset from Content-Type to geocoder raw
 response, if possible.

---
 lib/geocoder/lookups/base.rb | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index 4120fc70..5fe8d55e 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -194,6 +194,19 @@ module Geocoder
           check_api_key_configuration!(query)
           response = make_api_request(query)
           body = response.body
+
+          # apply the charset from the Content-Type header, if possible
+          ct = response['content-type']
+
+          if ct && ct['charset']
+            charset = ct.split(';').select do |s|
+              s['charset']
+            end.first.to_s.split('=')
+            if charset.length == 2
+              body.force_encoding(charset.last) rescue ArgumentError
+            end
+          end
+
           if cache and (200..399).include?(response.code.to_i)
             cache[key] = body
           end
-- 
GitLab