Skip to content

Instantly share code, notes, and snippets.

View matiaskorhonen's full-sized avatar

Matias Korhonen matiaskorhonen

View GitHub Profile
@matiaskorhonen
matiaskorhonen / maintenance.html
Created January 16, 2019 14:04
Basic maintenance page (based on Heroku's)
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport"> <title>Offline for maintenance</title> <style>html, body {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
background-color: #F7F8FB;
height: 100%;
-webkit-font-smoothing: antialiased; }
body {
@matiaskorhonen
matiaskorhonen / rflink.csv
Created July 14, 2018 08:04
Sample data from RFLink
@matiaskorhonen
matiaskorhonen / signed_to_float.rb
Last active July 14, 2018 09:04
Convert signed hexadecimals from RFLink to floats
# Convert signed hexadecimals from RFLink to floats
def signed_to_float(hex)
int = hex.to_i(16)
if int & 0x8000 > 0
-(int & 0x7FFF).to_f / 10
else
int.to_f / 10
end
end
@matiaskorhonen
matiaskorhonen / scheduler.sh
Created February 13, 2018 11:29
How to use local time for a task with the Heroku Scheduler
# Set the Scheduler frequency to Hourly
if [[ $(TZ=Europe/Helsinki date +%H) = 07 ]] ; then echo "Do the thing!" ; else echo "Not 7am" ; fi
@matiaskorhonen
matiaskorhonen / dns.rb
Created February 7, 2018 14:47
Test script to help diagnose erroneous NXDOMAIN responses from Ubiquiti networking gear
begin
require "dnsruby"
rescue LoadError
puts "gem install dnsruby"
end
require "yaml/store"
include Dnsruby
trap("SIGINT") { exit 1 }
@matiaskorhonen
matiaskorhonen / Caddyfile
Created February 7, 2018 14:26
Redirect all non-IP requests to with a www-prefix using Caddy
:8080 {
# Redirect only when the requested host *isn't* an IP address
redir 301 {
if {hostonly} not_match ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
if {hostonly} not_match ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$
if_op and
http://www.{hostonly}{uri}
}
}
@matiaskorhonen
matiaskorhonen / rename_appicons.rb
Created January 6, 2018 12:42
Small script to rename iOS app icons
require "fileutils"
require "json"
json = File.open("Contents.json", "r+")
contents = JSON.load(json.read)
contents["images"].map do |image|
ext = File.extname(image["filename"])
new_filename = "#{image["size"]}@#{image["scale"]}#{ext}"
FileUtils.mv(image["filename"], new_filename)
@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