class Locator
Constants
- GEOCODE_URL
Attributes
location[R]
Public Class Methods
new(location, options = {})
click to toggle source
# File lib/meteorologist/locator.rb, line 5 def initialize(location, options = {}) data = options.fetch(:data) { get_data_from_api(location) } @location = location @navigation_data = validate_and_parse(data) @cache_class = options.fetch(:cache_class) { LocationCache } end
Public Instance Methods
coordinates()
click to toggle source
# File lib/meteorologist/locator.rb, line 12 def coordinates "#{geometry['location']['lat']},#{geometry['location']['lng']}" end
name()
click to toggle source
# File lib/meteorologist/locator.rb, line 16 def name navigation_data['formatted_address'] end
write_to_cache()
click to toggle source
# File lib/meteorologist/locator.rb, line 20 def write_to_cache @cache_class.write(location, attributes_for_cache) end
Private Instance Methods
attributes_for_cache()
click to toggle source
# File lib/meteorologist/locator.rb, line 27 def attributes_for_cache { coordinates: coordinates, name: name } end
geometry()
click to toggle source
# File lib/meteorologist/locator.rb, line 34 def geometry navigation_data['geometry'] end
get_data_from_api(location)
click to toggle source
# File lib/meteorologist/locator.rb, line 38 def get_data_from_api(location) `curl -s "#{GEOCODE_URL}/json?address=#{location}&key=#{ENV['GOOGLEMAPSSECRET']}"` end
validate_and_parse(data)
click to toggle source
# File lib/meteorologist/locator.rb, line 42 def validate_and_parse(data) response = JSON.parse(data) raise ArgumentError, "Location not found" if response['results'].empty? response['results'][0] end