From 8b7e8e1487e00a19a3982d1dfbdb4550f0d2b039 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:12:56 -0500
Subject: [PATCH] Extract context_part

---
 lib/geocoder/results/mapbox.rb | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/geocoder/results/mapbox.rb b/lib/geocoder/results/mapbox.rb
index d5e7ea0f..e458731d 100644
--- a/lib/geocoder/results/mapbox.rb
+++ b/lib/geocoder/results/mapbox.rb
@@ -16,27 +16,27 @@ module Geocoder::Result
     end
 
     def city
-      context.map { |c| c['text'] if c['id'] =~ /place/ }.compact.first
+      context_part('place')
     end
 
     def state
-      context.map { |c| c['text'] if c['id'] =~ /region/ }.compact.first
+      context_part('region')
     end
 
     alias_method :state_code, :state
 
     def postal_code
-      context.map { |c| c['text'] if c['id'] =~ /postcode/ }.compact.first
+      context_part('postcode')
     end
 
     def country
-      context.map { |c| c['text'] if c['id'] =~ /country/ }.compact.first
+      context_part('country')
     end
 
     alias_method :country_code, :country
 
     def neighborhood
-      context.map { |c| c['text'] if c['id'] =~ /neighborhood/ }.compact.first
+      context_part('neighborhood')
     end
 
     def address
@@ -45,6 +45,10 @@ module Geocoder::Result
 
     private
 
+    def context_part(name)
+      context.map { |c| c['text'] if c['id'] =~ Regexp.new(name) }.compact.first
+    end
+
     def context
       Array(data['context'])
     end
-- 
GitLab