Skip to content

Instantly share code, notes, and snippets.

View matiaskorhonen's full-sized avatar

Matias Korhonen matiaskorhonen

View GitHub Profile
@matiaskorhonen
matiaskorhonen / authentication_token.rb
Last active June 19, 2018 06:09
Example AuthenticationToken implementation using ActiveSupport::MessageEncryptor
class AuthenticationToken
# Encode a hash
def self.encode(payload, ttl: 14.days.to_i)
keygen = ActiveSupport::KeyGenerator.new(Rails.application.secrets.token_secret)
key = keygen.generate_key(Rails.application.secrets.token_salt, 32)
crypt = ActiveSupport::MessageEncryptor.new(key)
payload[:expires] = ttl.seconds.from_now.to_i
crypt.encrypt_and_sign(JSON.dump(payload.deep_stringify_keys))
end
// matches polyfill
this.Element && function(ElementPrototype) {
ElementPrototype.matches = ElementPrototype.matches ||
ElementPrototype.matchesSelector ||
ElementPrototype.webkitMatchesSelector ||
ElementPrototype.msMatchesSelector ||
function(selector) {
var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;
while (nodes[++i] && nodes[i] != node);
return !!nodes[i];
@matiaskorhonen
matiaskorhonen / resources.md
Created February 3, 2017 09:58
Rails Security - above and beyond the defaults resources
# config/initializers/active_record_uuid_ids.rb
Rails.application.config.generators do |generator|
# Always use UUIDs for primary keys (when generating migrations)
generator.orm :active_record, primary_key_type: :uuid
end
@matiaskorhonen
matiaskorhonen / postgresapp_unix_socket.md
Last active June 15, 2021 21:37 — forked from iamvery/postgresapp_unix_socket.md
Setup Postgres.app to allow connections via unix sockets

These are instructions to setup Postgres.app to allow connections over unix sockets. These instructions were written for macOS Sierra (10.12) and Postgres.app 9.6.0

  1. Run Postgres.app once so that the configuration is initialized in ~/Library/Application Support/Postgres/var-9.6/
  2. Quit Postgres.app
  3. Open ~/Library/Application Support/Postgres/var-9.6/postgresql.conf in your favorite text editor
  4. Uncomment the line unix_socket_directories = '/tmp' and change it to unix_socket_directories = '/var/pgsql_socket,/tmp'
  5. Run sudo mkdir -p /var/pgsql_socket
  6. Run sudo chmod 770 /var/pgsql_socket
  7. Run sudo chown root:staff /var/pgsql_socket
// Underscore.js
// http://underscorejs.org
// (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
//
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
@matiaskorhonen
matiaskorhonen / tumblr-to-middleman.rb
Created September 16, 2016 11:03
Tumblr blog post extraction code. Pretty much tailored for my needs when I migrated from Tumblr to Middleman, but this might still be of use to someone…
#!/usr/bin/env ruby
require "tumblr_client"
require "time"
require "yaml"
require "fileutils"
require "open-uri"
require "nokogiri"
require "loofah"
require "reverse_markdown"
@matiaskorhonen
matiaskorhonen / navbar_tab_builder.rb
Created August 11, 2016 07:16
Bootstrap v4 navbar tabs_on_rails tab builder
class NavbarTabBuilder < TabsOnRails::Tabs::Builder
def open_tabs(options = {})
options[:class] ||= "nav navbar-nav"
@context.tag("ul", options, open = true)
end
def close_tabs(options = {})
"</ul>".html_safe
end
@matiaskorhonen
matiaskorhonen / Chromium48.sh
Created April 20, 2016 11:04
Install Chromium 48 on Raspbian Jessie
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+build/8883797/+files/chromium-browser_48.0.2564.82-0ubuntu0.15.04.1.1193_armhf.deb && \
wget https://launchpad.net/~canonical-chromium-builds/+archive/ubuntu/stage/+build/8883797/+files/chromium-codecs-ffmpeg-extra_48.0.2564.82-0ubuntu0.15.04.1.1193_armhf.deb && \
sudo dpkg -i chromium-codecs-ffmpeg-extra_48.0.2564.82-0ubuntu0.15.04.1.1193_armhf.deb chromium-browser_48.0.2564.82-0ubuntu0.15.04.1.1193_armhf.deb
@matiaskorhonen
matiaskorhonen / sign-pdf.rb
Last active March 21, 2025 02:34
Quick and dirty PDF signing in Ruby (using Origami)
#!/usr/bin/env ruby
require "openssl"
require "time"
begin
require "origami"
rescue LoadError
abort "origami not installed: gem install origami"
end