Skip to content

Instantly share code, notes, and snippets.

@8vius
Created November 25, 2014 20:24
Show Gist options
  • Save 8vius/08668e52cc6fc327baf4 to your computer and use it in GitHub Desktop.
Save 8vius/08668e52cc6fc327baf4 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
@venues = @venues.creator(params[:user_id]) if params[:user_id].present?
@venues = @venues.private_venues(params[:private_venue]) if params[:private_venue].present?
@venues = @venues.search_by_categories(params[:category]) if params[:category].present?
@venues = @venues.search(params[:query]) if params[:query].present?
@venues = @venues.near(params[:ll]) if params[:ll].present?
@venues = @venues.after(params[:after]) if params[:after].present?
@venues = @venues.before(params[:before]) if params[:before].present?
if @venues.to_a.count < 30 and params[:ll].present?
foursquare_client = foursquare_client()
fq_venues = foursquare_client.explore_venues(ll: params[:ll], radius: 1000, limit: 30 - @venues.count(:all), section: 'food', query: (params[:query].present? ? params[:query] : ''))
for item in fq_venues.groups[0].items
fq_venue = item.venue
location = Location.build_from_foursquare(fq_venue)
@venues << @venues.build(name: fq_venue.name, foursquare_id: fq_venue.id, location: location)
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