Commit a435221
Changed files (6)
app
jobs
models
bin
spec
models
app/jobs/import_gyms_job.rb
@@ -0,0 +1,7 @@
+class ImportGymsJob < ActiveJob::Base
+ queue_as :default
+
+ def perform(location)
+ Gym.import(location.city) if location.present?
+ end
+end
app/models/gym.rb
@@ -60,6 +60,12 @@ class Gym < ActiveRecord::Base
end
end
+ def self.import(city, pages: 5)
+ (1..pages).each do |page|
+ Gym.search_yelp(q: 'gym', city: city, page: page).each(&:save!)
+ end
+ end
+
def duplicate?(distance: 0.1)
return true if yelp_id.present? && Gym.where.not(id: id).exists?(yelp_id: yelp_id)
Gym.
app/models/user_session.rb
@@ -5,6 +5,10 @@ class UserSession < ActiveRecord::Base
where("accessed_at > ?", 2.weeks.ago).where(revoked_at: nil)
end
+ after_create do
+ ImportGymsJob.perform_later(location)
+ end
+
def revoke!
update_attribute(:revoked_at, Time.current)
end
bin/yelp
@@ -1,16 +1,8 @@
#!/bin/env ruby
-
# bin/rails runner -e development bin/yelp
-#url='https://api.yelp.com/v2/search/?term=gym&location=Calgary&sort=2&limit=20&cc=CA&category_filter=gyms'
-#curl $url
-Gym.delete_all
-Location.delete_all
+
+Gym.destroy_all
cities = ['Calgary', 'Edmonton', 'Portland', 'Victoria', 'Anaheim', 'San Diego', 'Seattle']
cities.each do |city|
- (1..5).each do |page|
- puts "Searching #{city}, page: #{page}"
- gyms = Gym.search_yelp(q: 'gym', city: city, page: page)
- puts " Found: #{gyms.count}, #{gyms.map(&:name)}"
- gyms.each(&:save!)
- end
+ Gym.import(city)
end
spec/jobs/import_gyms_job_spec.rb
@@ -0,0 +1,18 @@
+require "rails_helper"
+
+describe ImportGymsJob do
+ subject { ImportGymsJob.new }
+ let(:location) { build(:portland) }
+
+ it 'imports all the gyms in the city' do
+ allow(Gym).to receive(:import)
+ subject.perform(location)
+ expect(Gym).to have_received(:import).with(location.city)
+ end
+
+ it 'skips the import if no location is present' do
+ allow(Gym).to receive(:import)
+ subject.perform(nil)
+ expect(Gym).to_not have_received(:import)
+ end
+end
spec/models/user_session_spec.rb
@@ -57,4 +57,13 @@ describe UserSession do
expect(UserSession.authenticate('blah')).to be_nil
end
end
+
+ describe "#after_create" do
+ it 'schedules a job to import gyms in city' do
+ allow(ImportGymsJob).to receive(:perform_later)
+ subject.location = create(:portland)
+ subject.save!
+ expect(ImportGymsJob).to have_received(:perform_later).with(subject.location)
+ end
+ end
end