class Seed::Alerts def self.call(...) new(...).call end def call ParksByState.distinct.pluck(:state).each do |state| relevant_park_codes = ParksByState.where(state: state).pluck(:park_code) offset, total = 0, 1 while offset < total do response_body = NpsClient.current.alerts(state: state, offset: offset).body offset = response_body['start'].to_i + response_body['limit'].to_i total = response_body['total'].to_i alerts = response_body['data'] .pluck('id', 'category', 'description', 'lastIndexedDate', 'parkCode', 'title', 'url') .map do |identifier, category, description, indexed_date, park_code, title, url| { identifier:, category:, description:, indexed_date:, park_code:, title:, url: } end .select { relevant_park_codes.include?(it[:park_code]) } Alert.upsert_all(alerts, unique_by: :identifier) Rails.logger.info("Upserted #{alerts.count} alerts for #{state}") end end end end