diff options
| -rw-r--r-- | app/jobs/import_gyms_job.rb | 4 | ||||
| -rw-r--r-- | app/models/gym.rb | 1 | ||||
| -rw-r--r-- | spec/jobs/import_gyms_job_spec.rb | 9 | ||||
| -rw-r--r-- | spec/models/gym_spec.rb | 5 |
4 files changed, 15 insertions, 4 deletions
diff --git a/app/jobs/import_gyms_job.rb b/app/jobs/import_gyms_job.rb index 82898eb..5e77c85 100644 --- a/app/jobs/import_gyms_job.rb +++ b/app/jobs/import_gyms_job.rb @@ -2,6 +2,8 @@ class ImportGymsJob < ActiveJob::Base queue_as :default def perform(location) - Gym.import(location.city) if location.present? + if location.present? && !Gym.closest_to(location).exists? + Gym.import(location.city) + end end end diff --git a/app/models/gym.rb b/app/models/gym.rb index eeeadcb..d39f07f 100644 --- a/app/models/gym.rb +++ b/app/models/gym.rb @@ -61,6 +61,7 @@ class Gym < ActiveRecord::Base end def self.import(city, pages: 5) + return if Rails.env.test? || city.blank? (1..pages).each do |page| Gym.search_yelp(q: 'gym', city: city, page: page).each(&:save!) end diff --git a/spec/jobs/import_gyms_job_spec.rb b/spec/jobs/import_gyms_job_spec.rb index 3605993..69e3969 100644 --- a/spec/jobs/import_gyms_job_spec.rb +++ b/spec/jobs/import_gyms_job_spec.rb @@ -15,4 +15,13 @@ describe ImportGymsJob do subject.perform(nil) expect(Gym).to_not have_received(:import) end + + it 'skips the import of gyms in the city are already present' do + allow(Gym).to receive(:import) + create(:gym, location: location) + + subject.perform(location) + + expect(Gym).to_not have_received(:import) + end end diff --git a/spec/models/gym_spec.rb b/spec/models/gym_spec.rb index c2fc2a5..c8968e7 100644 --- a/spec/models/gym_spec.rb +++ b/spec/models/gym_spec.rb @@ -124,15 +124,14 @@ describe Gym do it "returns true when a dup is found" do subject.location = create(:portland) subject.save! - other = create(:gym, location: create(:portland)) + create(:gym, location: create(:portland)) expect(subject.duplicate?).to be_truthy end it 'returns true when another gym has the same yelp id' do subject.yelp_id = "hello-world" - subject.save! - other = create(:gym, yelp_id: subject.yelp_id) + create(:gym, yelp_id: subject.yelp_id) expect(subject.duplicate?).to be_truthy end |
