Skip to content

Instantly share code, notes, and snippets.

@stephenyeargin
Last active February 15, 2018 21:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenyeargin/7678371 to your computer and use it in GitHub Desktop.
Save stephenyeargin/7678371 to your computer and use it in GitHub Desktop.
Add B-cycle widgets to your Dashing dashboard.

B-cycle Widget

Screenshot

B-cycle is a bike sharing service that operates in several American cities. More information at their website. This widget allows you to use the station ID in a widget to get the current count of bikes and docks available to show on your Dashing dashboard.

Demo

A demo repository is available on GitHub and here is a live Dashboard on Heroku.

Installation

At the top of jobs/bcycle.rb, you will see the two required enviornment variables (or simply modify that file to suit your needs).

  • BCYCLE_CITY - lowercase name of city.

Copy these files into their appropriate spot or use dashing install 7678371 in the project directory to have Dashing do it for you.

After the job is up and running, you simply add the following syntax to your {dashboard}.erb file like any other widget.

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

Note that you will want to change the bcycle_nashville_2315 to bcycle_{your_city}_{your_station_id}. You can have as many stations as you want on a dashboard. All are sent across as events in a configured city.

The bike.svg needs to be moved into your assets/images/ folder manually.

class Dashing.Bcycle extends Dashing.Widget
@accessor 'backgroundClass', ->
if @get('status') == "Active"
"bcycle-status-active"
else if @get('status') == "Unavailable"
"bcycle-status-unavailable"
else
"bcycle-status-unknown"
constructor: ->
super
ready: ->
$(@node).addClass(@get('backgroundClass'))
onData: (data) ->
$(@node).addClass(@get('backgroundClass'))
<h1 class="title" data-bind="name"></h1>
<h3>
<span data-bind="bikes_available">?</span> bikes,
<span data-bind="docks_available">?</span> docks
</h3>
<p class="more-info">Status: <span data-bind="status">?</span></p>
<p class="more-info" data-bind="moreinfo | raw"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/http'
require 'uri'
require 'json'
# Configure the program city
BCYCLE_CITY = (ENV['BCYCLE_CITY'] || 'nashville').downcase
##
# Get BCycle Data
def get_bcycle_data(city, type)
uri = URI("https://gbfs.bcycle.com/bcycle_#{city}/#{type}.json")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.request(Net::HTTP::Get.new(uri.request_uri))
JSON.parse(response.body)
rescue StandardError => e
puts response.body
puts e.inspect
puts "\e[33mUnable to retrieve BCycle data.\e[0m"
end
SCHEDULER.every '5m', { first_in: 0 } do |job|
# Stations
stations = get_bcycle_data(BCYCLE_CITY, 'station_information')
raise stations['error'] if stations.key?('error')
# Statuses
station_statuses = get_bcycle_data(BCYCLE_CITY, 'station_status')
raise station_statuses['error'] if station_statuses.key?('error')
# Build Status Dictionary
statuses = {}
station_statuses['data']['stations'].each do |status|
statuses[status['station_id']] = status
end
# Send Data
stations['data']['stations'].each do |station|
data = {
id: station['station_id'],
name: station['name'],
bikes_available: statuses[station['station_id']]['num_bikes_available'],
docks_available: statuses[station['station_id']]['num_docks_available'],
status: statuses[station['station_id']]['is_renting'] == 1 ? 'Active' : 'Unavailable'
}
send_event(station['station_id'], data)
end
end
.widget-bcycle {
background-image: url("/assets/bike.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: 400px 400px;
background-color: #000000;
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
.bcycle-status-active {
background-color: #242876;
}
.bcycle-status-unavailable {
background-color: #999999;
}
.bcycle-status-unknown {
background-color: #999999;
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dmetzman
Copy link

when i run this, i get "cannot find end of central file directory" Any ideas?

@stephenyeargin
Copy link
Author

@dmetzman Sorry for the delay, must have missed the notification. I haven't come across that one before. Can you describe which context you see it in? A google search indicates it may be related to decompressing a ZIP file, which shouldn't be a factor with this plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment