tonic's notebooks

  • Color Scheme - /tonic/random-color-scheme
    Last edited 7 years ago
    var http = require('request-promise'); var _ = require('lodash'); var URL = "http://www.colr.org/json/scheme/random"; async function randomColorScheme() { var response = await http(URL); var scheme = JSON.parse(response)['schemes'][0]; try { var colors = _.map(scheme['colors'], function(c) {return "#" + c}); var tags = _.map(scheme['tags'], function(t) {return t['name']}); return {colors: colors, tags: tags}; } catch (e) { return {}; } }; module.exports = randomColorScheme;
  • Haight Ashbury - /tonic/haight-ashbury
    Last edited 7 years ago
    var GeoJSON = await request(haightAshburyURL()); JSON.parse(GeoJSON);
  • yelp demo - /tonic/yelp-demo
    Last edited 7 years ago
    var yelp = require("yelp").createClient({ consumer_key: process.env.YELP_CONSUMER_KEY, consumer_secret: process.env.YELP_CONSUMER_SECRET, token: process.env.YELP_TOKEN, token_secret: process.env.YELP_TOKEN_SECRET }); require("bluebird").Promise.promisifyAll(yelp); module.exports = yelp.searchAsync.bind(yelp);
  • Yandex Translation API - /tonic/yandex-translation-api
    Last edited 7 years ago
    var request = require("request-promise"); var apiKey = process.env.YANDEX_API_KEY; async function yandex(method, opts) { var url = "https://translate.yandex.net/api/v1.5/tr.json/" + method + "?"; url += Object.keys(opts).map(function(key){ return key+"="+encodeURIComponent(opts[key]) }).concat("key="+apiKey).join("&") return request.get(url); } module.exports.languages = async function languages() { return JSON.parse(await yandex("getLangs", { ui: "en" })).langs; } module.exports.translate = async function translate(from, to, text) { var response = await yandex("translate", {text: text, lang: from+"-"+to}) return JSON.parse(response).text[0] }
  • Untitled - /tonic/geojson-await
    Last edited 7 years ago
    JSON.parse(await require("request-promise")("https://storage.googleapis.com/maps-devrel/google.json"))
  • async/await - /tonic/await
    Last edited 7 years ago
    A common way to get around nesting callbacks everywhere in JavaScript is to use promises. But these require chaining a bunch of "then" functions together to do anything, which is still cumbersome. Putting "await" before any promise let's you write linear looking code, which works great with Tonic and our object viewers that show you the value of the last expression:
  • Yelp Search - /tonic/yelp-search
    Last edited 7 years ago
    var yelp = require("yelp").createClient({ consumer_key: "KB0aK2nqy4zDQ8ns4kPyyg", consumer_secret: "2TL5pmvWNEAe4HSd8qD4zcVAbUE", token: "lkHsYupnCX_R7UXmot3_is7kCFHjZlG_", token_secret: "1xUkaFbpPo2STwdgOC4K7Ftc6N4" }); require("bluebird").Promise.promisifyAll(yelp);
  • Untitled - /tonic/geocode
    Last edited 7 years ago
    var Promise = require('bluebird'); var request = Promise.promisifyAll(require('request')); var uri = 'http://maps.googleapis.com/maps/api/geocode/json'; 0;
  • Random - /tonic/random
    Last edited 7 years ago
    module.exports = function random() { if (arguments.length === 0) return Math.random(); if (arguments.length === 1) if (arguments[0] < 0) return range(arguments[0], 0); else return range(0, arguments[0]); return range(arguments[0], arguments[1]); } function range(aMinimum, aMaximum) { return Math.floor(Math.random() * (aMaximum - aMinimum)) + aMinimum; }
  • Untitled - /tonic/five
    Last edited 7 years ago
    module.exports.five = function(){ return 5; }