Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolten/9755082 to your computer and use it in GitHub Desktop.
Save kevincolten/9755082 to your computer and use it in GitHub Desktop.
Rails CORS
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :cor
def cor
headers["Access-Control-Allow-Origin"] = "http://localhost:8000"
headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",")
headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",")
head(:ok) if request.request_method == "OPTIONS"
end
def nearby_businesses(address)
address = address.split(' ').join('+')
geocode_url = "https://maps.googleapis.com/maps/api/geocode/json?"
geocode_url += "address=#{address}&"
geocode_url += "sensor=false"
geocode_response = RestClient.get(URI.encode(geocode_url.strip))
location_hash = JSON.parse(geocode_response)["results"][0]["geometry"]["location"]
coordinates = "#{location_hash["lat"]},#{location_hash["lng"]}"
logger.info(coordinates);
places_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
places_url += "key=#{ENV['GOOGLE_PUBLIC_API_KEY']}"
places_url = [places_url,
"location=#{coordinates}",
"sensor=false",
"language=en",
"rankby=distance",
"types=bar|restaurant|cafe"].join("&")
places_response = RestClient.get(URI.encode(places_url.strip))
places_hash = JSON.parse(places_response)
end
def businessDetails(business_reference)
business_url = "https://maps.googleapis.com/maps/api/place/details/json?"
business_url += "key=#{ENV['GOOGLE_PUBLIC_API_KEY']}&"
business_url += "reference=#{business_reference}&"
business_url += "sensor=false"
business_response = RestClient.get(URI.encode(business_url.strip))
JSON.parse(business_response)['result']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment