geo-spider 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -11,14 +11,14 @@ module GeoSpider
|
|
11
11
|
REGEXP = /(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
|
12
12
|
|
13
13
|
def locations
|
14
|
-
results = @element.
|
14
|
+
results = @element.inner_text.scan(REGEXP)
|
15
15
|
results = results.map(&:first)
|
16
16
|
|
17
17
|
found_locations = []
|
18
18
|
|
19
19
|
results.each do |result|
|
20
20
|
begin
|
21
|
-
p = geocoder.locate(result)
|
21
|
+
p = geocoder.locate(:zip => result, :country => "GB")
|
22
22
|
found_locations << Location.new(:latitude => p.latitude, :longitude => p.longitude, :title => result)
|
23
23
|
rescue Graticule::Error => e
|
24
24
|
next
|
@@ -27,7 +27,7 @@ module GeoSpider
|
|
27
27
|
return found_locations
|
28
28
|
end
|
29
29
|
|
30
|
-
# You need to set a valid
|
30
|
+
# 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.
|
31
31
|
|
32
32
|
def self.api_key=(api_key)
|
33
33
|
@@api_key = api_key
|
@@ -36,8 +36,8 @@ module GeoSpider
|
|
36
36
|
private
|
37
37
|
|
38
38
|
def geocoder
|
39
|
-
raise "No
|
40
|
-
Graticule.service(:
|
39
|
+
raise "No Multimap API key set" unless @@api_key
|
40
|
+
Graticule.service(:multimap).new @@api_key
|
41
41
|
end
|
42
42
|
|
43
43
|
end
|
data/lib/geo-spider/version.rb
CHANGED
@@ -25,9 +25,9 @@ describe Page, "with a single postcode which is being parsed" do
|
|
25
25
|
OpenURI.should_receive(:open_uri).and_return(page_as_string('single_postcode.html'))
|
26
26
|
@page = Page.new("http://www.example.com")
|
27
27
|
GeoSpider::Extractors::Postcode.api_key = "waffles"
|
28
|
-
|
28
|
+
mock_geocoder = OpenStruct.new( { :locate => OpenStruct.new({ :longitude => -1.000000, :latitude => 51.000000 })})
|
29
29
|
Graticule.stub!(:service)
|
30
|
-
Graticule.service.should_receive(:new).and_return(
|
30
|
+
Graticule.service.should_receive(:new).and_return(mock_geocoder)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should find one location" do
|
@@ -49,9 +49,9 @@ describe Page, "with multiple microformats and postcodes being parsed" do
|
|
49
49
|
OpenURI.should_receive(:open_uri).and_return(page_as_string('multiple_postcodes_and_microformats.html'))
|
50
50
|
@page = Page.new("http://www.example.com")
|
51
51
|
|
52
|
-
|
52
|
+
mock_geocoder = OpenStruct.new( { :locate => OpenStruct.new({ :longitude => -1.000000, :latitude => 51.000000 })})
|
53
53
|
Graticule.stub!(:service)
|
54
|
-
Graticule.service.should_receive(:new).twice.and_return(
|
54
|
+
Graticule.service.should_receive(:new).twice.and_return(mock_geocoder)
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should find four locations" do
|
@@ -122,4 +122,17 @@ describe Page, "which is finding the title" do
|
|
122
122
|
@page.title.should == "Heading 1"
|
123
123
|
end
|
124
124
|
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe Page, "which is parsing a page with a string in a URL that happens to match a postcode" do
|
128
|
+
|
129
|
+
before(:each) do
|
130
|
+
OpenURI.should_receive(:open_uri).and_return(page_as_string('postcode_in_url.html'))
|
131
|
+
@page = Page.new("http://www.example.com")
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should not find any locations" do
|
135
|
+
@page.locations.should be_empty
|
136
|
+
end
|
137
|
+
|
125
138
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo-spider
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Taylor
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-10-
|
12
|
+
date: 2008-10-03 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|