Class: AskGeo
- Inherits:
-
Object
- Object
- AskGeo
- Defined in:
- lib/ask_geo.rb
Defined Under Namespace
Classes: APIError
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #base_url ⇒ Object
- #format_url(points) ⇒ Object
-
#initialize(opts = {}) ⇒ AskGeo
constructor
A new instance of AskGeo.
- #lookup(points) ⇒ Object
- #serialize_point(point) ⇒ Object
- #serialize_points(points) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ AskGeo
Returns a new instance of AskGeo.
11 12 13 14 |
# File 'lib/ask_geo.rb', line 11 def initialize(opts = {}) @account_id = opts[:account_id] @api_key = opts[:api_key] end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
9 10 11 |
# File 'lib/ask_geo.rb', line 9 def account_id @account_id end |
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/ask_geo.rb', line 9 def api_key @api_key end |
Instance Method Details
#base_url ⇒ Object
16 17 18 |
# File 'lib/ask_geo.rb', line 16 def base_url "http://www.askgeo.com/api" end |
#format_url(points) ⇒ Object
35 36 37 |
# 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) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# 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) ⇒ Object
30 31 32 33 |
# 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 |