Add seed parks service
This commit is contained in:
21
app/services/seed/parks.rb
Normal file
21
app/services/seed/parks.rb
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
class Seed::Parks
|
||||||
|
def self.call(...)
|
||||||
|
new(...).call
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
offset, total = 0, 1
|
||||||
|
while offset < total do
|
||||||
|
response_body = NpsClient.current.parks(offset: offset).body
|
||||||
|
offset = response_body['start'].to_i + response_body['limit'].to_i
|
||||||
|
total = response_body['total'].to_i
|
||||||
|
parks = response_body['data']
|
||||||
|
.pluck('parkCode', 'name', 'states')
|
||||||
|
.map do |code, name, states|
|
||||||
|
{ code:, name:, states: }
|
||||||
|
end
|
||||||
|
Park.upsert_all(parks, unique_by: :code)
|
||||||
|
Rails.logger.info("Upserted #{parks.count} national parks")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user