Skip to content

Instantly share code, notes, and snippets.

@HaQadosch
Forked from rauchg/README.md
Created March 24, 2016 17:21
Show Gist options
  • Save HaQadosch/b0bee6bd56d3a98534b7 to your computer and use it in GitHub Desktop.
Save HaQadosch/b0bee6bd56d3a98534b7 to your computer and use it in GitHub Desktop.
require-from-twitter

require-from-twitter

Since Twitter doesn't have an edit button, it's a suitable host for JavaScript modules.

How to use

Source tweet: https://twitter.com/rauchg/status/712799807073419264

const leftPad = await requireFromTwitter('712799807073419264');
console.log(leftPad(1, 5));      // '00001'
console.log(leftPad(1234, 5));   // '01234'
console.log(leftPad(12345, 5));  // '12345'

Running this example

# populate `twitter-config.json` with your API tokens
$ npm install
$ npm test
import { babel } from './package';
import { transform } from 'babel-core';
import { decodeHTML as decode } from 'entities';
import Twit from 'twit';
// edit twitter-config.json first
const twit = new Twit(require('./twitter-config'));
export default async function requireFromTwitter (id) {
const tweet = await twit.get(`/statuses/show/:id`, { id });
if (tweet.errors) throw new Error(`Cannot find module '${id}'`);
const { text } = tweet.data;
const exports = {};
eval(transform(decode(text), babel).code);
return exports.default;
}
{
"name": "require-from-twitter",
"version": "0.0.1",
"description": "require(), but from a tweet",
"dependencies": {
"babel-preset-stage-0": "6.5.0",
"babel-preset-es2015": "6.6.0",
"babel-runtime": "6.6.1",
"babel-plugin-transform-runtime": "6.6.0",
"babel-cli": "6.6.5",
"babel-core": "6.7.4",
"twit": "2.2.3",
"entities": "1.1.1"
},
"babel": {
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"transform-runtime"
]
},
"scripts": {
"test": "babel-node test"
}
}
import requireFromTwitter from './';
requireFromTwitter('712799807073419264')
.then((leftPad) => {
console.log(leftPad(1, 5));
console.log(leftPad(1234, 5));
console.log(leftPad(12345, 5));
}, (err) => console.error(err.stack));
{
"consumer_key": "",
"consumer_secret": "",
"access_token": "",
"access_token_secret": ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment