Skip to content

Instantly share code, notes, and snippets.

@emilong
emilong / ruby_heredoc.md
Last active August 15, 2019 21:40
Heredocs in Ruby

Heredocs in Ruby!

Heredocs are a way to write multi-line strings in Ruby. Some of their syntax can be a bit tricky to figure out, especially when calling methods on them as literals or passing them as arguments to methods, but both can be done!

Original, verbatim Heredoc with whitespace preserved

<<-EOF
This
  reminds
for file in $(ag -l -Grb,js '(XXX|TODO)' $(git ls-files) 2>/dev/null); do
git blame -fn "$file" | egrep "(XXX|TODO)" | grep "$(git config user.name)"
don
@emilong
emilong / postgres_queries_and_commands.sql
Last active June 15, 2018 16:46 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- index usage stats
SELECT
relname,
100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
n_live_tup rows_in_table
FROM
pg_stat_user_tables
WHERE
seq_scan + idx_scan > 0
ORDER BY
@emilong
emilong / fingerprint.sh
Created December 1, 2017 19:28
base64 key fingerprint
awk '{print $2}' /etc/ssh/ssh_host_ecdsa_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64
@emilong
emilong / twilio-function-to-slack.js
Created September 4, 2017 01:18
Twilio Function to relay SMS to Slack
const https = require("https");
// Make sure to declare SLACK_WEBHOOK_PATH in your Environment
// variables at
// https://www.twilio.com/console/runtime/functions/configure
exports.handler = (context, event, callback) => {
// Extract the bits of the message we want
const { To, From, Body } = event;
@emilong
emilong / brand-store-init.js
Created May 29, 2017 05:36
Initializing a MobX store from roast.io preloaded state
import BrandStore from "./brand-store";
const store = new BrandStore(window.__PRELOADED_STATE__);
// the store reference won't change, so just set it once
// and forget!
window.__PRELOADED_STATE__ = store;
@emilong
emilong / brand-state.js
Created May 29, 2017 05:31
MobX store for brand data
import autobind from "autobind-decorator";
import { extendObservable, action, observable } from "mobx";
const brandUrl = brand =>
`https://makeup-api.herokuapp.com/api/v1/products.json?brand=${brand.toLowerCase()}`;
export default class BrandStore {
constructor(preloadedState) {
const { brandProducts = {} } = preloadedState || {};
extendObservable(this, {
@emilong
emilong / savePreloadedStateMiddleware.js
Last active May 29, 2017 05:17
Configuring preloaded state to save after each action on the store
import { applyMiddleware, createStore } from "redux";
import thunk from "redux-thunk";
import reducer from "./api";
function savePreloadedState({ getState }) {
return next => action => {
const returnValue = next(action);
// just point the __PRELOADED_STATE__ at the state after this action.
window.__PRELOADED_STATE__ = getState();
@emilong
emilong / api.js
Last active May 29, 2017 05:27
Redux duck for makeup app
const START_BRAND_FETCH = "makeup/api/START_BRAND_FETCH";
const STORE_BRAND_PRODUCTS = "makeup/api/STORE_BRAND_PRODUCTS";
const EMPTY_STATE = { brandsBeingFetched: [], brandProducts: {} };
// Generally you may want to set this when running createStore(), but
// this may work too, depending on your redux state tree. See
// http://redux.js.org/docs/recipes/reducers/InitializingState.html
// for details.
const INITIAL_STATE = window.__PRELOADED_STATE__ || EMPTY_STATE;
@emilong
emilong / Brand.js
Created May 29, 2017 03:07
Fetching data to component state
const brandUrl = brand =>
`https://makeup-api.herokuapp.com/api/v1/products.json?brand=${brand.toLowerCase()}`;
export default class Brand extends Component {
fetchBrand = (brand = this.props.match.params.brand) => {
fetch(brandUrl(brand))
.then(response => response.json())
.then(products => this.setState({ products }));
};