Skip to content

Instantly share code, notes, and snippets.

@stephenyeargin
Last active February 16, 2019 10:09
Show Gist options
  • Save stephenyeargin/49dc387329cd7164870e to your computer and use it in GitHub Desktop.
Save stephenyeargin/49dc387329cd7164870e to your computer and use it in GitHub Desktop.
Add an Uber widget to your Dashboard

Uber Dashing Widget

This Dashing Dashboard widget allows you to see an estimate of how close the nearest Uber driver is to your location (in minutes). Useful if you're planning a fast getaway from the office.

screenshot

Before you get started

Go ahead and register an application with Uber. It is a relatively painless process if you already have an Uber account.

From that, you really only need one of the credentials, the Server Token. The other stuff is helpful if you are building a full-fledge OAuth application that can request rides, etc. This one just polls a single endpoint.

Your next step is to get the latitude and longitude of wherever this Dashboard lives. The easiest way to do this is to key your address into Google Maps and look in the address bar for the two sets of numbers. If your location is accidentally located in the middle of the ocean, try reversing them.

To recap, get this stuff:

  • The server token from your registered application
  • The latitude and longitude of your location

The job file

This is what polls the API for the estimated time to arrival (in seconds) per "product". A product, in Uber-land, refers to the kind of car that will come pick you up, with UberX being the small sedans, Uber XL the larger SUVs and Uber Black Car the more "formal" car service. Products will change based on the location you obtained above.

Place the uber.rb file in the ./jobs folder. You will need to either have the information above loaded as the referenced environment variables (recommended), or simply hard-code in those values into the Ruby code.

The template

I did not do a custom widget for this iteration, so it simply uses the List widget. You can get more creative with styling if you want to.

    <li data-row="1" data-col="1" data-sizex="2" data-sizey="2">
      <div data-id="uber_pickup_time" data-view="List" data-unordered="true" data-title="Get an Uber" data-moreinfo="Estimated time to pickup from current location."></div>
      <i class="icon-time icon-background"></i>
    </li>
require 'net/http'
require 'uri'
require 'json'
UBER_SERVER_TOKEN = ENV["UBER_SERVER_TOKEN"]
UBER_LATITUDE = ENV["UBER_LATITUDE"]
UBER_LONGITUDE = ENV["UBER_LONGITUDE"]
SCHEDULER.every '5m', :first_in => 0 do |job|
begin
uri = URI("https://api.uber.com/v1/estimates/time?start_latitude=#{UBER_LATITUDE}&start_longitude=#{UBER_LONGITUDE}")
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)
request.add_field("Authorization", "Token #{UBER_SERVER_TOKEN}")
response = http.request(request)
uber_response = JSON.parse(response.body)
pickup_times = []
uber_response['times'].map do |item|
estimate = (item['estimate']/60).ceil
pickup_times << {
label: item['localized_display_name'],
value: "#{estimate} minute(s)"
}
end
event_name = 'uber_pickup_time'
send_event(event_name, { items: pickup_times })
rescue
puts "\e[33mYou need to add your UBER_SERVER_TOKEN and define a latitude and longitude.\e[0m"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment