Skip to content

Instantly share code, notes, and snippets.

View jamesmartin's full-sized avatar
🤫
Shhh

James Martin jamesmartin

🤫
Shhh
View GitHub Profile
@jamesmartin
jamesmartin / fork_exec.rb
Created May 4, 2015 06:21
Fork/exec a sub process, wait for it to rejoin and then print the status
# Runs exec inside the fork in order to capture the output of the subprocess
pid = fork { exec('ssh 127.0.0.1') }
_, status = Process.waitpid2(pid)
puts status.success?
@jamesmartin
jamesmartin / flatten.js
Created April 13, 2015 23:59
Flatten an Array in Javascript
exports.first = function(l) {
return l[0]
};
exports.rest = function(l) {
return l.reduce(function(accumulator, item, index) {
if(index > 0) {
accumulator.push(item);
}
return accumulator;
@jamesmartin
jamesmartin / safe_struct.rb
Last active August 29, 2015 14:18
Define a struct-like object, with attributes that can be called normally, or yielded when they exist.
class SafeStruct
def self.with_attributes(params)
params.keys.map do |key|
define_method(key) do |&block|
ivar = instance_variable_get("@#{key}".to_sym)
if block && !ivar.nil?
block.call(ivar)
end
ivar
end
@jamesmartin
jamesmartin / descendants_spec.rb
Last active August 29, 2015 14:17
Recursively collect all descendants of a Person
require 'rspec'
require 'person'
describe "descendants" do
it "returns all its descendants" do
sonny = Person.new('Sonny')
pa = Person.new('Pa', sonny)
grandpa = Person.new('Grandpa', pa)
expect(Person.all_descendants_of(grandpa).map(&:name)).to eq ['Pa', 'Sonny']
@jamesmartin
jamesmartin / callable_conditions.rb
Created February 9, 2015 19:47
Select from a list of values where arbitrary conditions are met
def has_foo?
->(thing) { thing == "foo" }
end
def has_bar?
->(thing) { thing == "bar" }
end
def has_baz?
->(thing) { thing == "baz" }
@jamesmartin
jamesmartin / gist:a648b6ca27017c7d888b
Last active November 6, 2020 06:10
Timezone Problems with Timecop and Circle CI
# System time zone is set to 'Australia/Sydney'
#
Time.zone = "Europe/Madrid"
=> "Europe/Madrid"
Timecop.freeze(Time.zone.now)
=> 2015-02-06 23:36:46 +1100 # The "frozen" time. +1100 is Australia/Sydney. Oh dear.
Time.zone.now
=> Fri, 06 Feb 2015 13:36:46 CET +01:00 # The time here is for the configured Time.zone; Europe/Madrid +01:00
@jamesmartin
jamesmartin / mask_subdomains
Last active February 3, 2019 12:54
Varnish: Route requests to subdomains based on URL
backend insights {
.host = "insights.example.com"
.port = "3000";
}
backend help {
.host = "help.example.com"
.port = "80";
}
@jamesmartin
jamesmartin / application.js.coffee
Created October 16, 2014 01:50
Uneasy Bedfellows: Rails, RequireJS and the Asset Pipeline
#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require bootstrap
#
#= require require-config
#= require requirejs/require
#= require_tree .
#
define "jquery", -> window.jQuery
@jamesmartin
jamesmartin / runtime.sh
Last active December 20, 2015 16:19
Calculate the duration (in seconds) of a process based on its log file entries.
timestamp() {
date -j -f "%Y-%m-%d%H:%M:%S" $* +%s
}
extract_time_field(){
echo $* | awk '{print $1$2}' | awk -F',' '{print $1}'
}
for logfile in `ls *.log`; do
starttime=$(extract_time_field $(cat $logfile | head -n1))
@jamesmartin
jamesmartin / gist:5988769
Created July 13, 2013 00:08
Using netcat to send files.
# target listens for connection
nc -l <port> > /path/to/target/destination/file
# host sends file
nc <listener_ip> <port> < /path/to/source/file