Skip to content

Instantly share code, notes, and snippets.

View matiaskorhonen's full-sized avatar

Matias Korhonen matiaskorhonen

View GitHub Profile
@matiaskorhonen
matiaskorhonen / __paths.txt.erb
Last active March 2, 2019 09:11
Read paths from a file and process them with https://github.com/addyosmani/critical/
<% sitemap.resources.select {|r| r.path =~ /\.html\z/i }.each do |resource| %>
<%= resource.destination_path %>
<% end %>
--- array.c~ Thu Dec 21 14:39:19 1995
+++ array.c Thu Dec 21 14:36:06 1995
@@ -283,8 +283,8 @@
end = len + end;
if (end < 0) end = 0;
}
- if (len < end) end = len;
- if (beg < end) {
+ if (end > len) end = len;
+ if (beg > end) {
@matiaskorhonen
matiaskorhonen / recursive-hashes
Last active November 11, 2020 16:39
Generate SHA256 recursively in a directory tree. Uses the same checksum file format as https://linux.die.net/man/1/sha256sum
#!/usr/bin/env ruby
require "digest"
require "find"
require "optparse"
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: recursive-hashes [options]"
@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)