Created
March 20, 2013 04:26
-
-
Save dangalipo/5202305 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #spec/support/geocoding.rb | |
| # Disable geocoding when testing | |
| module Geocoder | |
| class StubbedResult | |
| def types | |
| ["street_address"] | |
| end | |
| def geometry | |
| { "location" => { "lat" => 32.87324, "lng" => 115.0983 } } | |
| end | |
| end | |
| def search(param) | |
| [ StubbedResult.new ] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module HasLocation | |
| extend ActiveSupport::Concern | |
| # ... | |
| def geocode! | |
| address = self.formatted_address(format: :singleline).to_str | |
| result = Geocoder.search(address).first | |
| set_location_from_geocode(result) or errors.add(:address_line_1, "must be a valid address") | |
| end | |
| def set_location_from_geocode(result) | |
| return unless result | |
| return unless result.types | |
| return if (result.types & ["street_address", "premise", "subpremise"]).empty? | |
| return unless (location = result.geometry["location"]) | |
| self.latitude = location["lat"] | |
| self.longitude = location["lng"] | |
| pack_location | |
| end | |
| #... | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Venue < ActiveRecord::Base | |
| include HasAddress | |
| include HasLocation | |
| # ... | |
| before_validation :geocode! | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "spec_helper" | |
| describe Api::V1::VenuesController do | |
| describe "#index" do | |
| subject { get :index, params } | |
| context "with required parameters" do | |
| let(:active_matching_venue) { FactoryGirl.create(:subscription, :active).account.venues.first } | |
| let(:active_non_matching_venue) { FactoryGirl.create(:subscription, :active).account.venues.first } | |
| let(:inactive_venue) { FactoryGirl.create(:subscription, :inactive).account.venues.first } | |
| let(:suspended_venue) { FactoryGirl.create(:subscription, :suspended).account.venues.first } | |
| let(:params) { { latitude: active_matching_venue.latitude, longitude: active_matching_venue.longitude, name: name } } | |
| let(:name) { nil } | |
| before(:each) do | |
| active_non_matching_venue.latitude = 0.0 | |
| active_non_matching_venue.longitude = 0.0 | |
| active_non_matching_venue.pack_location | |
| active_non_matching_venue.save | |
| debugger | |
| true | |
| end | |
| it { subject; assigns(:venues).should include active_matching_venue } | |
| it { subject; assigns(:venues).should_not include active_non_matching_venue } | |
| it { subject; assigns(:venues).should_not include inactive_venue } | |
| it { subject; assigns(:venues).should_not include suspended_venue } | |
| specify { Venue.should_receive(:search).with(include("latitude", "longitude")).and_return(Venue.scoped); subject } | |
| it { should be_successful } | |
| context 'and optional name field' do | |
| let(:name) { matching_active_venue.name } | |
| end | |
| end | |
| context "without required parameters" do | |
| let(:params) { {} } | |
| it { should be_unprocessable_entity } | |
| it { should set_json_error "latitude and longitude are required parameters" } | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Api::V1::VenuesController < Api::V1::ApplicationController | |
| def index | |
| unless (params[:latitude] && params[:longitude]) | |
| render_unprocessable_entity("latitude and longitude are required parameters") and return | |
| end | |
| @venues = Venue.with_active_account.search(params) | |
| render json: { venues: ApiVenueDecorator.decorate_collection(@venues) } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment