Skip to content

Instantly share code, notes, and snippets.

@juan-grases
Last active August 29, 2015 14:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-grases/e9c340f4642ac60d39d6 to your computer and use it in GitHub Desktop.
Save juan-grases/e9c340f4642ac60d39d6 to your computer and use it in GitHub Desktop.
Food establishments by location using Google Places API.

##Description

Dashing widget to display location based food establishments from Google Places API.

##Usage

To use this widget copy places.html, places.coffee and places.scss into the /widgets/places directory. Put the places.rb file in your /jobs folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-id="places" data-view="Places"></div>
</li>

##Settings

  • Google Api Client Key from the API console
  • Latitude and Longitude for your desire location.
  • radius search from the desire location in meters.
  • A new food establishment is fetched every 5 minutes, but you can change that by editing the job schedule
class Dashing.Places extends Dashing.Widget
ready: ->
# This is fired when the widget is done being rendered
onData: (data) ->
$("i.star").attr 'class', "star icon-star-empty"
$("i.rating").attr 'class', "rating"
if data.rating
star_number = 1
while Math.floor(data.rating) >= star_number
$("i#star"+star_number).attr 'class', "star icon-star"
star_number++
diff = star_number - data.rating
if diff <= 0.5
$("i#star"+star_number).attr 'class', "star icon-star-half-empty"
if data.price
price_number = 1
while data.price >= price_number
$("i#price"+price_number).attr 'class', "rating icon-usd"
price_number++
<i class="places"></i>
<h1 class="title" data-bind="title"></h1>
<h3 class="name" data-bind="name"></h3>
<h4 class = "vicinity" data-bind= "vicinity"></h4>
<img data-bind-src='image'/>
<p class="open" data-bind="open"></p>
<div id="rating">
<i id = 'star1' class="star icon-star-empty"></i>
<i id = 'star2' class="star icon-star-empty"></i>
<i id = 'star3' class="star icon-star-empty"></i>
<i id = 'star4' class="star icon-star-empty"></i>
<i id = 'star5' class="star icon-star-empty"></i>
</div>
<i id = 'price1' class = "rating"></i>
<i id = 'price2' class = "rating"></i>
<i id = 'price3' class = "rating"></i>
<i id = 'price4' class = "rating"></i>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/http'
require 'json'
require 'openssl'
$api_key = "YOUR_API_KEY"
def getAllLocations(list, next_page_token=nil, times=0)
# Latitude, Longitude and radius for location
lat = "42.361868"
lon = "-71.060610"
radius = "1000"
uri = URI.parse("https://maps.googleapis.com/maps/api/place/nearbysearch/json")
params = {:radius => radius, :types => "food", :sensor => "false", :key => $api_key}
if(next_page_token)
params[:pagetoken] = next_page_token
else
params[:location] = "#{lat},#{lon}"
end
uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code != "200"
puts "google places website communication (status-code: #{response.code})\n#{response.body}"
else
data = JSON.parse(response.body)
result = list + data['results']
#Google Places API only let you fetch 2 more pages from the initial request
if data['next_page_token'] && times < 2
times += 1
return getAllLocations(result, next_page_token, times)
else
return result
end
end
end
locationsList = getAllLocations []
SCHEDULER.every '5m', :first_in => '10s' do |job|
rand_index = rand(locationsList.size)
if(locationsList[rand_index]["photos"])
photo_reference =locationsList[rand_index]["photos"][0]["photo_reference"]
img_url = "https://maps.googleapis.com/maps/api/place/photo?maxheight=400&photoreference=#{photo_reference}&sensor=false&key=#{$api_key}"
else
img_url = locationsList[rand_index]["icon"]
end
open_message = "Status: Unknown"
if(locationsList[rand_index]["opening_hours"] && locationsList[rand_index]["opening_hours"]["open_now"])
open_message = "Status: Closed"
if(locationsList[rand_index]["opening_hours"]["open_now"])
open_message = "Status: Open"
end
end
send_event('places', {:name => locationsList[rand_index]['name'],
:vicinity => locationsList[rand_index]['vicinity'],
:image => img_url,
:open => open_message,
:price => locationsList[rand_index]['price_level'],
:rating => locationsList[rand_index]['rating']})
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #47bbb3;
$title-color: rgba(255, 255, 255, 0.7);;
// ----------------------------------------------------------------------------
// Widget-weather styles
// ----------------------------------------------------------------------------
.widget-places {
background-color: $background-color;
.title {
color: $title-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment