Module: NoaaWeatherClient::Services::FindNearestStation

Defined in:
lib/noaa_weather_client/services/find_nearest_station.rb

Class Method Summary collapse

Class Method Details

.find(lat, lon, stations, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/noaa_weather_client/services/find_nearest_station.rb', line 6

def self.find(lat, lon, stations, options = {})
  calc = options.fetch(:calculator, CalculateDistanceBetweenLatLon)
  filter = options.fetch(:filter, nil)
  count = options.fetch(:count, 1)
  stations.select!(&filter) if filter
  stations.sort_by! { |s| calc.get_distance(lat, lon, s.latitude, s.longitude) }
  count == 1 ? stations.first : stations.take(count)
end