lib/algolia_places.rb in algolia_places-0.1.1 vs lib/algolia_places.rb in algolia_places-0.1.2
- old
+ new
@@ -11,10 +11,11 @@
include Singleton
class << self
extend Forwardable
def_delegators :instance, :coordinates, :coordinates
+ def_delegators :instance, :hits, :hits
def_delegators :instance, :configuration, :configuration
def root
File.expand_path '../..', __FILE__
end
@@ -41,21 +42,41 @@
end
def coordinates(query)
begin
resp = retrieve_query(query)
- validate_response(resp)
+ coordinates_response(resp)
rescue RestClient::ExceptionWithResponse => err
if self.rest_exception?
raise err
else
self.logger.fatal("#{err}\n#{err.backtrace.inspect}")
default_coords
end
end
end
+
+ def hits(query)
+ begin
+ resp = retrieve_query(query)
+ hits_response(resp)
+ rescue RestClient::ExceptionWithResponse => err
+ if self.rest_exception?
+ raise err
+ else
+ self.logger.fatal("#{err}\n#{err.backtrace.inspect}")
+ []
+ end
+ end
+ end
- def validate_response(resp)
+ protected
+
+ def hits_response(resp)
+ JsonPath.new('$.hits').on(resp.body).first
+ end
+
+ def coordinates_response(resp)
results = JsonPath.new('$.hits[0]._geoloc').on resp.body
geo_loc = results.first
if geo_loc.nil?
default_coords
else