From c439bdf4e8bb031c4624dac2a04cb0ef569b9bd7 Mon Sep 17 00:00:00 2001
From: Rob Murray <robmurray17@gmail.com>
Date: Thu, 22 May 2014 15:23:57 +0100
Subject: [PATCH] Handle responses that only have one component in the Location
 field, assume this is a county and so return blank city

---
 lib/geocoder/results/postcode_anywhere_uk.rb             | 2 +-
 .../postcode_anywhere_uk_geocode_v2_00_hampshire         | 1 +
 test/unit/lookups/postcode_anywhere_uk_test.rb           | 9 +++++++++
 3 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire

diff --git a/lib/geocoder/results/postcode_anywhere_uk.rb b/lib/geocoder/results/postcode_anywhere_uk.rb
index 9b402d8d..abf13f71 100644
--- a/lib/geocoder/results/postcode_anywhere_uk.rb
+++ b/lib/geocoder/results/postcode_anywhere_uk.rb
@@ -21,7 +21,7 @@ module Geocoder::Result
     def city
       # is this too big a jump to assume that the API always
       # returns a City, County as the last elements?
-      city = @data['Location'].split(',')[-2]
+      city = @data['Location'].split(',')[-2] || blank_result
       city.strip
     end
 
diff --git a/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire b/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire
new file mode 100644
index 00000000..6eb2a4a6
--- /dev/null
+++ b/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire
@@ -0,0 +1 @@
+[{"Location":"Hampshire","Easting":"448701","Northing":"126642","Latitude":"51.037","Longitude":"-1.3068","OsGrid":"SU 48701 26642","Accuracy":"Standard"}]
\ No newline at end of file
diff --git a/test/unit/lookups/postcode_anywhere_uk_test.rb b/test/unit/lookups/postcode_anywhere_uk_test.rb
index b49c5cd3..83f526ec 100644
--- a/test/unit/lookups/postcode_anywhere_uk_test.rb
+++ b/test/unit/lookups/postcode_anywhere_uk_test.rb
@@ -27,6 +27,15 @@ class PostcodeAnywhereUkTest < GeocoderTestCase
     assert_equal 'Hallow', results.first.city
   end
 
+  def test_result_components_with_county
+    results = Geocoder.search('hampshire')
+
+    assert_equal 1, results.size
+    assert_equal 'Hampshire, SU 48701 26642', results.first.address
+    assert_equal [51.037, -1.3068], results.first.coordinates
+    assert_equal '', results.first.city
+  end
+
   def test_no_results
     assert_equal [], Geocoder.search('no results')
   end
-- 
GitLab