Skip to content

Instantly share code, notes, and snippets.

View tombh's full-sized avatar

Thomas Buckley-Houston tombh

View GitHub Profile
task calculate_per_location_costs: :environment do
year = ENV['YEAR'].to_i
month = ENV['MONTH'].to_i
# Method 1: By server usage
# Get all servers that were created in the given month
servers_by_location = Server.with_deleted
.where('extract(year from created_at) = ?', year)
.where('extract(month from created_at) = ?', month)
.group_by(&:location)
@tombh
tombh / gist:b45e86788620435adc5a
Last active November 9, 2021 13:49
Use .env file to filter sensitive data from VCR cassettes
# Assuming that you're using .env to store your sensitive app credentials, then you can
# use VCR's `filter_sensitive_data` method to convert occurrences of those credentials
# to `<%= ENV['#{key}'] %>` in your recorded VCR cassettes.
require 'vcr'
# Use the .env file to compile the list of sensitive data that should not be recorded in
# cassettes
def sensitive_strings
contents = File.read "#{Rails.root}/.env"
@tombh
tombh / gist:f66de84fd3a63e670ad9
Created January 3, 2015 17:38
Convert OpenSSH public key to OpenSSL public key using ruby
require 'base64'
require 'openssl'
# Parse SSH keys to be used by OpenSSL lib
# Taken from Zerg Support project.
# See: https://github.com/pwnall/zerg_support/blob/faaa5dd140c95588a1db2a25f6c9d9cacb4f9b0a/lib/zerg_support/open_ssh.rb
module OpenSSHKeyConverter
# The components in a openssh .pub / known_host RSA public key.
RSA_COMPONENTS = ['ssh-rsa', :e, :n]
# The components in a openssh .pub / known_host DSA public key.
chocolates = ['maltesers', 'minstrels', 'green & blacks', 'montezuma']
we_ate = chocolates[0..1]
# Outputs: ['maltesers', 'minstrels']
<?php
$chocolates = ['maltesers', 'minstrels', 'green & blacks', 'montezuma'];
$we_ate = array_slice($chocolates, 0, 2);
// Outputs: ['maltesers', 'minstrels']
?>
desc 'Add, remove and list config for an app'
command :config do |c|
c.desc 'Delete config by keys'
c.command :rm do |sc|
sc.action do |global_options, options, args|
@api.request :delete, "/app/#{Git.first_sha}/config", {
keys: args.to_json
}
end
end
@tombh
tombh / gist:ffa163e8c05beb3c8317
Created June 12, 2014 10:42
Rspec exception getting caught by async method
require 'celluloid'
# It wasn't until I commented out this line that I realised what was actually going on below
# Celluloid.logger = nil
class Foo
include Celluloid
def do
bar
end
@tombh
tombh / gist:5519b4a0d17d78a9fef7
Created June 4, 2014 14:31
Docker-api rspec errors after bumping API_VERSION to 0.11
1) Docker::Container#changes returns the changes as an array
Failure/Error: ]
expected: [{"Path"=>"/dev", "Kind"=>0}, {"Path"=>"/dev/kmsg", "Kind"=>0}, {"Path"=>"/root", "Kind"=>2}]
got: [{"Kind"=>2, "Path"=>"/root"}] (using ==)
Diff:
@@ -1,4 +1,2 @@
-[{"Path"=>"/dev", "Kind"=>0},
- {"Path"=>"/dev/kmsg", "Kind"=>0},
- {"Path"=>"/root", "Kind"=>2}]
+[{"Kind"=>2, "Path"=>"/root"}]
@tombh
tombh / quiet_fetch.rb
Created June 18, 2013 16:54
When you're accessing deep keys within a hash you don't want to have to write stuff like `if foo && foo["bar"] && foo["bar"]["baz"] && foo["bar"]["baz"]["bang"]`. So here's an idea to have a method on the Hash class that uses a sort of XML type query, eg; hash.quiet_fetch('a/b/c')
class Hash
def quiet_fetch query
current = self
query.split('/').each do |key|
if current.fetch(key, false)
current = current.fetch(key)
else
return nil
end
end
# Place this file in the same directory as `Vagrantfile'
# then simply require "vagrant-snapshot.rb" at the top of Vagrantfile.
require 'optparse'
Vagrant.commands.register(:snap) { Snap::Commands }
# Provide rake-like desc() 'inflected' documentation
# See http://stackoverflow.com/questions/2948328/access-attributes-methods-comments-programmatically-in-ruby
class Module