Add naive impl of seeding alerts
This commit is contained in:
@@ -7,6 +7,10 @@ class NpsClient
|
||||
conn.get('parks', { start: offset })
|
||||
end
|
||||
|
||||
def alerts(park_code:, offset: 0)
|
||||
conn.get('alerts', { parkCode: park_code, start: offset })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def conn
|
||||
|
||||
23
app/services/seed/alerts.rb
Normal file
23
app/services/seed/alerts.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
class Seed::Alerts
|
||||
def self.call(...)
|
||||
new(...).call
|
||||
end
|
||||
|
||||
def call
|
||||
Park.find_each do |park|
|
||||
offset, total = 0, 1
|
||||
while offset < total do
|
||||
response_body = NpsClient.current.alerts(park_code: park.code, 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
|
||||
Alert.upsert_all(alerts, unique_by: :identifier)
|
||||
Rails.logger.info("Upserted #{alerts.count} alerts for #{park.name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user