Commit 032defa

mo khan <mo@mokhan.ca>
2016-05-28 22:11:10
convert search to a static gateway
1 parent 890fc6b
Changed files (5)
app/models/gym.rb
@@ -43,8 +43,8 @@ class Gym < ActiveRecord::Base
   end
 
   def self.search_yelp(q: "gym", categories: ["gyms"], city: , page: 1, per_page: 20)
-    Search.yelp(q, categories, city, page, per_page) do |result|
-      map_from(result)
+    Search.yelp.for(q, city, categories, page, per_page) do |result|
+      Gym.map_from(result)
     end
   end
 
@@ -65,8 +65,7 @@ class Gym < ActiveRecord::Base
   end
 
   def self.create_from_yelp!(id)
-    Gym.find_by(yelp_id: id) ||
-      Gym.map_from(::Yelp.client.business(id).business)
+    Gym.find_by(yelp_id: id) || Gym.map_from(Search.yelp.for_business(id))
   end
 
   def self.import(city, pages: 5)
app/models/search.rb
@@ -6,7 +6,7 @@ class Search
       @client = client
     end
 
-    def search(q, city, categories = [], page = 1, per_page = 20, &block)
+    def for(q, city, categories = [], page = 1, per_page = 20, &block)
       return paginate([]) if city.blank?
 
       cache(key: key_for(q, city, page, per_page)) do
@@ -14,6 +14,10 @@ class Search
       end
     end
 
+    def for_business(yelp_id)
+      client.business(yelp_id).try(:business)
+    end
+
     private
 
     def key_for(*args)
@@ -45,7 +49,7 @@ class Search
     end
   end
 
-  def self.yelp(q, categories = [], city, page, per_page, &block)
-    Yelp.new.search(q, city, categories, page, per_page, &block)
+  def self.yelp
+    @yelp ||= Yelp.new
   end
 end
spec/controllers/gyms_controller_spec.rb
@@ -29,7 +29,9 @@ describe GymsController do
 
     it "returns matches from yelp" do
       gym = build(:gym)
-      allow(Search).to receive(:yelp).and_return(Kaminari.paginate_array([gym]))
+      yelp = double
+      allow(Search).to receive(:yelp).and_return(yelp)
+      allow(yelp).to receive(:for).and_return(Kaminari.paginate_array([gym]))
       get :index, q: "sait", source: "yelp"
 
       expect(assigns(:gyms)).to match_array([gym])
spec/features/profiles_spec.rb
@@ -31,6 +31,7 @@ feature "Profiles", type: :feature do
 
     it "allows me to edit my profile" do
       subject.change(gender: :male, social_tolerance: :low)
+      subject.save_changes
 
       expect(page).to have_content(user.username)
       expect(page).to have_content(
@@ -42,17 +43,10 @@ feature "Profiles", type: :feature do
       gym = build(:gym)
       allow(Gym).to receive(:create_from_yelp!).and_return(gym)
 
-      link_text = I18n.translate("profiles.edit.choose_home_gym")
-      subject.click_link(link_text)
-      within("#gym-search form") do
-        fill_in "q", with: "sait"
-        fill_in "city", with: "calgary"
-        click_button("Search")
-      end
-      subject.wait_for_ajax
-      subject.click_button("Mine")
-      subject.wait_for_ajax
+      subject.click_link(I18n.t("profiles.edit.choose_home_gym"))
+      subject.choose_home_gym(city: "calgary", name: "sait")
       subject.save_changes
+
       expect(page).to have_content(gym.name)
     end
   end
spec/support/pages/edit_profile_page.rb
@@ -10,7 +10,17 @@ class EditProfilePage < PageModel
       page.choose(gender.to_s.titleize)
       page.choose(social_tolerance.to_s.titleize)
     end
-    save_changes
+  end
+
+  def choose_home_gym(city:, name:)
+    within("#gym-search form") do
+      fill_in "q", with: name
+      fill_in "city", with: city
+      click_button("Search")
+    end
+    wait_for_ajax
+    click_button("Mine")
+    wait_for_ajax
   end
 
   def save_changes