geokit-premier 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Geocoder" do
4
+
5
+ after(:each) do
6
+ Geokit::Geocoders.google_client_id = nil
7
+ Geokit::Geocoders.google_premier_secret_key = nil
8
+ Geokit::Geocoders::google = nil
9
+ end
10
+
11
+
12
+ describe "self.google_client_id" do
13
+ it "should be nil by default and settable" do
14
+ Geokit::Geocoders.google_client_id.should == nil
15
+ Geokit::Geocoders.google_client_id = 'abc'
16
+ Geokit::Geocoders.google_client_id.should == 'abc'
17
+ end
18
+ end
19
+
20
+ describe "self.google_premier_secret_key" do
21
+ it "should be nil by default and settable" do
22
+ Geokit::Geocoders.google_premier_secret_key.should == nil
23
+ Geokit::Geocoders.google_premier_secret_key = 'abc123'
24
+ Geokit::Geocoders.google_premier_secret_key.should == 'abc123'
25
+ end
26
+ end
27
+
28
+ describe "#sign_url" do
29
+ it "should encrypt the url" do
30
+ expected = 'http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA='
31
+ actual = Geokit::Geocoders::Geocoder.sign_url('http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID','vNIXE0xscrmjlyV-12Nj_BvUPaw=')
32
+ actual.should == expected
33
+ end
34
+
35
+ it "xml example" do
36
+ secret = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
37
+ url = "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&client=gme-cenx&sensor=false"
38
+ expected = "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&client=gme-cenx&sensor=false&signature=1LZ2Iz3gtt-OH0uIv0nJBFGN8E8="
39
+ Geokit::Geocoders::Geocoder.sign_url(url,secret).should == expected
40
+ end
41
+
42
+ end
43
+
44
+ describe "#urlsafe_decode64" do
45
+ it "should deal with - and +" do
46
+ Geokit::Geocoders::Geocoder.urlsafe_decode64("a-b+c-d+").should == "k\346\376s\347~"
47
+ end
48
+ end
49
+
50
+ describe "#urlsafe_encode64" do
51
+ it "should deal with - and +" do
52
+ Geokit::Geocoders::Geocoder.urlsafe_encode64("k\346\376s\347~").should == "a-b-c-d-\n"
53
+ end
54
+ end
55
+
56
+ describe "GoogleGeocoder3#geocode_url" do
57
+
58
+ it "should use default if not premier" do
59
+ Geokit::Geocoders::google = 'abc123'
60
+ expected = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=Ottawa"
61
+ Geokit::Geocoders::GoogleGeocoder3.geocode_url('Ottawa',{}).should == expected
62
+ end
63
+
64
+ it "should use client if premier" do
65
+ Geokit::Geocoders.google_client_id = 'gme-cenx'
66
+ Geokit::Geocoders.google_premier_secret_key = 'ciK-I4AWUmFx5jBRIjtrL6hDC04='
67
+ expected = "http://maps.googleapis.com/maps/api/geocode/json?address=Ottawa&client=gme-cenx&sensor=false&oe=utf-8&signature=VG4njf1Yo59tnEvwPAMlgOoj4_0="
68
+ Geokit::Geocoders::GoogleGeocoder3.geocode_url('Ottawa',{}).should == expected
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,38 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ require 'lib/geokit'
3
+ require 'rubygems'
4
+ require 'irb'
5
+ require 'irb/completion'
6
+
7
+ # Requires supporting ruby files with custom matchers and macros, etc,
8
+ # in spec/support/ and its subdirectories.
9
+ # Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ # == Mock Framework
14
+ #
15
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
16
+ #
17
+ # config.mock_with :mocha
18
+ # config.mock_with :flexmock
19
+ # config.mock_with :rr
20
+ config.mock_with :rspec
21
+ # config.before(:each) { Machinist.reset_before_test }
22
+
23
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
24
+ # config.fixture_path = "#{::Rails.root}/spec/fixtures"
25
+
26
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
27
+ # examples within a transaction, remove the following line or assign false
28
+ # instead of true.
29
+ # config.use_transactional_fixtures = true
30
+
31
+ end
32
+
33
+ module IRB
34
+ def IRB.parse_opts
35
+ # Don't touch ARGV, which belongs to the app which called this module.
36
+ end
37
+ end
38
+
@@ -0,0 +1,58 @@
1
+ require 'test/unit'
2
+ require 'net/http'
3
+ require 'rubygems'
4
+ require 'mocha'
5
+ require 'lib/geokit'
6
+
7
+ class MockSuccess < Net::HTTPSuccess #:nodoc: all
8
+ def initialize
9
+ end
10
+ end
11
+
12
+ class MockFailure < Net::HTTPServiceUnavailable #:nodoc: all
13
+ def initialize
14
+ end
15
+ end
16
+
17
+ # Base class for testing geocoders.
18
+ class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
19
+
20
+ class Geokit::Geocoders::TestGeocoder < Geokit::Geocoders::Geocoder
21
+ def self.do_get(url)
22
+ sleep(2)
23
+ end
24
+ end
25
+
26
+ # Defines common test fixtures.
27
+ def setup
28
+ @address = 'San Francisco, CA'
29
+ @full_address = '100 Spear St, San Francisco, CA, 94105-1522, US'
30
+ @full_address_short_zip = '100 Spear St, San Francisco, CA, 94105, US'
31
+
32
+ @latlng = Geokit::LatLng.new(37.7742, -122.417068)
33
+ @success = Geokit::GeoLoc.new({:city=>"SAN FRANCISCO", :state=>"CA", :country_code=>"US", :lat=>@latlng.lat, :lng=>@latlng.lng})
34
+ @success.success = true
35
+ end
36
+
37
+ def test_timeout_call_web_service
38
+ url = "http://www.anything.com"
39
+ Geokit::Geocoders::request_timeout = 1
40
+ assert_nil Geokit::Geocoders::TestGeocoder.call_geocoder_service(url)
41
+ end
42
+
43
+ def test_successful_call_web_service
44
+ url = "http://www.anything.com"
45
+ Geokit::Geocoders::Geocoder.expects(:do_get).with(url).returns("SUCCESS")
46
+ assert_equal "SUCCESS", Geokit::Geocoders::Geocoder.call_geocoder_service(url)
47
+ end
48
+
49
+ def test_find_geocoder_methods
50
+ public_methods = Geokit::Geocoders::Geocoder.public_methods.map { |m| m.to_s }
51
+ assert public_methods.include?("yahoo_geocoder")
52
+ assert public_methods.include?("google_geocoder")
53
+ assert public_methods.include?("ca_geocoder")
54
+ assert public_methods.include?("us_geocoder")
55
+ assert public_methods.include?("multi_geocoder")
56
+ assert public_methods.include?("ip_geocoder")
57
+ end
58
+ end
@@ -0,0 +1,97 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+
4
+ class BoundsTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def setup
7
+ # This is the area in Texas
8
+ @sw = Geokit::LatLng.new(32.91663,-96.982841)
9
+ @ne = Geokit::LatLng.new(32.96302,-96.919495)
10
+ @bounds=Geokit::Bounds.new(@sw,@ne)
11
+ @loc_a=Geokit::LatLng.new(32.918593,-96.958444) # inside bounds
12
+ @loc_b=Geokit::LatLng.new(32.914144,-96.958444) # outside bouds
13
+
14
+ # this is a cross-meridan area
15
+ @cross_meridian=Geokit::Bounds.normalize([30,170],[40,-170])
16
+ @inside_cm=Geokit::LatLng.new(35,175)
17
+ @inside_cm_2=Geokit::LatLng.new(35,-175)
18
+ @east_of_cm=Geokit::LatLng.new(35,-165)
19
+ @west_of_cm=Geokit::LatLng.new(35,165)
20
+
21
+ end
22
+
23
+ def test_equality
24
+ assert_equal Geokit::Bounds.new(@sw,@ne), Geokit::Bounds.new(@sw,@ne)
25
+ end
26
+
27
+ def test_normalize
28
+ res=Geokit::Bounds.normalize(@sw,@ne)
29
+ assert_equal res,Geokit::Bounds.new(@sw,@ne)
30
+ res=Geokit::Bounds.normalize([@sw,@ne])
31
+ assert_equal res,Geokit::Bounds.new(@sw,@ne)
32
+ res=Geokit::Bounds.normalize([@sw.lat,@sw.lng],[@ne.lat,@ne.lng])
33
+ assert_equal res,Geokit::Bounds.new(@sw,@ne)
34
+ res=Geokit::Bounds.normalize([[@sw.lat,@sw.lng],[@ne.lat,@ne.lng]])
35
+ assert_equal res,Geokit::Bounds.new(@sw,@ne)
36
+ end
37
+
38
+ def test_point_inside_bounds
39
+ assert @bounds.contains?(@loc_a)
40
+ end
41
+
42
+ def test_point_outside_bounds
43
+ assert !@bounds.contains?(@loc_b)
44
+ end
45
+
46
+ def test_point_inside_bounds_cross_meridian
47
+ assert @cross_meridian.contains?(@inside_cm)
48
+ assert @cross_meridian.contains?(@inside_cm_2)
49
+ end
50
+
51
+ def test_point_outside_bounds_cross_meridian
52
+ assert !@cross_meridian.contains?(@east_of_cm)
53
+ assert !@cross_meridian.contains?(@west_of_cm)
54
+ end
55
+
56
+ def test_center
57
+ assert_in_delta 32.939828,@bounds.center.lat,0.00005
58
+ assert_in_delta(-96.9511763,@bounds.center.lng,0.00005)
59
+ end
60
+
61
+ def test_center_cross_meridian
62
+ assert_in_delta 35.41160, @cross_meridian.center.lat,0.00005
63
+ assert_in_delta 179.38112, @cross_meridian.center.lng,0.00005
64
+ end
65
+
66
+ def test_creation_from_circle
67
+ bounds=Geokit::Bounds.from_point_and_radius([32.939829, -96.951176],2.5)
68
+ inside=Geokit::LatLng.new 32.9695270000,-96.9901590000
69
+ outside=Geokit::LatLng.new 32.8951550000,-96.9584440000
70
+ assert bounds.contains?(inside)
71
+ assert !bounds.contains?(outside)
72
+ end
73
+
74
+ def test_bounds_to_span
75
+ sw = Geokit::LatLng.new(32, -96)
76
+ ne = Geokit::LatLng.new(40, -70)
77
+ bounds = Geokit::Bounds.new(sw, ne)
78
+
79
+ assert_equal Geokit::LatLng.new(8, 26), bounds.to_span
80
+ end
81
+
82
+ def test_bounds_to_span_with_bounds_crossing_prime_meridian
83
+ sw = Geokit::LatLng.new(20, -70)
84
+ ne = Geokit::LatLng.new(40, 100)
85
+ bounds = Geokit::Bounds.new(sw, ne)
86
+
87
+ assert_equal Geokit::LatLng.new(20, 170), bounds.to_span
88
+ end
89
+
90
+ def test_bounds_to_span_with_bounds_crossing_dateline
91
+ sw = Geokit::LatLng.new(20, 100)
92
+ ne = Geokit::LatLng.new(40, -70)
93
+ bounds = Geokit::Bounds.new(sw, ne)
94
+
95
+ assert_equal Geokit::LatLng.new(20, 190), bounds.to_span
96
+ end
97
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ Geokit::Geocoders::geocoder_ca = "SOMEKEYVALUE"
4
+
5
+ class CaGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ CA_SUCCESS=<<-EOF
8
+ <?xml version="1.0" encoding="UTF-8" ?>
9
+ <geodata><latt>49.243086</latt><longt>-123.153684</longt></geodata>
10
+ EOF
11
+
12
+ def setup
13
+ @ca_full_hash = {:street_address=>"2105 West 32nd Avenue",:city=>"Vancouver", :state=>"BC"}
14
+ @ca_full_loc = Geokit::GeoLoc.new(@ca_full_hash)
15
+ end
16
+
17
+ def test_geocoder_with_geo_loc_with_account
18
+ response = MockSuccess.new
19
+ response.expects(:body).returns(CA_SUCCESS)
20
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
21
+ Geokit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
22
+ verify(Geokit::Geocoders::CaGeocoder.geocode(@ca_full_loc))
23
+ end
24
+
25
+ def test_service_unavailable
26
+ response = MockFailure.new
27
+ #Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml")).returns(response)
28
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
29
+ Geokit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
30
+ assert !Geokit::Geocoders::CaGeocoder.geocode(@ca_full_loc).success
31
+ end
32
+
33
+ private
34
+
35
+ def verify(location)
36
+ assert_equal "BC", location.state
37
+ assert_equal "Vancouver", location.city
38
+ assert_equal "49.243086,-123.153684", location.ll
39
+ assert !location.is_us?
40
+ end
41
+ end
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'lib/geokit'
3
+
4
+ class GeoLocTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def setup
7
+ @loc = Geokit::GeoLoc.new
8
+ end
9
+
10
+ def test_is_us
11
+ assert !@loc.is_us?
12
+ @loc.country_code = 'US'
13
+ assert @loc.is_us?
14
+ end
15
+
16
+ def test_success
17
+ assert !@loc.success?
18
+ @loc.success = false
19
+ assert !@loc.success?
20
+ @loc.success = true
21
+ assert @loc.success?
22
+ end
23
+
24
+ def test_street_number
25
+ @loc.street_address = '123 Spear St.'
26
+ assert_equal '123', @loc.street_number
27
+ end
28
+
29
+ def test_street_name
30
+ @loc.street_address = '123 Spear St.'
31
+ assert_equal 'Spear St.', @loc.street_name
32
+ end
33
+
34
+ def test_city
35
+ @loc.city = "san francisco"
36
+ assert_equal 'San Francisco', @loc.city
37
+ end
38
+
39
+ def test_full_address
40
+ @loc.city = 'San Francisco'
41
+ @loc.state = 'CA'
42
+ @loc.zip = '94105'
43
+ @loc.country_code = 'US'
44
+ assert_equal 'San Francisco, CA, 94105, US', @loc.full_address
45
+ @loc.full_address = 'Irving, TX, 75063, US'
46
+ assert_equal 'Irving, TX, 75063, US', @loc.full_address
47
+ end
48
+
49
+ def test_hash
50
+ @loc.city = 'San Francisco'
51
+ @loc.state = 'CA'
52
+ @loc.zip = '94105'
53
+ @loc.country_code = 'US'
54
+ @another = Geokit::GeoLoc.new @loc.to_hash
55
+ assert_equal @loc, @another
56
+ end
57
+
58
+ def test_all
59
+ assert_equal [@loc], @loc.all
60
+ end
61
+
62
+ def test_to_yaml
63
+ @loc.city = 'San Francisco'
64
+ @loc.state = 'CA'
65
+ @loc.zip = '94105'
66
+ @loc.country_code = 'US'
67
+ assert_equal(
68
+ "--- !ruby/object:Geokit::GeoLoc \ncity: San Francisco\ncountry_code: US\nfull_address: \nlat: \nlng: \nprecision: unknown\nprovince: \nstate: CA\nstreet_address: \nsuccess: false\nzip: \"94105\"\n",
69
+ @loc.to_yaml)
70
+ end
71
+
72
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
3
+
4
+ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
5
+
6
+ IP_SUCCESS=<<-EOF
7
+ <?xml version="1.0" encoding="UTF-8"?>
8
+ <geoPlugin>
9
+ <geoplugin_city>Belo Horizonte</geoplugin_city>
10
+ <geoplugin_region>Minas Gerais</geoplugin_region>
11
+ <geoplugin_areaCode>0</geoplugin_areaCode>
12
+ <geoplugin_dmaCode>0</geoplugin_dmaCode>
13
+ <geoplugin_countryCode>BR</geoplugin_countryCode>
14
+ <geoplugin_countryName>Brazil</geoplugin_countryName>
15
+ <geoplugin_continentCode>SA</geoplugin_continentCode>
16
+ <geoplugin_latitude>-19.916700</geoplugin_latitude>
17
+ <geoplugin_longitude>-43.933300</geoplugin_longitude>
18
+ <geoplugin_currencyCode>BRL</geoplugin_currencyCode>
19
+ <geoplugin_currencySymbol>&#82;&#36;</geoplugin_currencySymbol>
20
+ <geoplugin_currencyConverter>2.2575001717</geoplugin_currencyConverter>
21
+ </geoPlugin>
22
+ EOF
23
+
24
+ def setup
25
+ super
26
+ @success.provider = "geoPlugin"
27
+ end
28
+
29
+ def test_successful_lookup
30
+ success = MockSuccess.new
31
+ success.expects(:body).returns(IP_SUCCESS)
32
+ url = 'http://www.geoplugin.net/xml.gp?ip=200.150.38.66'
33
+ GeoKit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(success)
34
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode('200.150.38.66')
35
+ assert_not_nil location
36
+ assert_equal -19.916700, location.lat
37
+ assert_equal -43.933300, location.lng
38
+ assert_equal "Belo Horizonte", location.city
39
+ assert_equal "Minas Gerais", location.state
40
+ assert_equal "BR", location.country_code
41
+ assert_equal "geoPlugin", location.provider
42
+ assert location.success?
43
+ end
44
+
45
+ def test_invalid_ip
46
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode("pixrum")
47
+ assert_not_nil location
48
+ assert !location.success?
49
+ end
50
+
51
+ def test_service_unavailable
52
+ failure = MockFailure.new
53
+ url = 'http://www.geoplugin.net/xml.gp?ip=10.10.10.10'
54
+ GeoKit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
55
+ location = GeoKit::Geocoders::GeoPluginGeocoder.geocode("10.10.10.10")
56
+ assert_not_nil location
57
+ assert !location.success?
58
+ end
59
+ end