Skip to content

Instantly share code, notes, and snippets.

View sobstel's full-sized avatar
💎

sobstel

💎
  • 16:55 (UTC +02:00)
View GitHub Profile
@sobstel
sobstel / loadable.js
Created March 4, 2018 21:18
Loader/loading Higher-Order Component
// Sample usage: (taken from https://github.com/sobstel/golazon)
//
// class Competition extends Component {
// (...)
// }
//
// const fetchData = ({ id }) => {
// return competitionService.competition(id).then(competition => ({ competition }));
// };
//
@sobstel
sobstel / fetch_with_timeout.js
Created March 4, 2018 17:47
fetch with timeout
const API_URL = '...';
const TIMEOUT = 10; // seconds
export default (path) => {
/* global fetch */
const req = fetch(API_URL + path);
const timeout = new Promise((resolve, reject) => {
return setTimeout(() => reject(new Error('request timeout')), TIMEOUT * 1000);
});
@sobstel
sobstel / README
Last active January 6, 2018 13:14
Climb the stairs (brain friendly, not so cpu friendly)
There's a staircase with N steps, and you can climb 1 or 2 steps at a time.
Given N, write a function that returns the number of unique ways you can
climb the staircase. The order of the steps matters.
For example, if N is 4, then there are 5 unique ways:
1, 1, 1, 1
2, 1, 1
1, 2, 1
@sobstel
sobstel / warden.rb
Last active January 24, 2020 18:36
Warden Rails cookie
Warden::Manager.after_authentication do |user, auth, _opts|
cookies = ActionDispatch::Request.new(auth.env).cookie_jar
cookies.encrypted[REMEMBER_ME_COOKIE_NAME] = { value: user.id, expires: 1.week.from_now }
end
@sobstel
sobstel / import_from_db.sh
Created December 4, 2017 14:28
Heroku prod database export to Docker database container
#!/bin/bash
# stop on first error
set -e
command -v heroku >/dev/null 2>&1 || { echo >&2 "heroku command is required"; exit 1; }
command -v docker-compose >/dev/null 2>&1 || { echo >&2 "docker-compose command is required"; exit 1; }
DUMPFILE="/tmp/_FILENAME_.dump"
@sobstel
sobstel / Gemfile
Last active December 2, 2017 22:12
Quick&dirty trello task list
source 'https://rubygems.org'
ruby '~> 2.3.1'
gem 'cuba'
@sobstel
sobstel / optimize-images.js
Created October 6, 2017 21:45
Optimal images optimization
//
// Optimizes all images
// (takes only those that have no corresponding @2x)
//
const im = require('imagemagick');
const glob = require('glob');
const fs = require('fs');
const { exec } = require('child_process');
@sobstel
sobstel / fetch_timeout.js
Created August 6, 2017 10:24
fetch timeout
const CONNECTION_TIMEOUT = 20000;
function getData (url) {
let timer = null;
const service = fetch(url)
.then((response) => {
clearTimeout(timer);
return response.json();
});
@sobstel
sobstel / rn-scrollview-repeat-bg.js
Created May 12, 2017 14:24
React Native ScrollView repeated background
import React, { PropTypes, PureComponent } from 'react';
import { Image, ScrollView, } from 'react-native';
export default class ScrollViewWithBg extends PureComponent {
constructor (props) {
super(props);
this.state = {contentSize: {width: 0, height: 0}}
}
@sobstel
sobstel / translatable.rb
Created April 18, 2017 14:21
Traductor (rails models translations)
module Translatable
extend ActiveSupport::Concern
included do
translation_class_name = "Translations::#{model_name}Translation"
has_many :translations,
class_name: translation_class_name,
dependent: :delete_all,
inverse_of: model_name.to_s.underscore.to_sym