From 5d3744c28c5debe9140039ca2f435266af5fa2f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20C=2E=20Mu=CC=88ller?= <jcmuller@gmail.com>
Date: Tue, 15 Nov 2016 13:07:21 -0500
Subject: [PATCH] Fix mapbox result so that responses without context don't
 raise error

---
 lib/geocoder/results/mapbox.rb       |   2 +-
 test/fixtures/mapbox_Shanghai,_China | 224 +++++++++++++++++++++++++++
 test/unit/result_test.rb             |  10 ++
 3 files changed, 235 insertions(+), 1 deletion(-)
 create mode 100644 test/fixtures/mapbox_Shanghai,_China

diff --git a/lib/geocoder/results/mapbox.rb b/lib/geocoder/results/mapbox.rb
index 9e1af899..e4b71b9e 100644
--- a/lib/geocoder/results/mapbox.rb
+++ b/lib/geocoder/results/mapbox.rb
@@ -46,7 +46,7 @@ module Geocoder::Result
     private
 
     def context
-      @data['context']
+      Array(data['context'])
     end
   end
 end
diff --git a/test/fixtures/mapbox_Shanghai,_China b/test/fixtures/mapbox_Shanghai,_China
new file mode 100644
index 00000000..f40b8cc6
--- /dev/null
+++ b/test/fixtures/mapbox_Shanghai,_China
@@ -0,0 +1,224 @@
+{
+    "type": "FeatureCollection",
+        "query": [
+            "shanghai",
+        "china"
+        ],
+        "features": [
+        {
+            "id": "country.6702069377157440",
+            "type": "Feature",
+            "text": "China",
+            "place_name": "China",
+            "relevance": 0.49,
+            "properties": {
+                "wikidata": "Q148",
+                "short_code": "cn"
+            },
+            "bbox": [
+                73.499857014,
+            15.6816898058459,
+            134.772810006,
+            53.560710919
+            ],
+            "center": [
+                101.901875,
+            35.486703
+            ],
+            "geometry": {
+                "type": "Point",
+                "coordinates": [
+                    101.901875,
+                35.486703
+                ]
+            }
+        },
+        {
+            "id": "neighborhood.8146618069770140",
+            "type": "Feature",
+            "text": "Chinatown",
+            "place_name": "Chinatown, Washington, 20001, District of Columbia, United States",
+            "relevance": 0.49,
+            "properties": {
+
+            },
+            "bbox": [
+                -77.0265197753999,
+            38.8958426648877,
+            -77.0178665827466,
+            38.9031438069192
+            ],
+            "center": [
+                -77.021,
+            38.8998
+            ],
+            "geometry": {
+                "type": "Point",
+                "coordinates": [
+                    -77.021,
+                38.8998
+                ]
+            },
+            "context": [
+            {
+                "id": "place.12334081418246050",
+                "text": "Washington",
+                "wikidata": "Q61"
+            },
+            {
+                "id": "postcode.3526019892841050",
+                "text": "20001"
+            },
+            {
+                "id": "region.6884744206035790",
+                "text": "District of Columbia",
+                "short_code": "US-DC",
+                "wikidata": "Q61"
+            },
+            {
+                "id": "country.12862386939497690",
+                "text": "United States",
+                "short_code": "us",
+                "wikidata": "Q30"
+            }
+            ]
+        },
+        {
+            "id": "neighborhood.8687109740770140",
+            "type": "Feature",
+            "text": "Chinatown",
+            "place_name": "Chinatown, San Francisco, 94108, California, United States",
+            "relevance": 0.49,
+            "properties": {
+
+            },
+            "bbox": [
+                -122.411564112065,
+            37.7895769681609,
+            -122.404174805,
+            37.7985673428131
+            ],
+            "center": [
+                -122.4071,
+            37.7943
+            ],
+            "geometry": {
+                "type": "Point",
+                "coordinates": [
+                    -122.4071,
+                37.7943
+                ]
+            },
+            "context": [
+            {
+                "id": "place.17925508508361910",
+                "text": "San Francisco",
+                "wikidata": "Q62"
+            },
+            {
+                "id": "postcode.1814482917195520",
+                "text": "94108"
+            },
+            {
+                "id": "region.6020809690311220",
+                "text": "California",
+                "short_code": "US-CA",
+                "wikidata": "Q99"
+            },
+            {
+                "id": "country.12862386939497690",
+                "text": "United States",
+                "short_code": "us",
+                "wikidata": "Q30"
+            }
+            ]
+        },
+        {
+            "id": "region.7004037305164860",
+            "type": "Feature",
+            "text": "Chinandega",
+            "place_name": "Chinandega, Nicaragua",
+            "relevance": 0.49,
+            "properties": {
+                "short_code": "NI-CI",
+                "wikidata": "Q644024"
+            },
+            "bbox": [
+                -87.785656816,
+            12.423387369,
+            -86.654601813223,
+            13.293349936
+            ],
+            "center": [
+                -87.210612,
+            12.857407
+            ],
+            "geometry": {
+                "type": "Point",
+                "coordinates": [
+                    -87.210612,
+                12.857407
+                ]
+            },
+            "context": [
+            {
+                "id": "country.11224608483080220",
+                "text": "Nicaragua",
+                "short_code": "ni",
+                "wikidata": "Q811"
+            }
+            ]
+        },
+        {
+            "id": "neighborhood.5866411034770140",
+            "type": "Feature",
+            "text": "Chinatown",
+            "place_name": "Chinatown, Portland, 97209, Oregon, United States",
+            "relevance": 0.49,
+            "properties": {
+
+            },
+            "bbox": [
+                -122.679673745277,
+            45.522452158706,
+            -122.664221513386,
+            45.530089952328
+            ],
+            "center": [
+                -122.673,
+            45.5257
+            ],
+            "geometry": {
+                "type": "Point",
+                "coordinates": [
+                    -122.673,
+                45.5257
+                ]
+            },
+            "context": [
+            {
+                "id": "place.10101300842326360",
+                "text": "Portland",
+                "wikidata": "Q6106"
+            },
+            {
+                "id": "postcode.16394767032837600",
+                "text": "97209"
+            },
+            {
+                "id": "region.14077928225490090",
+                "text": "Oregon",
+                "short_code": "US-OR",
+                "wikidata": "Q824"
+            },
+            {
+                "id": "country.12862386939497690",
+                "text": "United States",
+                "short_code": "us",
+                "wikidata": "Q30"
+            }
+            ]
+        }
+        ],
+        "attribution": "© 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service. (https://www.mapbox.com/about/maps/)"
+}
diff --git a/test/unit/result_test.rb b/test/unit/result_test.rb
index 7f1aeb3f..5a469d32 100644
--- a/test/unit/result_test.rb
+++ b/test/unit/result_test.rb
@@ -82,6 +82,16 @@ class ResultTest < GeocoderTestCase
     end
   end
 
+  def test_mapbox_result_without_context
+    assert_nothing_raised do
+      Geocoder.configure(:lookup => :mapbox)
+      set_api_key!(:mapbox)
+      result = Geocoder.search("Shanghai, China")[0]
+      puts result.inspect
+      assert_equal nil, result.city
+    end
+  end
+
   private # ------------------------------------------------------------------
 
   def assert_result_has_required_attributes(result)
-- 
GitLab