Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / session_file_store.rb
Created April 24, 2016 00:31
Rails session file store
class SessionFileStore < ActionDispatch::Session::AbstractStore
def get_session(env, sid)
sid ||= generate_sid
session = Marshal.load(IO.binread(tmp_file(sid))) rescue nil
[sid, session.presence || {}]
end
def set_session(env, sid, session, options)
Dir.mkdir(sessions_path) unless Dir.exist? sessions_path
IO.binwrite(tmp_file(sid), Marshal.dump(session))
@reu
reu / cpf.js
Created May 9, 2016 18:14
CPF validator JS
"use strict";
const blackList = [
"00000000000",
"11111111111",
"22222222222",
"33333333333",
"44444444444",
"55555555555",
"66666666666",
@reu
reu / farfetch-graphql.js
Last active March 3, 2017 16:19
Farfetch GraphQL fragments plugin
const graphQLFragments = (fragments = {}) => req => {
const uniq = list => list.filter(value => list.indexOf(value) == list.lastIndexOf(value));
const hasFragment = graphQL => graphQL.match(/\.{3}\s*([A-Za-z0-9\_]+)/g) != null;
const findFragments = graphQL =>
graphQL
.match(/\.{3}\s*([A-Za-z0-9\_]+)/g)
.map(fragment => fragment.trim().replace(/^\.{3}/, ""))
.filter(fragment => fragment.match(/\s+on\s+/) == null)
.map(fragment => {
if (fragment in fragments) {
@reu
reu / mkdirp.js
Last active April 14, 2017 02:58
const fs = require("fs");
const path = require("path");
const promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, res) => err === null ? resolve(res) : reject(err))
);
const mkdir = promisify(fs.mkdir);
const stat = promisify(fs.stat);
import React, {
Component,
PropTypes,
} from "react";
import {
Animated,
View,
} from "react-native";
const { curry, splitEvery } = require("ramda");
const batchMap = curry((batchSize, fn, list) =>
splitEvery(batchSize, list)
.map(batch => all => Promise.all(batch.map(fn)).then(res => all.concat(res)))
.reduce((results, batch) => results.then(batch), Promise.resolve([]))
);
module.exports = batchMap;
// @flow
import {
always,
memoize,
pick,
} from "ramda";
type ParsedURL = {
protocol: string,
const http = require("http");
const path = require("path");
const target = process.env.TARGET; // ex: google.com
const port = process.env.PORT || 80;
http
.createServer(({ url, headers }, res) => {
const protocol = headers["x-forwarded-proto"] || "http";
res.writeHead(308, { Location: `${protocol}://${path.join(target, url)}` });
@reu
reu / example.js
Last active January 4, 2023 00:34
HTML builder DSL
import { h1, div, p } from "./html";
div([
h1({ class: "title" }, "Title"),
p("A nice text"),
]);
@reu
reu / proxy.js
Created March 22, 2019 15:01
Simple HTTP proxy
const http = require("http");
const { PORT, PROXY_HOST, PROXY_PORT } = process.env;
http
.createServer((req, res) => {
const proxyRequest = http
.request({
hostname: PROXY_HOST,
port: PROXY_PORT || 80,