Skip to content

Instantly share code, notes, and snippets.

# WEB
# e.g. webcheck /about google.com google.co.uk
# will check each host in parallel, multiple times
function webcheck {
trap handle_sigint SIGINT
set +m
path=$1
shift
hosts=$*
for host in $hosts ; do
@mahemoff
mahemoff / console.rb
Created August 17, 2016 10:00
Debugging JSAssets on IRB
# Run this on console to check asset path will work
JsAssets::List.exclude = ['boot.js'] ; JsAssets::List.allow = %w(app.js models/*.js prod-*.css); `rm -fr tmp/cache`; JsAssets::List.fetch
@mahemoff
mahemoff / assign.coffee
Last active August 16, 2016 18:46
CoffeeScript Object.assign polyfill
# adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
# with help from JS2Coffee
# https://codepen.io/mahemoff/pen/xOBgdk?editors=0010
if typeof Object.assign != 'function'
Object.assign = (target) ->
index = 1
while index < arguments.length
@mahemoff
mahemoff / redis-delete-keys.rb
Created July 18, 2016 10:21
Monkey-patch redis namespace to delete all keys
class Redis::Namespace
def delete_all_keys
keys = self.keys '*'
self.del keys if keys.present?
end
end
funky_redis = Redis::Namespace.new('funky', redis: Redis.new(url: '...'))
funky_redis.set 'count', 10 # makes funky:10 entry
funky_redis.delete_all_keys # deletes funky:10 entry
@mahemoff
mahemoff / safe_strip.rb
Last active June 28, 2016 09:04
unicode-friendly ruby whitespace removal
class String
# performance hack - memoize these regexps
SPACE_8_RE = Regexp.new('[[:space:]]'.encode('UTF-8'))
SPACE_16_RE = Regexp.new('[[:space:]]'.encode('UTF-16LE'))
def safe_strip
if self.encoding=='UTF-8'
self.gsub SPACE_8_RE, ''
elsif self.encoding=='UTF-16'
@mahemoff
mahemoff / README.md
Last active June 14, 2016 11:48
Android Play control freak script to show %ge
  1. Install Chrome freak extension (https://chrome.google.com/webstore/detail/control-freak/jgnchehlaggacipokckdlbdemfeohdhc?hl=en)
  2. Visit Play store developer console and open an app listing
  3. Open control freak icon beside URL bar (look for the gears/setting icon on top-right of browser)
  4. In control freak, click on "This domain (play.google.com)
  5. In control freak, copy the CSS, JS, and Libs from above here
  6. Hit save and exit control freak
  7. Reload the page! App listing should now include %ge.
@mahemoff
mahemoff / timeago.js
Created June 3, 2016 14:31
jQuery timeago supporting multiple locales and weeks instead of months
/**
* Timeago is a jQuery plugin that makes it easy to support automatically
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
*
* @name timeago
* @version 1.4.1
* @requires jQuery v1.2.3+
* @author Ryan McGeary
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
@mahemoff
mahemoff / time.en_abbrev.yml
Last active September 13, 2018 13:25
Abbreviated/short time ago
def short_time_ago(time, options={})
timestamp = time.iso8601
dist = DateTime.now.to_i - time.to_i
# Quick (non-i18n friendly hack to always show weeks instead of months (since "m" is ambiguous)
message =
if dist.to_i < 6.5.days || dist.to_i > 364.5.days
time_ago_in_words time, locale: :en_abbrev
else
"#{(dist/1.week).round}w"
end
#!/bin/bash
set -e
##############################
# CORE COMMANDS
##############################
function ap { ansible-playbook -i $inventory $* ; }
function flush_cache { ansible-playbook --flush-cache ; }
@mahemoff
mahemoff / codeblog.md
Created April 3, 2016 00:02
Fixing tests: Quick notes #codeblog

Had to spend some hours fixing various tests. What could go wrong? Quite a few things I'll capture below:

  • CircleCI login problem. To debug these, it would be useful to ssh to the build box, but it hasn't been possible for some weeks. Today I realised it's because the Github project owner is a different account, and since Github doesn't support the same ssh ID for different accounts, I made a custom ssh key, added it to Github, and could then ssh into the Circle box.
  • There was a call using Mocha's .any_instance.stubs which was never restored. Some refactoring brought this bug to the open as tests maybe ran in a different order. There doesn't appear to be any way to globally reset all mocks, so I just added .any_instance.unstub to undo this in the same test.
  • Another refactor inadvertently removed Sidekiq's "fake" framework, which is now in the superclass setup, so it will always be called. Sidekiq::Worker.clear_all and Sidekiq::Testing.fake! called every time.
  • Added error logging to a "Headles