Commit ead941b

mo khan <mo@mokhan.ca>
2016-05-03 15:56:18
use yelp api to import gyms.
1 parent 69f3f8d
app/controllers/gyms_controller.rb
@@ -1,8 +1,13 @@
 class GymsController < ApplicationController
   before_action { @search_path = gyms_path }
+  before_action only: [:index] { @remote_search = true }
 
   def index
-    @gyms = Gym.search_with(params).closest_to(current_session.location).includes(:location)
+    @gyms = Gym.
+      includes(:location).
+      search_with(params).
+      closest_to(current_session.location).
+      order(:name)
   end
 
   def new
app/helpers/application_helper.rb
@@ -7,7 +7,7 @@ module ApplicationHelper
       class: "gravatar"
   end
 
-  def search_form(search_path: @search_path || dashboard_path, remote: false)
+  def search_form(search_path: @search_path || dashboard_path, remote: @remote_search)
     form_tag search_path, id: "search-form", method: :get, remote: remote do
       search_field_tag :q, params[:q], placeholder: t(:search)
     end
app/models/gym.rb
@@ -28,4 +28,27 @@ class Gym < ActiveRecord::Base
       all
     end
   end
+
+  def self.search_yelp(city: "Calgary", page: 1, page_size: 20)
+    offset = (page * page_size) - page_size
+    Yelp.client.search(city, {
+      category_filter: 'gyms',
+      limit: page_size,
+      offset: offset,
+      term: 'gym',
+    }).businesses.map do |result|
+      Gym.new(
+        name: result.name,
+        location_attributes: {
+          address: result.location.address.first,
+          city: result.location.city,
+          postal_code: result.location.postal_code,
+          region: result.location.state_code,
+          country: result.location.country_code,
+          latitude: result.location.coordinate.try(:latitude),
+          longitude: result.location.coordinate.try(:longitude),
+        }
+      )
+    end
+  end
 end
bin/yelp
@@ -0,0 +1,12 @@
+#!/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
+cities = ['Calgary', 'Edmonton']
+cities.each do |city|
+  (1..5).each do |page|
+    puts "Searching #{city}, page: #{page}"
+    Gym.search_yelp(city: city, page: page).each(&:save!)
+  end
+end
config/initializers/yelp.rb
@@ -0,0 +1,6 @@
+Yelp.client.configure do |config|
+  config.consumer_key = ENV["YELP_CONSUMER_KEY"]
+  config.consumer_secret = ENV["YELP_CONSUMER_SECRET"]
+  config.token = ENV["YELP_TOKEN"]
+  config.token_secret = ENV["YELP_TOKEN_SECRET"]
+end
spec/models/gym_spec.rb
@@ -80,4 +80,20 @@ describe Gym do
       expect(results).to match_array([portland_gym])
     end
   end
+
+  describe ".search_yelp", skip: !ENV['YELP_CONSUMER_KEY'].present? do
+    it 'returns results' do
+      results = Gym.search_yelp(city: "Calgary")
+      expect(results).to be_present
+      expect(results.count).to be > 0
+      expect(results.first).to be_instance_of(Gym)
+    end
+
+    it 'returns the next page of results' do
+      results = Gym.search_yelp(city: "Calgary", page: 2)
+      expect(results).to be_present
+      expect(results.count).to be > 0
+      expect(results.first).to be_instance_of(Gym)
+    end
+  end
 end
.env.example
@@ -6,3 +6,7 @@ SMTP_HOST=''
 SMTP_PASSWORD=''
 SMTP_PORT=''
 SMTP_USERNAME=''
+YELP_CONSUMER_KEY=''
+YELP_CONSUMER_SECRET=''
+YELP_TOKEN=''
+YELP_TOKEN_SECRET=''
Gemfile
@@ -65,6 +65,7 @@ source 'https://rubygems.org' do
   gem 'turbolinks'
   gem 'uglifier', '>= 1.3.0'
   gem 'web-console', '~> 2.0', group: :development
+  gem 'yelp'
 end
 
 source 'https://rails-assets.org' do
Gemfile.lock
@@ -149,6 +149,10 @@ GEM
     factory_girl_rails (4.7.0)
       factory_girl (~> 4.7.0)
       railties (>= 3.0.0)
+    faraday (0.9.2)
+      multipart-post (>= 1.2, < 3)
+    faraday_middleware (0.10.0)
+      faraday (>= 0.7.4, < 0.10)
     fast_stack (0.2.0)
     ffaker (2.2.0)
     flamegraph (0.1.0)
@@ -223,6 +227,7 @@ GEM
     minitest (5.8.4)
     multi_json (1.11.3)
     multi_test (0.1.2)
+    multipart-post (2.0.0)
     net-scp (1.2.1)
       net-ssh (>= 2.6.5)
     net-ssh (3.1.1)
@@ -338,6 +343,7 @@ GEM
       concurrent-ruby (~> 1.0)
       connection_pool (~> 2.2, >= 2.2.0)
       redis (~> 3.2, >= 3.2.1)
+    simple_oauth (0.3.1)
     simplecov (0.11.2)
       docile (~> 1.1.0)
       json (~> 1.8)
@@ -403,6 +409,10 @@ GEM
     websocket-extensions (0.1.2)
     xpath (2.0.0)
       nokogiri (~> 1.3)
+    yelp (2.1.2)
+      faraday (~> 0.8, >= 0.8.0)
+      faraday_middleware (~> 0.8, >= 0.8.0)
+      simple_oauth (~> 0.3.1)
 
 PLATFORMS
   ruby
@@ -475,6 +485,7 @@ DEPENDENCIES
   turbolinks!
   uglifier (>= 1.3.0)!
   web-console (~> 2.0)!
+  yelp!
 
 BUNDLED WITH
    1.12.1