Skip to content
Snippets Groups Projects
Commit 631c7670 authored by Luke Roberts's avatar Luke Roberts
Browse files

port freegeopip to telize, still need to handle errors

parent bed0d261
No related branches found
No related tags found
No related merge requests found
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 parse_raw_data(raw_data)
raw_data.match(/^<html><title>404/) ? nil : super(raw_data)
end
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: Freegeoip.net returns plain text "Not Found" on bad request
(doc = fetch_data(query)) ? [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
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
{
"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"
}
<html><title>404: Not Found</title><body>404: Not Found</body></html>
# 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
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment