Class: GeoSpider::Extractors::Postcode

Inherits:
Base
  • Object
show all
Defined in:
lib/geo-spider/extractors/postcode.rb

Constant Summary collapse

REGEXP =

Full BS 7666 postcode format. Source: en.wikipedia.org/wiki/UK_postcodes

/(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])(\s*)[0-9][ABD-HJLNP-UW-Z]{2})/i

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from GeoSpider::Extractors::Base

Class Method Details

.api_key=(api_key) ⇒ Object

You need to set a valid Multimap API key before the UK postcode geocoding will work. Multimap have the most accurate UK postcode geocoding I’ve discovered.



32
33
34
# File 'lib/geo-spider/extractors/postcode.rb', line 32

def self.api_key=(api_key)
  @@api_key = api_key
end

Instance Method Details

#locationsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/geo-spider/extractors/postcode.rb', line 13

def locations
  results = @element.inner_text.scan(REGEXP)
  results = results.map(&:first)
  
  found_locations = []
  
  results.each do |result|
    begin
      p = geocoder.locate(:postal_code => result, :country => "GB")
      found_locations << Location.new(:latitude => p.latitude, :longitude => p.longitude, :title => result)
    rescue Graticule::Error => e
      next
    end
  end
  return found_locations
end