Skip to content

Instantly share code, notes, and snippets.

@8vius
Created November 21, 2014 18:32
Show Gist options
  • Save 8vius/19803e9ad65efdeb170f to your computer and use it in GitHub Desktop.
Save 8vius/19803e9ad65efdeb170f to your computer and use it in GitHub Desktop.
module API
module V1
class VenuesController < ApiController
before_action :set_venue, except: [:index, :create]
before_action :authenticate, only: [:create, :destroy]
def index
@venues = Venue
if params.has_key?(:ll)
lat, lng = params[:ll].split(',')
@venues = @venues.near(lat, lng)
end
if params.has_key?(:user_id)
@venues = @venues.where(creator_id: params[:user_id])
end
if params.has_key?(:private_venue)
@venues = @venues.where(private_venue: params[:private_venue])
end
if params.has_key?(:category)
@venues = @venues.search_by_categories(params[:category])
end
if params.has_key?(:query)
@venues = @venues.search(params[:query])
end
if params.has_key?(:after)
@venues = @venues.where("venues.id > :after", after: params[:after])
elsif params.has_key?(:before)
@venues = @venues.where("venues.id < :before", before: params[:before])
end
if @venues.to_a.count < 30
foursquare_client = foursquare_client()
fq_venues = foursquare_client.explore_venues(ll: params[:ll], radius: 1000, limit: 30 - @venues.count(:all), section: 'food')
for item in fq_venues.groups[0].items
fq_venue = item.venue
location = Location.build_from_foursquare(fq_venue)
venue = @venues.build(name: fq_venue.name, foursquare_id: fq_venue.id, location: location)
venue.id = nil
@venues << venue
end
end
render :index, status: :ok
end
def show
render :show, status: :ok
end
def create
if venue_params.has_key?(:foursquare_id) and @venue = Venue.find_by(foursquare_id: venue_params[:foursquare_id])
render :show, status: :ok
else
if venue_params.has_key?(:foursquare_id)
foursquare_client = foursquare_client(current_user.oauth_token_for('foursquare'))
foursquare_venue = foursquare_client.venue(venue_params[:foursquare_id])
location = Location.create_from_foursquare(foursquare_venue)
@venue = Venue.new(venue_params)
@venue.location = location
for category in foursquare_venue[:categories]
@venue.categories.build(name: category[:name])
end
elsif venue_params.has_key?(:location_attributes)
location = Location.create!(venue_params[:location_attributes])
@venue = Venue.new(venue_params.except!(:location_attributes))
@venue.location = location
else
@venue = Venue.new(venue_params)
end
if @venue.save
render :show, status: :created
else
render json: @venue.errors, status: :unprocessable_entity
end
end
end
def destroy
end
private
def set_venue
@venue = Venue.find(params[:id])
end
def venue_params
params.require(:venue).permit(:name,
:foursquare_id,
:creator_id,
:owner_id,
:private_venue,
location_attributes: [:lat, :lng, :country, :state, :city, :street, :postal_code],
categories_attributes: [:name],
contact_attributes: [:phone, :email, :website, :twitter])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment