class AskGeo

Attributes

account_id[RW]
api_key[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/ask_geo.rb, line 11
def initialize(opts = {})
  @account_id = opts[:account_id]
  @api_key    = opts[:api_key]
end

Public Instance Methods

base_url() click to toggle source
# File lib/ask_geo.rb, line 16
def base_url
  "http://www.askgeo.com/api"
end
format_url(points) click to toggle source
# File lib/ask_geo.rb, line 35
def format_url(points)
  "#{base_url}/#{account_id}/#{api_key}/timezone.json?points=#{CGI::escape(serialize_points(points))}"
end
lookup(points) click to toggle source
# File lib/ask_geo.rb, line 39
def lookup(points)
  response = JSON.parse(Net::HTTP.get URI.parse(format_url(points)))
  if response['code'] != 0
    raise APIError.new(response['message'] || 'Unknown server error')
  end
  data = response['data']
  data.size == 1 ? data.first : data
rescue JSON::ParserError
  raise APIError.new("Invalid server response")
end
serialize_point(point) click to toggle source
# File lib/ask_geo.rb, line 20
def serialize_point(point)
  case point
  when Hash
    "#{point[:lat]},#{point[:lon]}"
  else
    # Assume this is something whose #to_s makes a "lat,lon" string.
    point.to_s.gsub(/\s+/, '')
  end
end
serialize_points(points) click to toggle source
# File lib/ask_geo.rb, line 30
def serialize_points(points)
  points = [points] if !points.kind_of?(Array)
  points.map{|p| serialize_point(p)}.join(';')
end