Permalink
Browse files

Change way keys/options are defined and update readme

 * Means each provider encapsulates its own requirements
  • Loading branch information...
1 parent 4c6be2c commit 33c2c4af2c7a31650a0118b4b83e88b91123091e @mnoack mnoack committed Dec 26, 2013
View
@@ -1,3 +1,7 @@
+## 1.8.1
+
+* Change way keys/dependencies defined
+
## 1.8.0
* Added Bing Geocoder
View
@@ -107,14 +107,15 @@ If you're using this gem by itself, here are the configuration options:
# This is your yahoo application key for the Yahoo Geocoder.
# See http://developer.yahoo.com/faq/index.html#appid
# and http://developer.yahoo.com/maps/rest/V1/geocode.html
- Geokit::Geocoders::yahoo = 'REPLACE_WITH_YOUR_YAHOO_KEY'
+ Geokit::Geocoders::YahooGeocoder.key = 'REPLACE_WITH_YOUR_YAHOO_KEY'
+ Geokit::Geocoders::YahooGeocoder.secret = 'REPLACE_WITH_YOUR_YAHOO_SECRET'
# This is your Google Maps geocoder keys (all optional).
# See http://www.google.com/apis/maps/signup.html
# and http://www.google.com/apis/maps/documentation/#Geocoding_Examples
- Geokit::Geocoders::google_client_id = ''
- Geokit::Geocoders::google_cryptographic_key = ''
- Geokit::Geocoders::google_channel = ''
+ Geokit::Geocoders::GoogleGeocoder.client_id = ''
+ Geokit::Geocoders::GoogleGeocoder.cryptographic_key = ''
+ Geokit::Geocoders::GoogleGeocoder.channel = ''
# You can also set multiple API KEYS for different domains that may be directed to this same application.
# The domain from which the current user is being directed will automatically be updated for Geokit via
@@ -127,15 +128,21 @@ If you're using this gem by itself, here are the configuration options:
# usage tied to an account, the value should be set to username:password.
# See http://geocoder.us
# and http://geocoder.us/user/signup
- Geokit::Geocoders::geocoder_us = false
+ Geokit::Geocoders::UsGeocoder.key = 'username:password'
# This is your authorization key for geocoder.ca.
# To use the free service, the value can be set to nil or false. For
# usage tied to an account, set the value to the key obtained from
# Geocoder.ca.
# See http://geocoder.ca
# and http://geocoder.ca/?register=1
- Geokit::Geocoders::geocoder_ca = false
+ Geokit::Geocoders::CaGeocoder.key = 'KEY'
+
+ # Most other geocoders need either no setup or a key
+ Geokit::Geocoders::BingGeocoder.key = ''
+ Geokit::Geocoders::GeonamesGeocoder.key = ''
+ Geokit::Geocoders::MapQuestGeocoder.key = ''
+ Geokit::Geocoders::YandexGeocoder.key = ''
# require "external_geocoder.rb"
# Please see the section "writing your own geocoders" for more information.
@@ -32,18 +32,6 @@ module Geokit
module Geocoders
@@proxy = nil
@@request_timeout = nil
- @@bing = 'REPLACE_WITH_YOUR_BING_KEY'
- @@bing_options = {}
- @@yahoo_consumer_key = 'REPLACE_WITH_YOUR_YAHOO_BOSS_OAUTH_CONSUMER_KEY'
- @@yahoo_consumer_secret = 'REPLACE_WITH_YOUR_YAHOO_BOSS_OAUTH_CONSUMER_SECRET'
- @@yandex = nil
- @@mapquest = 'REPLACE_WITH_YOUR_MAPQUEST_KEY'
- @@google_client_id = nil
- @@google_cryptographic_key = nil
- @@google_channel = nil
- @@geocoder_us = false
- @@geocoder_ca = false
- @@geonames = false
@@provider_order = [:google,:us]
@@ip_provider_order = [:geo_plugin,:ip]
@@logger=Logger.new(STDOUT)
@@ -88,6 +76,19 @@ class TooManyQueriesError < StandardError; end
# The Geocoder base class which defines the interface to be used by all
# other geocoders.
class Geocoder
+ def self.config(*attrs)
+ attrs.each do |attr|
+ class_eval <<-METHOD
+ @@#{attr} = nil
+ def self.#{attr}=(value)
+ @@#{attr} = value
+ end
+ def self.#{attr}
+ @@#{attr}
+ end
+ METHOD
+ end
+ end
# Main method which calls the do_geocode template method which subclasses
# are responsible for implementing. Returns a populated GeoLoc or an
# empty one with a failed success code.
@@ -3,6 +3,7 @@ module Geocoders
# Bing geocoder implementation. Requires the Geokit::Geocoders::bing variable to
# contain a Bing Maps API key. Conforms to the interface set by the Geocoder class.
class BingGeocoder < Geocoder
+ config :key, :options
private
@@ -17,11 +18,10 @@ def self.do_geocode(address)
end
def self.submit_url(address)
- options = Geokit::Geocoders::bing_options
culture = options && options[:culture]
culture_string = culture ? "&c=#{culture}" : ''
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
- "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address_str)}?key=#{Geokit::Geocoders::bing}#{culture_string}&o=xml"
+ "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address_str)}?key=#{key}#{culture_string}&o=xml"
end
def self.parse_xml(xml)
@@ -12,6 +12,7 @@
module Geokit
module Geocoders
class CaGeocoder < Geocoder
+ config :key
private
@@ -41,7 +42,7 @@ def self.submit_url(loc)
args << "city=#{Geokit::Inflector::url_escape(loc.city)}" if loc.city
args << "prov=#{loc.state}" if loc.state
args << "postal=#{loc.zip}" if loc.zip
- args << "auth=#{Geokit::Geocoders::geocoder_ca}" if Geokit::Geocoders::geocoder_ca
+ args << "auth=#{key}" if key
args << "geoit=xml"
'http://geocoder.ca/?' + args.join('&')
end
@@ -3,6 +3,7 @@ module Geocoders
# Another geocoding web service
# http://www.geonames.org
class GeonamesGeocoder < Geocoder
+ config :key
private
@@ -23,8 +24,8 @@ def self.submit_url(address)
address_str.gsub!(/,/, " ")
params = "/postalCodeSearch?placename=#{Geokit::Inflector::url_escape(address_str)}&maxRows=10"
- if Geokit::Geocoders::geonames
- "http://ws.geonames.net#{params}&username=#{Geokit::Geocoders::geonames}"
+ if key
+ "http://ws.geonames.net#{params}&username=#{key}"
else
"http://ws.geonames.org#{params}"
end
@@ -1,6 +1,7 @@
module Geokit
module Geocoders
class GoogleGeocoder < Geocoder
+ config :client_id, :cryptographic_key, :channel
private
# Template method which does the reverse-geocode lookup.
@@ -69,10 +70,10 @@ def self.sign_gmap_bus_api_url(urlToSign, google_cryptographic_key)
def self.submit_url(query_string)
- if Geokit::Geocoders::google_client_id && Geokit::Geocoders::google_cryptographic_key
- channel = Geokit::Geocoders::google_channel ? "&channel=#{Geokit::Geocoders::google_channel}" : ''
- urlToSign = query_string + "&client=#{Geokit::Geocoders::google_client_id}" + channel
- signature = sign_gmap_bus_api_url(urlToSign, Geokit::Geocoders::google_cryptographic_key)
+ if client_id && cryptographic_key
+ channel = channel ? "&channel=#{channel}" : ''
+ urlToSign = query_string + "&client=#{client_id}" + channel
+ signature = sign_gmap_bus_api_url(urlToSign, cryptographic_key)
"http://maps.googleapis.com" + urlToSign + "&signature=#{signature}"
else
"http://maps.google.com" + query_string
@@ -3,13 +3,14 @@ module Geocoders
# MapQuest geocoder implementation. Requires the Geokit::Geocoders::mapquest variable to
# contain a MapQuest API key. Conforms to the interface set by the Geocoder class.
class MapQuestGeocoder < Geocoder
+ config :key
private
# Template method which does the reverse-geocode lookup.
def self.do_reverse_geocode(latlng)
latlng=LatLng.normalize(latlng)
- url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=#{Geokit::Geocoders::mapquest}&location=#{latlng.lat},#{latlng.lng}"
+ url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=#{key}&location=#{latlng.lat},#{latlng.lng}"
res = call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
json = res.body
@@ -20,7 +21,7 @@ def self.do_reverse_geocode(latlng)
# Template method which does the geocode lookup.
def self.do_geocode(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
- url = "http://www.mapquestapi.com/geocoding/v1/address?key=#{Geokit::Geocoders::mapquest}&location=#{Geokit::Inflector::url_escape(address_str)}"
+ url = "http://www.mapquestapi.com/geocoding/v1/address?key=#{key}&location=#{Geokit::Inflector::url_escape(address_str)}"
res = call_geocoder_service(url)
return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
json = res.body
@@ -4,14 +4,15 @@
module Geokit
module Geocoders
class UsGeocoder < Geocoder
+ config :key
private
def self.do_geocode(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
query = (address_str =~ /^\d{5}(?:-\d{4})?$/ ? "zip" : "address") + "=#{Geokit::Inflector::url_escape(address_str)}"
- url = if Geokit::Geocoders::geocoder_us
- "http://#{Geokit::Geocoders::geocoder_us}@geocoder.us/member/service/csv/geocode"
+ url = if key
+ "http://#{key}@geocoder.us/member/service/csv/geocode"
else
"http://geocoder.us/service/csv/geocode"
end
@@ -3,15 +3,16 @@ module Geocoders
# Yahoo geocoder implementation. Requires the Geokit::Geocoders::YAHOO variable to
# contain a Yahoo API key. Conforms to the interface set by the Geocoder class.
class YahooGeocoder < Geocoder
+ config :key, :secret
private
def self.submit_url(address)
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
query_string = "?q=#{Geokit::Inflector::url_escape(address_str)}&flags=J"
o = OauthUtil.new
- o.consumer_key = Geocoders::yahoo_consumer_key
- o.consumer_secret = Geocoders::yahoo_consumer_secret
+ o.consumer_key = key
+ o.consumer_secret = secret
base = "http://yboss.yahooapis.com/geo/placefinder"
parsed_url = URI.parse("#{base}#{query_string}")
"#{base}?#{o.sign(parsed_url).query_string}"
@@ -3,6 +3,8 @@ module Geocoders
# Yandex geocoder implementation. Expects the Geokit::Geocoders::YANDEX variable to
# contain a Yandex API key (optional). Conforms to the interface set by the Geocoder class.
class YandexGeocoder < Geocoder
+ config :key
+
private
# Template method which does the geocode lookup.
@@ -16,7 +18,7 @@ def self.do_geocode(address)
def self.submit_url(address_str)
url = "http://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector::url_escape(address_str)}&format=json"
- url += "&key=#{Geokit::Geocoders::yandex}" if Geokit::Geocoders::yandex
+ url += "&key=#{key}" if key
url
end
@@ -1,3 +1,3 @@
module Geokit
- VERSION = '1.8.0'
+ VERSION = '1.8.1'
end
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'helper')
-Geokit::Geocoders::bing = 'AuWcmtBIoPeOubm9BtcN44hTmWw_wNoJ5NEO2L0RaKrGAUE_nlwciKAqwapdq7k7'
+Geokit::Geocoders::BingGeocoder.key = 'AuWcmtBIoPeOubm9BtcN44hTmWw_wNoJ5NEO2L0RaKrGAUE_nlwciKAqwapdq7k7'
class BingGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -15,7 +15,8 @@ def assert_url(expected_url)
# the testing methods themselves
def test_bing_full_address
VCR.use_cassette('bing_full') do
- url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(@full_address)}?key=#{Geokit::Geocoders::bing}&o=xml"
+ key = Geokit::Geocoders::BingGeocoder.key
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(@full_address)}?key=#{key}&o=xml"
res = Geokit::Geocoders::BingGeocoder.geocode(@full_address)
assert_equal "CA", res.state
assert_equal "San Francisco", res.city
@@ -30,7 +31,8 @@ def test_bing_full_address
def test_bing_full_address_au
address = '440 King William Street, Adelaide, Australia'
VCR.use_cassette('bing_full_au') do
- url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{Geokit::Geocoders::bing}&o=xml"
+ key = Geokit::Geocoders::BingGeocoder.key
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{key}&o=xml"
res = Geokit::Geocoders::BingGeocoder.geocode(address)
assert_equal "SA", res.state
assert_equal "Adelaide", res.city
@@ -46,7 +48,8 @@ def test_bing_full_address_au
def test_bing_full_address_de
address = "Platz der Republik 1, 11011 Berlin, Germany"
VCR.use_cassette('bing_full_de') do
- url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{Geokit::Geocoders::bing}&o=xml"
+ key = Geokit::Geocoders::BingGeocoder.key
+ url = "http://dev.virtualearth.net/REST/v1/Locations/#{URI.escape(address)}?key=#{key}&o=xml"
res = Geokit::Geocoders::BingGeocoder.geocode(address)
assert_equal "BE", res.state
assert_equal "Berlin", res.city
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'helper')
-Geokit::Geocoders::geocoder_ca = "SOMEKEYVALUE"
+Geokit::Geocoders::CaGeocoder::key = "SOMEKEYVALUE"
class CaGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -1,10 +1,6 @@
# -*- coding: utf-8 -*-
require File.join(File.dirname(__FILE__), 'helper')
-Geokit::Geocoders::google_client_id = nil
-Geokit::Geocoders::google_cryptographic_key = nil
-Geokit::Geocoders::google_channel = nil
-
class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
def setup
@@ -32,11 +28,11 @@ def test_google_signature
# Example from:
# https://developers.google.com/maps/documentation/business/webservices#signature_examples
def test_google_signature_and_url
- Geokit::Geocoders::google_client_id = 'clientID'
- Geokit::Geocoders::google_cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
+ Geokit::Geocoders::GoogleGeocoder.client_id = 'clientID'
+ Geokit::Geocoders::GoogleGeocoder.cryptographic_key = 'vNIXE0xscrmjlyV-12Nj_BvUPaw='
url = Geokit::Geocoders::GoogleGeocoder.send(:submit_url, '/maps/api/geocode/json?address=New+York&sensor=false')
- Geokit::Geocoders::google_client_id = nil
- Geokit::Geocoders::google_cryptographic_key = nil
+ Geokit::Geocoders::GoogleGeocoder.client_id = nil
+ Geokit::Geocoders::GoogleGeocoder.cryptographic_key = nil
assert_equal 'http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=', url
end
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'helper')
-Geokit::Geocoders::geocoder_us = nil
+Geokit::Geocoders::UsGeocoder.key = nil
class UsGeocoderTest < BaseGeocoderTest #:nodoc: all
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), 'helper')
-Geokit::Geocoders::yahoo_consumer_key = 'dj0yJmk9cXByQVN2WHZmTVhDJmQ9WVdrOVZscG1WVWhOTldrbWNHbzlNakF6TlRJME16UTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0zNg--'
-Geokit::Geocoders::yahoo_consumer_secret = 'SECRET'
+Geokit::Geocoders::YahooGeocoder.key = 'dj0yJmk9cXByQVN2WHZmTVhDJmQ9WVdrOVZscG1WVWhOTldrbWNHbzlNakF6TlRJME16UTJNZy0tJnM9Y29uc3VtZXJzZWNyZXQmeD0zNg--'
+Geokit::Geocoders::YahooGeocoder.secret = 'SECRET'
class YahooGeocoderTest < BaseGeocoderTest #:nodoc: all
def setup
@@ -1,8 +1,6 @@
# encoding: UTF-8
require File.join(File.dirname(__FILE__), 'helper')
-# Geokit::Geocoders::yandex = 'yandex'
-
class YandexGeocoderTest < BaseGeocoderTest #:nodoc: all
YANDEX_FULL=<<-EOF.strip
{"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"Москва, улица Новый Арбат, дом 24","found":"1","results":"10"}},"featureMember":[{"GeoObject":{"metaDataProperty":{"GeocoderMetaData":{"kind":"house","text":"Россия, Москва, улица Новый Арбат, 24","precision":"exact","AddressDetails":{"Country":{"AddressLine":"Москва, улица Новый Арбат, 24","CountryNameCode":"RU","CountryName":"Россия","Locality":{"LocalityName":"Москва","Thoroughfare":{"ThoroughfareName":"улица Новый Арбат","Premise":{"PremiseNumber":"24"}}}}}}},"description":"Москва, Россия","name":"улица Новый Арбат, 24","boundedBy":{"Envelope":{"lowerCorner":"37.585565 55.751928","upperCorner":"37.589662 55.754238"}},"Point":{"pos":"37.587614 55.753083"}}}]}}}

0 comments on commit 33c2c4a

Please sign in to comment.