Class: WolfCore::GeoLocationDataSource
- Inherits:
-
Object
- Object
- WolfCore::GeoLocationDataSource
- Includes:
- LoggingUtils
- Defined in:
- lib/wolf_core/infrastructure/geo_location_data_source.rb
Instance Method Summary collapse
- #calculate_distance(coord1, coord2) ⇒ Object
- #get_address_coordinates(address_string) ⇒ Object
-
#initialize ⇒ GeoLocationDataSource
constructor
A new instance of GeoLocationDataSource.
Methods included from LoggingUtils
Constructor Details
#initialize ⇒ GeoLocationDataSource
Returns a new instance of GeoLocationDataSource.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/wolf_core/infrastructure/geo_location_data_source.rb', line 10 def initialize api_key = ENV["GOOGLE_MAPS_API_KEY"] if api_key.nil? || api_key.empty? raise ArgumentError, "GOOGLE_MAPS_API_KEY environment variable must be configured" end Geocoder.configure( lookup: :google, api_key: api_key ) end |
Instance Method Details
#calculate_distance(coord1, coord2) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wolf_core/infrastructure/geo_location_data_source.rb', line 33 def calculate_distance(coord1, coord2) lat1, lon1 = coord1[:latitude], coord1[:longitude] lat2, lon2 = coord2[:latitude], coord2[:longitude] earth_radius_meters = 6_371_000 lat1_rad = lat1 * Math::PI / 180 lat2_rad = lat2 * Math::PI / 180 delta_lat = (lat2 - lat1) * Math::PI / 180 delta_lon = (lon2 - lon1) * Math::PI / 180 a = Math.sin(delta_lat / 2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(delta_lon / 2)**2 c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) earth_radius_meters * c end |
#get_address_coordinates(address_string) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wolf_core/infrastructure/geo_location_data_source.rb', line 22 def get_address_coordinates(address_string) results = Geocoder.search(address_string) return nil if results.empty? result = results.first { latitude: result.latitude, longitude: result.longitude } rescue StandardError => e log_geocoding_error(address_string, e) nil end |