diff --git a/README.md b/README.md index 2f69b2d66e28387e90d6f46024170d5a2e7671f4..0d55fda569a81161fd9ff418e9af46f410145e49 100644 --- a/README.md +++ b/README.md @@ -578,6 +578,17 @@ Data Science Toolkit provides an API whose reponse format is like Google's but w * **Limitations**: ? * **Notes**: If you are [running your own local instance of the FreeGeoIP service](https://github.com/fiorix/freegeoip) you can configure the host like this: `Geocoder.configure(freegeoip: {host: "..."})`. +#### Telize (`:telize`) + +* **API key**: none +* **Quota**: none +* **Region**: world +* **SSL support**: no +* **Languages**: English +* **Documentation**: http://www.telize.com/ +* **Terms of Service**: ? +* **Limitations**: ? + #### MaxMind Web Services (`:maxmind`) * **API key**: required diff --git a/lib/geocoder/lookup.rb b/lib/geocoder/lookup.rb index 2250e10cfc39ea03fcffdf2c145e877cfa21ed60..dbfa651959a1ee1a0c2a7098a746469df85df11d 100644 --- a/lib/geocoder/lookup.rb +++ b/lib/geocoder/lookup.rb @@ -47,10 +47,11 @@ module Geocoder # def ip_services [ + :baidu_ip, :freegeoip, :maxmind, :maxmind_local, - :baidu_ip + :telize ] end diff --git a/lib/geocoder/lookups/telize.rb b/lib/geocoder/lookups/telize.rb new file mode 100644 index 0000000000000000000000000000000000000000..4c284a65bbdeac28b1126c7ef764581f38e1cc21 --- /dev/null +++ b/lib/geocoder/lookups/telize.rb @@ -0,0 +1,45 @@ +require 'geocoder/lookups/base' +require 'geocoder/results/telize' + +module Geocoder::Lookup + class Telize < Base + + def name + "Telize" + end + + def query_url(query) + #currently doesn't support HTTPS + "http://www.telize.com/geoip/#{query.sanitized_text}" + end + + private # --------------------------------------------------------------- + + def results(query) + # don't look up a loopback address, just return the stored result + return [reserved_result(query.text)] if query.loopback_ip_address? + # note: Telize returns json with a code attribute of 401 on bad request + doc = fetch_data(query) + (doc && doc['code'] == 401) ? [] : [doc] + end + + def reserved_result(ip) + { + "ip" => ip, + "city" => "", + "region_code" => "", + "region_name" => "", + "metrocode" => "", + "zipcode" => "", + "latitude" => "0", + "longitude" => "0", + "country_name" => "Reserved", + "country_code" => "RD" + } + end + + def host + configuration[:host] || "telize.net" + end + end +end diff --git a/lib/geocoder/results/telize.rb b/lib/geocoder/results/telize.rb new file mode 100644 index 0000000000000000000000000000000000000000..84aec721e787d54efefc428f521ce249e73315f0 --- /dev/null +++ b/lib/geocoder/results/telize.rb @@ -0,0 +1,45 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class Telize < Base + + def address(format = :full) + s = state_code.to_s == "" ? "" : ", #{state_code}" + "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "") + end + + def city + @data['city'] + end + + def state + @data['region_name'] + end + + def state_code + @data['region_code'] + end + + def country + @data['country_name'] + end + + def country_code + @data['country_code'] + end + + def postal_code + @data['zipcode'] + end + + def self.response_attributes + %w[metrocode ip] + end + + response_attributes.each do |a| + define_method a do + @data[a] + end + end + end +end diff --git a/test/fixtures/telize_10_10_10_10 b/test/fixtures/telize_10_10_10_10 new file mode 100644 index 0000000000000000000000000000000000000000..b85a5590334e3afd7ebe5d3e9ccb6eb0d5493c8b --- /dev/null +++ b/test/fixtures/telize_10_10_10_10 @@ -0,0 +1,4 @@ +{ +"message": "Input string is not a valid IP address", +"code": 401 +} diff --git a/test/fixtures/telize_74_200_247_59 b/test/fixtures/telize_74_200_247_59 new file mode 100644 index 0000000000000000000000000000000000000000..78c7ce3e57bcf79534475006a19c0cbeba3422ad --- /dev/null +++ b/test/fixtures/telize_74_200_247_59 @@ -0,0 +1,12 @@ +{ + "city": "Plano", + "region_code": "TX", + "region_name": "Texas", + "metrocode": "623", + "zipcode": "75093", + "longitude": "-96.8134", + "country_name": "United States", + "country_code": "US", + "ip": "74.200.247.59", + "latitude": "33.0347" +} diff --git a/test/fixtures/telize_no_results b/test/fixtures/telize_no_results new file mode 100644 index 0000000000000000000000000000000000000000..b85a5590334e3afd7ebe5d3e9ccb6eb0d5493c8b --- /dev/null +++ b/test/fixtures/telize_no_results @@ -0,0 +1,4 @@ +{ +"message": "Input string is not a valid IP address", +"code": 401 +} diff --git a/test/test_helper.rb b/test/test_helper.rb index d872af3d1c85eaf88d23d5e8e5112744747ba3e5..06c1a8b4caf15299de97a1f4cc26b58d5c8d4009 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -143,6 +143,17 @@ module Geocoder end end + class Telize + private + def fixture_prefix + "telize" + end + + def default_fixture_filename + "telize_74_200_247_59" + end + end + class Maxmind private def default_fixture_filename diff --git a/test/unit/lookup_test.rb b/test/unit/lookup_test.rb index ba7a14a373dd4b67f2a1a944fec6e4ec2bcccf30..d0507afa952ed51cd891cf0c82383aaf227add10 100644 --- a/test/unit/lookup_test.rb +++ b/test/unit/lookup_test.rb @@ -23,7 +23,7 @@ class LookupTest < GeocoderTestCase def test_query_url_contains_values_in_params_hash Geocoder::Lookup.all_services_except_test.each do |l| - next if l == :freegeoip || l == :maxmind_local # does not use query string + next if [:freegeoip, :maxmind_local, :telize].include? l # does not use query string set_api_key!(l) url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new( "test", :params => {:one_in_the_hand => "two in the bush"} diff --git a/test/unit/lookups/telize_test.rb b/test/unit/lookups/telize_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..aa112b2091b29a1bfb90f82fcf12f7ed7bb5b317 --- /dev/null +++ b/test/unit/lookups/telize_test.rb @@ -0,0 +1,25 @@ +# encoding: utf-8 +$: << File.join(File.dirname(__FILE__), "..", "..") +require 'test_helper' + +class TelizeTest < GeocoderTestCase + + def setup + Geocoder.configure(ip_lookup: :telize) + end + + def test_result_on_ip_address_search + result = Geocoder.search("74.200.247.59").first + assert result.is_a?(Geocoder::Result::Telize) + end + + def test_result_components + result = Geocoder.search("74.200.247.59").first + assert_equal "Plano, TX 75093, United States", result.address + end + + def test_no_results + results = Geocoder.search("10.10.10.10") + assert_equal 0, results.length + end +end