graticule 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/CHANGELOG.txt +11 -0
  2. data/Manifest.txt +26 -11
  3. data/README.txt +1 -1
  4. data/bin/geocode +1 -1
  5. data/lib/graticule.rb +10 -8
  6. data/lib/graticule/cli.rb +1 -0
  7. data/lib/graticule/geocoder.rb +1 -12
  8. data/lib/graticule/geocoder/bogus.rb +15 -0
  9. data/lib/graticule/geocoder/geocoder_ca.rb +52 -0
  10. data/lib/graticule/geocoder/geocoder_us.rb +47 -0
  11. data/lib/graticule/geocoder/google.rb +106 -0
  12. data/lib/graticule/geocoder/host_ip.rb +50 -0
  13. data/lib/graticule/geocoder/local_search_maps.rb +45 -0
  14. data/lib/graticule/geocoder/meta_carta.rb +38 -0
  15. data/lib/graticule/geocoder/postcode_anywhere.rb +63 -0
  16. data/lib/graticule/geocoder/rest.rb +110 -0
  17. data/lib/graticule/geocoder/yahoo.rb +96 -0
  18. data/lib/graticule/location.rb +19 -7
  19. data/lib/graticule/version.rb +2 -2
  20. data/test/fixtures/responses/host_ip/private.txt +4 -0
  21. data/test/fixtures/responses/host_ip/success.txt +4 -0
  22. data/test/fixtures/responses/host_ip/unknown.txt +4 -0
  23. data/test/fixtures/responses/local_search_maps/success.txt +1 -0
  24. data/test/fixtures/responses/postcode_anywhere/badkey.xml +9 -0
  25. data/test/fixtures/responses/postcode_anywhere/canada.xml +16 -0
  26. data/test/fixtures/responses/postcode_anywhere/empty.xml +16 -0
  27. data/test/fixtures/responses/postcode_anywhere/success.xml +16 -0
  28. data/test/fixtures/responses/postcode_anywhere/uk.xml +18 -0
  29. data/test/test_helper.rb +4 -2
  30. data/test/unit/graticule/geocoder/geocoder_us_test.rb +43 -0
  31. data/test/unit/graticule/geocoder/google_test.rb +66 -0
  32. data/test/unit/graticule/geocoder/host_ip_test.rb +39 -0
  33. data/test/unit/graticule/geocoder/local_search_maps_test.rb +30 -0
  34. data/test/unit/graticule/geocoder/meta_carta_test.rb +44 -0
  35. data/test/unit/graticule/geocoder/postcode_anywhere_test.rb +50 -0
  36. data/test/unit/graticule/geocoder/yahoo_test.rb +48 -0
  37. data/test/unit/graticule/geocoder_test.rb +5 -9
  38. data/test/unit/graticule/location_test.rb +22 -7
  39. metadata +37 -18
  40. data/lib/graticule/geocoders/bogus.rb +0 -11
  41. data/lib/graticule/geocoders/geocoder_us.rb +0 -45
  42. data/lib/graticule/geocoders/google.rb +0 -99
  43. data/lib/graticule/geocoders/meta_carta.rb +0 -102
  44. data/lib/graticule/geocoders/rest.rb +0 -98
  45. data/lib/graticule/geocoders/yahoo.rb +0 -101
  46. data/test/unit/graticule/geocoders/geocoder_us_test.rb +0 -42
  47. data/test/unit/graticule/geocoders/geocoders.rb +0 -56
  48. data/test/unit/graticule/geocoders/google_test.rb +0 -22
  49. data/test/unit/graticule/geocoders/meta_carta_test.rb +0 -70
  50. data/test/unit/graticule/geocoders/yahoo_test.rb +0 -49
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Server="WS11" Version="3.0" Date="17/03/2007 18:12:11" Duration="1.703s">
3
+ <Schema Items="8">
4
+ <Field Name="line1"/>
5
+ <Field Name="line2"/>
6
+ <Field Name="city"/>
7
+ <Field Name="state"/>
8
+ <Field Name="postal_code"/>
9
+ <Field Name="country"/>
10
+ <Field Name="longitude"/>
11
+ <Field Name="latitude"/>
12
+ </Schema>
13
+ <Data Items="1">
14
+ <Item line1="204 Campbell Ave" city="Revelstoke" state="BC" postal_code="V0E" country="Canada" longitude="-118.196970002204" latitude="50.9997350418267"/>
15
+ </Data>
16
+ </PostcodeAnywhere>
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <PostcodeAnywhere Server="WS12" Version="3.0" Date="17/03/2007 19:01:39" Duration="2.422s">
3
+ <Schema Items="10">
4
+ <Field Name="id"/>
5
+ <Field Name="seq"/>
6
+ <Field Name="location"/>
7
+ <Field Name="grid_east_m"/>
8
+ <Field Name="grid_north_m"/>
9
+ <Field Name="longitude"/>
10
+ <Field Name="latitude"/>
11
+ <Field Name="os_reference"/>
12
+ <Field Name="wgs84_longitude"/>
13
+ <Field Name="wgs84_latitude"/>
14
+ </Schema>
15
+ <Data Items="1">
16
+ <Item id="17783983.00" seq="0" location="80 Wood Lane London NW9" grid_east_m="521000" grid_north_m="187500" longitude="-0.253788666693255" latitude="51.5728910186362" os_reference="TQ 21000 87500" wgs84_longitude="-0.255365891581496" wgs84_latitude="51.5733397173092"/>
17
+ </Data>
18
+ </PostcodeAnywhere>
data/test/test_helper.rb CHANGED
@@ -9,14 +9,16 @@ require 'test/unit'
9
9
  require 'breakpoint'
10
10
  require 'graticule'
11
11
 
12
+ TEST_RESPONSE_PATH = File.dirname(__FILE__) + '/fixtures/responses'
13
+
12
14
  module Test
13
15
  module Unit
14
16
  module Assertions
15
17
 
16
18
  private
17
- def response(geocoder, response)
19
+ def response(geocoder, response, extension = 'xml')
18
20
  clean_backtrace do
19
- File.read(File.dirname(__FILE__) + "/fixtures/responses/#{geocoder}/#{response}.xml")
21
+ File.read(File.dirname(__FILE__) + "/fixtures/responses/#{geocoder}/#{response}.#{extension}")
20
22
  end
21
23
  end
22
24
 
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class GeocoderUsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @geocoder = GeocoderUs.new
12
+ @location = Location.new(
13
+ :street => "1600 Pennsylvania Ave NW, Washington DC 20502",
14
+ :longitude => -77.037684,
15
+ :latitude => 38.898748
16
+ )
17
+ end
18
+
19
+ def test_success
20
+ prepare_response(:success)
21
+ assert_equal @location, @geocoder.locate('1600 Pennsylvania Ave, Washington DC')
22
+ end
23
+
24
+ def test_url
25
+ prepare_response(:success)
26
+ @geocoder.locate('1600 Pennsylvania Ave, Washington DC')
27
+ assert_equal 'http://rpc.geocoder.us/service/rest/geocode?address=1600%20Pennsylvania%20Ave,%20Washington%20DC',
28
+ URI::HTTP.uris.first
29
+ end
30
+
31
+ def test_locate_bad_address
32
+ prepare_response(:unknown)
33
+ assert_raises(AddressError) { @geocoder.locate('yuck') }
34
+ end
35
+
36
+ protected
37
+ def prepare_response(id)
38
+ URI::HTTP.responses << response('geocoder_us', id)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class GoogleTest < Test::Unit::TestCase
6
+ def setup
7
+ URI::HTTP.responses = []
8
+ URI::HTTP.uris = []
9
+ @geocoder = Google.new(:key => 'APP_ID')
10
+ end
11
+
12
+ def test_success
13
+ return unless prepare_response(:success)
14
+
15
+ location = Location.new(
16
+ :street => "1600 Amphitheatre Pkwy",
17
+ :locality => "Mountain View",
18
+ :region => "CA",
19
+ :postal_code => "94043",
20
+ :country => "US",
21
+ :longitude => -122.083739,
22
+ :latitude => 37.423021,
23
+ :precision => :address
24
+ )
25
+ assert_equal location, @geocoder.locate('1600 Amphitheatre Parkway, Mountain View, CA')
26
+ end
27
+
28
+ def test_bad_key
29
+ return unless prepare_response(:badkey)
30
+ assert_raises(CredentialsError) { @geocoder.locate('x') }
31
+ end
32
+
33
+ def test_locate_missing_address
34
+ return unless prepare_response(:missing_address)
35
+ assert_raises(AddressError) { @geocoder.locate 'x' }
36
+ end
37
+
38
+ def test_locate_server_error
39
+ return unless prepare_response(:server_error)
40
+ assert_raises(Error) { @geocoder.locate 'x' }
41
+ end
42
+
43
+ def test_locate_too_many_queries
44
+ return unless prepare_response(:limit)
45
+ assert_raises(CredentialsError) { @geocoder.locate 'x' }
46
+ end
47
+
48
+ def test_locate_unavailable_address
49
+ return unless prepare_response(:unavailable)
50
+ assert_raises(AddressError) { @geocoder.locate 'x' }
51
+ end
52
+
53
+ def test_locate_unknown_address
54
+ return unless prepare_response(:unknown_address)
55
+ assert_raises(AddressError) { @geocoder.locate 'x' }
56
+ end
57
+
58
+ protected
59
+
60
+ def prepare_response(id = :success)
61
+ URI::HTTP.responses << response('google', id)
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class HostIpTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @geocoder = HostIp.new
9
+ URI::HTTP.responses = []
10
+ URI::HTTP.uris = []
11
+ end
12
+
13
+ def test_success
14
+ prepare_response :success
15
+
16
+ location = Location.new :country => 'US', :locality => 'Mountain View',
17
+ :region => 'CA', :latitude => 37.402, :longitude => -122.078
18
+
19
+ assert_equal location, @geocoder.locate('64.233.167.99')
20
+ end
21
+
22
+ def test_unknown
23
+ prepare_response :unknown
24
+ assert_raises(AddressError) { @geocoder.locate('127.0.0.1') }
25
+ end
26
+
27
+ def test_private_ip
28
+ prepare_response :private
29
+ assert_raises(AddressError) { @geocoder.locate('127.0.0.1') }
30
+ end
31
+ private
32
+
33
+ def prepare_response(id = :success)
34
+ URI::HTTP.responses << response('host_ip', id, 'txt')
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class LocalSearchMapsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @geocoder = LocalSearchMaps.new
9
+ URI::HTTP.responses = []
10
+ URI::HTTP.uris = []
11
+ end
12
+
13
+ def test_success
14
+ prepare_response :success
15
+
16
+ location = Location.new :latitude => 51.510036, :longitude => -0.130427
17
+
18
+ assert_equal location, @geocoder.locate(:street => '48 Leicester Square',
19
+ :locality => 'London', :country => 'UK')
20
+ end
21
+
22
+ private
23
+
24
+ def prepare_response(id = :success)
25
+ URI::HTTP.responses << response('local_search_maps', id, 'txt')
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class MetaCartaTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @geocoder = MetaCarta.new
12
+ end
13
+
14
+ def test_locate
15
+ prepare_response(:success)
16
+
17
+ expected = Location.new :latitude => 44.393889, :longitude => 33.338611
18
+
19
+ assert_equal expected, @geocoder.locate('baghdad')
20
+ assert_equal true, URI::HTTP.responses.empty?
21
+ assert_equal 1, URI::HTTP.uris.length
22
+ assert_equal 'http://labs.metacarta.com/GeoParser/?output=locations&q=baghdad',
23
+ URI::HTTP.uris.first
24
+ end
25
+
26
+ def test_locate_bad_address
27
+ prepare_response(:bad_address)
28
+ assert_raises(AddressError) { @geocoder.locate('aoeueou') }
29
+ end
30
+
31
+ def test_locations
32
+ prepare_response(:multiple)
33
+ expected = Location.new :latitude => -122.33083, :longitude => 47.60639
34
+ assert_equal expected, @geocoder.locate('seattle')
35
+ end
36
+
37
+ protected
38
+ def prepare_response(id = :success)
39
+ URI::HTTP.responses << response('meta_carta', id)
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,50 @@
1
+ # account code: INDIV30777
2
+ # license key: KG39-HM17-PZ95-NR98
3
+ require File.dirname(__FILE__) + '/../../../test_helper'
4
+
5
+ module Graticule
6
+ module Geocoder
7
+ class PostcodeAnywhereTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ URI::HTTP.responses = []
11
+ URI::HTTP.uris = []
12
+ @geocoder = PostcodeAnywhere.new 'account_code', 'license_key'
13
+ end
14
+
15
+ def test_locate
16
+ prepare_response(:success)
17
+ location = Location.new(
18
+ :street => "204 Campbell Ave",
19
+ :locality => "Revelstoke",
20
+ :region => "BC",
21
+ :postal_code => "V0E",
22
+ :country => "Canada",
23
+ :longitude => -118.196970002204,
24
+ :latitude => 50.9997350418267
25
+ )
26
+ assert_equal location, @geocoder.locate(:street => "204 Campbell Ave",
27
+ :locality => "Revelstoke", :country => "Canada")
28
+ end
29
+
30
+ def test_locate_uk_address
31
+ prepare_response(:uk)
32
+
33
+ location = Location.new :latitude => 51.5728910186362, :longitude => -0.253788666693255
34
+ assert_equal location, @geocoder.locate(:street => '80 Wood Lane', :locality => 'London', :country => 'UK')
35
+ end
36
+
37
+ def test_empty
38
+ prepare_response(:empty)
39
+
40
+ assert_raises(Graticule::AddressError) { @geocoder.locate :street => 'foobar'}
41
+ end
42
+
43
+ protected
44
+ def prepare_response(id)
45
+ URI::HTTP.responses << response('postcode_anywhere', id)
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../../../test_helper'
2
+
3
+ module Graticule
4
+ module Geocoder
5
+ class YahooTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+ @geocoder = Yahoo.new 'APP_ID'
11
+ @location = Location.new(
12
+ :street => "701 First Ave",
13
+ :locality => "Sunnyvale",
14
+ :region => "CA",
15
+ :postal_code => "94089-1019",
16
+ :country => "US",
17
+ :longitude => -122.024853,
18
+ :latitude => 37.416384,
19
+ :precision => :address
20
+ )
21
+ end
22
+
23
+ def test_locate
24
+ prepare_response(:success)
25
+ assert_equal @location, @geocoder.locate('701 First Street, Sunnyvale, CA')
26
+ end
27
+
28
+ def test_url
29
+ prepare_response(:success)
30
+ @geocoder.locate('701 First Street, Sunnyvale, CA')
31
+ assert_equal 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=APP_ID&location=701%20First%20Street,%20Sunnyvale,%20CA&output=xml',
32
+ URI::HTTP.uris.first
33
+ end
34
+
35
+
36
+ def test_locate_bad_address
37
+ prepare_response(:unknown_address)
38
+ assert_raise(Error) { @geocoder.locate('yucksthoeusthaoeusnhtaosu') }
39
+ end
40
+
41
+ protected
42
+ def prepare_response(id)
43
+ URI::HTTP.responses << response('yahoo', id)
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -3,28 +3,24 @@ require File.dirname(__FILE__) + '/../../test_helper'
3
3
  module Graticule
4
4
  class GeocoderTest < Test::Unit::TestCase
5
5
 
6
- def test_cannot_instantiate
7
- assert_raises(NotImplementedError) { Geocoder.new }
8
- end
9
-
10
6
  def test_bogus_service
11
- assert_equal BogusGeocoder, Graticule.service(:bogus)
7
+ assert_equal Geocoder::Bogus, Graticule.service(:bogus)
12
8
  end
13
9
 
14
10
  def test_yahoo_service
15
- assert_equal YahooGeocoder, Graticule.service(:yahoo)
11
+ assert_equal Geocoder::Yahoo, Graticule.service(:yahoo)
16
12
  end
17
13
 
18
14
  def test_google_service
19
- assert_equal GoogleGeocoder, Graticule.service(:google)
15
+ assert_equal Geocoder::Google, Graticule.service(:google)
20
16
  end
21
17
 
22
18
  def test_geocoder_us_service
23
- assert_equal GeocoderUsGeocoder, Graticule.service(:geocoder_us)
19
+ assert_equal Geocoder::GeocoderUs, Graticule.service(:geocoder_us)
24
20
  end
25
21
 
26
22
  def test_meta_carta_service
27
- assert_equal MetaCartaGeocoder, Graticule.service(:meta_carta)
23
+ assert_equal Geocoder::MetaCarta, Graticule.service(:meta_carta)
28
24
  end
29
25
 
30
26
  end
@@ -3,14 +3,19 @@ require File.dirname(__FILE__) + '/../../test_helper'
3
3
  module Graticule
4
4
  class LocationTest < Test::Unit::TestCase
5
5
 
6
+ def setup
7
+ @washington_dc = Location.new :latitude => 38.898748, :longitude => -77.037684,
8
+ :street => '1600 Pennsylvania Avenue, NW', :locality => 'Washington',
9
+ :region => 'DC', :postal_code => 20500, :country => 'US'
10
+ end
11
+
6
12
  def test_distance_to
7
- washington_dc = Location.new(:latitude => 38.898748, :longitude => -77.037684)
8
13
  chicago = Location.new(:latitude => 41.85, :longitude => -87.65)
9
- assert_in_delta 594.820, washington_dc.distance_to(chicago), 1.0
14
+ assert_in_delta 594.820, @washington_dc.distance_to(chicago), 1.0
10
15
  end
11
16
 
12
17
  def test_responds_to
13
- [:latitude, :longitude, :street, :city, :state, :zip, :country, :coordinates, :precision].each do |m|
18
+ [:latitude, :longitude, :street, :locality, :region, :postal_code, :country, :coordinates, :precision].each do |m|
14
19
  assert Location.new.respond_to?(m), "should respond to #{m}"
15
20
  end
16
21
  end
@@ -24,7 +29,7 @@ module Graticule
24
29
  assert_equal Location.new, Location.new
25
30
 
26
31
  attrs = {:latitude => 100.5389, :longitude => -147.5893, :street => '123 A Street',
27
- :city => 'Somewhere', :state => 'NO', :zip => '12345', :country => 'USA', :precision => :address}
32
+ :locality => 'Somewhere', :region => 'NO', :postal_code => '12345', :country => 'USA', :precision => :address}
28
33
 
29
34
  assert_equal Location.new(attrs), Location.new(attrs)
30
35
  attrs.each do |k,v|
@@ -35,17 +40,27 @@ module Graticule
35
40
  end
36
41
 
37
42
  def test_antipode
38
- washington_dc = Location.new(:latitude => 38.898748, :longitude => -77.037684)
39
43
  chicago = Location.new(:latitude => 41.85, :longitude => -87.65)
40
44
 
41
- assert_equal [-38.898748, 102.962316], washington_dc.antipode.coordinates
45
+ assert_equal [-38.898748, 102.962316], @washington_dc.antipode.coordinates
42
46
  assert_equal [-41.85, 92.35], chicago.antipode.coordinates
43
47
  assert_equal [-41, -180], Graticule::Location.new(:latitude => 41, :longitude => 0).antipode.coordinates
44
48
  assert_equal [-41, 179], Graticule::Location.new(:latitude => 41, :longitude => -1).antipode.coordinates
45
49
  assert_equal [-41, -179], Graticule::Location.new(:latitude => 41, :longitude => 1).antipode.coordinates
46
50
 
47
- assert_equal washington_dc.coordinates, washington_dc.antipode.antipode.coordinates
51
+ assert_equal @washington_dc.coordinates, @washington_dc.antipode.antipode.coordinates
48
52
  assert_equal chicago.coordinates, chicago.antipode.antipode.coordinates
49
53
  end
54
+
55
+ def test_to_s
56
+ assert_equal "1600 Pennsylvania Avenue, NW\nWashington, DC 20500 US",
57
+ @washington_dc.to_s
58
+ assert_equal "1600 Pennsylvania Avenue, NW\nWashington, DC 20500",
59
+ @washington_dc.to_s(:country => false)
60
+ assert_equal "1600 Pennsylvania Avenue, NW\nWashington, DC 20500",
61
+ @washington_dc.to_s(:country => false)
62
+ assert_equal "1600 Pennsylvania Avenue, NW\nWashington, DC 20500\nlatitude: 38.898748, longitude: -77.037684",
63
+ @washington_dc.to_s(:country => false, :coordinates => true)
64
+ end
50
65
  end
51
66
  end