Skip to content

Commit

Permalink
raise errors when they occur
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru committed Jan 8, 2014
1 parent e13cf31 commit fcafd72
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/geonames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'addressable/template'

class GeoNames
class APIError < RuntimeError; end

OPTIONS = {
host: 'api.geonames.org',
time_format: '%Y-%m-%d %T %z',
Expand Down Expand Up @@ -39,10 +41,17 @@ def query(name, parameters)

uri = uris[name].expand(default.merge(parameters))

if block_given?
open(uri.to_s){|io| yield(io.read) }
result =
if block_given?
open(uri.to_s){|io| yield(io.read) }
else
open(uri.to_s){|io| JSON.parse(io.read) }
end

if status = result["status"]
raise APIError, status.inspect
else
open(uri.to_s){|io| JSON.parse(io.read) }
result
end
end

Expand Down

0 comments on commit fcafd72

Please sign in to comment.