Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / FetchConnector.js
Last active February 16, 2018 13:08
Fetch connector for elasticsearch.js
import 'abortcontroller-polyfill';
import ConnectionAbstract from 'elasticsearch/src/lib/connection';
/**
* @example
* const client = new elasticsearch.Client({
* connectionClass: FetchConnector
* });
*/
class FetchConnector extends ConnectionAbstract {
@tobiashm
tobiashm / readKey.js
Last active June 10, 2023 11:38
Read a singe key value from IndexedDB
// Assuming https://github.com/jakearchibald/idb-keyval has been used for key-value entries,
// the following is about the minimum code needed to read the value for a single key (w/o the lib.)
// Usage: `readKey('name').then(function(name) { console.log(name); });`
function readKey(key) {
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
if (!indexedDB) return Promise.reject(new Error('IndexedDB not available'));
return new Promise(function(resolve, reject) {
var open = indexedDB.open('keyval-store', 1);
@tobiashm
tobiashm / README.md
Last active August 30, 2024 11:58
Setting up local domain for Docker

Example for setting up <whatever>.docker as a local domain (for Docker services)

What to do:

  • Install dnsmasq for resolving hostnames
  • Configure dnsmasq to resolve .docker requests to localhost
  • Configure macOS to send .docker requests to dnsmasq
brew install dnsmasq
# Create config files
@tobiashm
tobiashm / README.md
Created August 30, 2017 08:02
Use PhantomJS to render PDF in Rails

Use PhantomJS to render PDF in Rails

Avoid disk IO

If you're deploying your app to somewhere where you can't be sure to have reliable disk read/write access, the normal strategy of writing to a temp-file doesn't work. Instead we can open a pipe to the Phantom.js process, and then pass in the HTML via stdin, and then have the rasterize.js script write out the resulting PDF to stdout, which we can then capture. Any log messages from the Phantom.js process can be passed via stderr if we want.

Session heist

@tobiashm
tobiashm / after_cd_nvm
Created August 18, 2017 09:35
Change nvm version using RVM hooks (place in ~/.rvm/hooks/after_cd_nvm)
#!/usr/bin/env bash
# Requirements:
# brew install jq node
# nvm use system; npm -g install semver
if [ -f package.json ]; then
requested_node_version=$(jq -r '.engines.node' package.json)
if [ "$requested_node_version" != "" ]; then
node_version=$(nvm_ls | xargs env NODE_VERSION=system ~/.nvm/nvm-exec semver -r "$requested_node_version")
@tobiashm
tobiashm / dabblet.css
Last active January 18, 2017 07:43
Untitled
@keyframes radar {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
#wifi {
width: 100px; height: 100px;
border: double 100px;
border-radius: 200px;
border-color: black transparent transparent;
animation: radar 3s linear infinite;
@tobiashm
tobiashm / docker-userns.md
Last active January 5, 2017 15:21
Docker user namespace on RHEL7

Setup User Namespace on RHEL7 for use with Docker

Install Docker

Enable User Namespace on RHEL7:

# Notice: Use whatever is the lates version of `/boot/vmlinuz-xxx.el7.x86_64`
sudo grubby --args="user_namespace.enable=1" --update-kernel=/boot/vmlinuz-3.10.0-327.13.1.el7.x86_64
sudo reboot now
@tobiashm
tobiashm / test_respond_to_missing.rb
Last active January 4, 2017 20:40
Ruby special behaviour: `respond_to_missing?` method gets hidden
# When you implement `method_missing` you should also implement `respond_to_missing?`
# But the later doesn't show up as instance methods on the class, and can not be called directly! Or does it???
class Foo
def method_missing(name, *)
"called #{name}"
end
def bar?
respond_to_missing?(:bar)
@tobiashm
tobiashm / README.md
Last active December 19, 2016 09:27
Karma on Rails

Use Karma.js with Ruby on Rails (Sprockets)

Inspired by http://monterail.com/blog/2014/karma-on-rails/

This basically allows you to reference your application JS files, which are generated by Sprockets, in the Karma config like this:

files: [
  '<%= require("application.js") %>',
  '<%= require("other.js") %>',

'static/files/**/*.js'

@tobiashm
tobiashm / boolean.rb
Created October 24, 2016 20:21
Boolean conversion method in Ruby
module Kernel
def Boolean(input)
case input
when true, /^(true|y|yes|1)$/i, 1
true
when false, /^(false|n|no|0)$/i, 0, "", nil
false
else
fail TypeError, "can't convert #{input.inspect} into boolean"
end