Skip to content

Instantly share code, notes, and snippets.

@AlSquire
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AlSquire/5b29d5c0ddbddac6e502 to your computer and use it in GitHub Desktop.
Save AlSquire/5b29d5c0ddbddac6e502 to your computer and use it in GitHub Desktop.
/**
* Welcome to Pebble.js!
*
* This is where you write your app.
*/
var UI = require('ui');
var ajax = require('ajax');
function fetchJSON(url) {
ajax(
{
url: url,
type: 'json'
}, function(data, status, request) {
displayJSON(data);
}, function(error, status, request) {
var card = UI.Card({
title: "Error",
body: status
});
card.show();
}
);
}
function displayJSON (json) {
if (json.hasOwnProperty('menu')) {
var menu = new UI.Menu(json.menu);
menu.on('select', function(e) {
if (e.item.hasOwnProperty('url')) {
fetchJSON(e.item.url);
}
});
menu.show();
} else if (json.hasOwnProperty('card')) {
var card = new UI.Card(json.card);
card.show();
}
}
var menuUrl = "https://gist.githubusercontent.com/AlSquire/5b29d5c0ddbddac6e502/raw/menu.json";
fetchJSON(menuUrl);
{
"card": {
"title": "Hey",
"body": "Tralala\nAll right!"
}
}
{
"menu": {
"sections": [{
"items": [
{
"title": "Friandises chat"
}, {
"title": "Dentifrice"
}, {
"title": "Steak"
}
]
}]
}
}
require 'sinatra'
require 'json'
require 'net/http'
get '/' do
"Hello World"
end
get '/velib.json' do
stations = {
2021 => 'OMTS',
4013 => 'Home',
4014 => 'Archives',
3008 => 'Perle',
4103 => 'BHV',
4015 => 'Pont Louis',
4012 => 'Ecouffes'
}
url = "http://opendata.paris.fr/api/records/1.0/search?dataset=stations-velib-disponibilites-en-temps-reel&q="
url += 'number%3A' + stations.keys.join('+OR+number%3A')
json = JSON.parse(Net::HTTP.get(URI(url)))
body = stations.map do |key, val|
fields = json['records'].detect { |r| r['fields']['number'] == key }['fields']
"#{val}: #{fields['available_bikes']}/#{fields['available_bike_stands']}"
end
content_type :json
{
card: {
title: "Velib",
body: body.join("\n")
}
}.to_json
end
{
"menu": {
"sections": [{
"items": [
{
"title": "Velibs",
"url": "http://alnet.herokuapp.com/velib.json"
},
{
"title": "AGDQ",
"url": "http://agdq-next.herokuapp.com/"
},
{
"title": "Card test",
"subtitle": "Yeah",
"url": "https://gist.githubusercontent.com/AlSquire/5b29d5c0ddbddac6e502/raw/card.json"
}, {
"title": "Rien",
"subtitle": "Gogogo"
}, {
"title": "Courses",
"url": "https://gist.githubusercontent.com/AlSquire/5b29d5c0ddbddac6e502/raw/errands.json"
}
]
}]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment