diff --git a/README.md b/README.md index fb69392a7aea0978da24e395d1222862c8161f81..94f1da1590902aac94e7dfff31e9bb37922bf17c 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,18 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of * **Terms of Service**: http://geocoder.ca/?terms=1 * **Limitations**: "Under no circumstances can our data be re-distributed or re-sold by anyone to other parties without our written permission." +#### Geocoder.us (`:geocoder_us`) + +* **API key**: HTTP Basic Auth +* **Sign up**: http://geocoder.us/user/signup +* **Quota**: You can purchase 20,000 credits at a time for $50 +* **Region**: US +* **SSL support**: no +* **Languages**: English +* **Documentation**: http://geocoder.us/help/ +* **Terms of Service**: http://geocoder.us/terms.shtml +* **Limitations**: ? + #### Mapquest (`:mapquest`) * **API key**: required @@ -538,7 +550,7 @@ For example: after_validation :reverse_geocode, :if => :has_coordinates after_validation :geocode, :if => :has_location, :unless => :has_coordinates - end + end Use Outside of Rails -------------------- @@ -641,7 +653,7 @@ Notes on Non-Rails Frameworks If you are using Geocoder with ActiveRecord and a framework other than Rails (like Sinatra or Padrino) you will need to add this in your model before calling Geocoder methods: - extend Geocoder::Model::ActiveRecord + extend Geocoder::Model::ActiveRecord Optimisation of Distance Queries -------------------------------- @@ -740,7 +752,7 @@ When reporting an issue, please list the version of Geocoder you are using and a Known Issue ----------- -You cannot use the `near` scope with another scope that provides an `includes` option because the `SELECT` clause generated by `near` will overwrite it (or vice versa). +You cannot use the `near` scope with another scope that provides an `includes` option because the `SELECT` clause generated by `near` will overwrite it (or vice versa). Instead of using `includes` to reduce the number of database queries, try using `joins` with either the `:select` option or a call to `preload`. For example: diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index 6a6ee407e67b60b16fb2999b229a79ef01a89514..e8d6b96b0cc3dc48af8533624399d1d721712f9b 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -28,6 +28,7 @@ module Geocoder :yahoo, :bing, :geocoder_ca, + :geocoder_us, :yandex, :nominatim, :mapquest, diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index bc4703a275459b5501da5f43d7fc8a25d5c3969d..b6208d15dc86a9092f444df060cd3223ca9ea3dc 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -72,7 +72,7 @@ module Geocoder def query_url(query) fail end - + ## # The working Cache object. # @@ -227,9 +227,16 @@ module Geocoder def make_api_request(query) timeout(configuration.timeout) do uri = URI.parse(query_url(query)) - client = http_client.new(uri.host, uri.port) - client.use_ssl = true if configuration.use_https - client.get(uri.request_uri, configuration.http_headers) + # client = http_client.new(uri.host, uri.port) + # client.use_ssl = true if configuration.use_https + # client.get(uri.request_uri, configuration.http_headers) + + http_client.start(uri.host, uri.port) do |client| + client.use_ssl = true if configuration.use_https + req = Net::HTTP::Get.new(uri.request_uri, configuration.http_headers) + req.basic_auth(uri.user, uri.password) if uri.user and uri.password + client.request(req) + end end end diff --git a/lib/geocoder/lookups/geocoder_us.rb b/lib/geocoder/lookups/geocoder_us.rb new file mode 100644 index 0000000000000000000000000000000000000000..be4aef5ff8801a098c8b2306cc3b9c8eeba7e60e --- /dev/null +++ b/lib/geocoder/lookups/geocoder_us.rb @@ -0,0 +1,39 @@ +require 'geocoder/lookups/base' +require "geocoder/results/geocoder_us" + +module Geocoder::Lookup + class GeocoderUs < Base + + def name + "Geocoder.us" + end + + def query_url(query) + if configuration.api_key + "http://#{configuration.api_key}@geocoder.us/member/service/csv/geocode?" + url_query_string(query) + else + "http://geocoder.us/service/csv/geocode?" + url_query_string(query) + end + end + + private + + def results(query) + return [] unless doc = fetch_data(query) + if doc.to_s =~ /^(\d+)\:/ + return [] + else + return [doc.size == 5 ? ((doc[0..1] << nil) + doc[2..4]) : doc] + end + end + + def query_url_params(query) + (query.text =~ /^\d{5}(?:-\d{4})?$/ ? {:zip => query} : {:address => query.sanitized_text}).merge(super) + end + + def parse_raw_data(raw_data) + raw_data.chomp.split(',') + end + end +end + diff --git a/lib/geocoder/results/geocoder_us.rb b/lib/geocoder/results/geocoder_us.rb new file mode 100644 index 0000000000000000000000000000000000000000..ca20ad418a82887f8e2877a237438719b200eb01 --- /dev/null +++ b/lib/geocoder/results/geocoder_us.rb @@ -0,0 +1,39 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class GeocoderUs < Base + def coordinates + [@data[0].to_f, @data[1].to_f] + end + + def address(format = :full) + "#{street_address}, #{city}, #{state} #{postal_code}, #{country}".sub(/^[ ,]*/, "") + end + + def street_address + @data[2] + end + + def city + @data[3] + end + + def state + @data[4] + end + + alias_method :state_code, :state + + def postal_code + @data[5] + end + + def country + 'United States' + end + + def country_code + 'US' + end + end +end diff --git a/test/fixtures/geocoder_us_madison_square_garden b/test/fixtures/geocoder_us_madison_square_garden new file mode 100644 index 0000000000000000000000000000000000000000..546df368221885513811d652d42c99f061787e0d --- /dev/null +++ b/test/fixtures/geocoder_us_madison_square_garden @@ -0,0 +1 @@ +40.678107, -73.897460, 4 Pennsylvania Ave, New York, NY, 11207 \ No newline at end of file diff --git a/test/fixtures/geocoder_us_no_results b/test/fixtures/geocoder_us_no_results new file mode 100644 index 0000000000000000000000000000000000000000..d349a8f9109ff7677c13821cfa3d2e95cebeda0c --- /dev/null +++ b/test/fixtures/geocoder_us_no_results @@ -0,0 +1 @@ +2: couldn't find this address! sorry \ No newline at end of file