Skip to content

Instantly share code, notes, and snippets.

@th3james
th3james / Rust-DI-with-dyn.rs
Last active February 3, 2023 13:55
Two Rust Dependency Injection patterns: one utilising compile-time generics, the other the runtime `dyn` keyword.
// An attempt to have a struct with dependency which is a trait
// with a default constructor with provides the production dependency
// This approach uses `dyn` keyword which means the traits are checked at runtime
//
// Adapted from https://doc.rust-lang.org/book/ch15-05-interior-mutability.html
pub trait Messenger {
fn send(&self, msg: &str);
}
pub struct ConcreteMessenger;
#!/usr/bin/env ruby
# Executes a given command on a given ecs cluster
# Usage:
# ./ecs_cluster_exec.rb cluster-name docker ps
def command_pipe(*commands)
commands.join(' | ')
end
def get_cluster_host_arns(cluster_name)

Keybase proof

I hereby claim:

  • I am th3james on github.
  • I am th3james (https://keybase.io/th3james) on keybase.
  • I have a public key ASBqlzaHAgmzGYcRHYURnr5RbHSvFOaGa3uS1QJl0R9r-wo

To claim this, I am signing this object:

@th3james
th3james / find_aws_user.rb
Created January 25, 2017 11:57
Find AWS user by access key
require 'json'
key_to_find = /.*thing-or-whatever.*/
users = JSON.parse(`aws iam list-users`).fetch('Users')
usernames = users.map { |user|
user.fetch('UserName')
}
user_secret_keys = Hash[
tell application "iTerm"
activate
set term to (make new terminal)
tell term
set serverSession to (launch session "Default Session")
tell serverSession
set name to "server"
@th3james
th3james / shatter-liz-phair.tab
Created July 12, 2015 15:10
Shatter by Liz Phair guitar tab
# Shatter - Liz Phair
Open D tuning with a capo on first fret.
Tab relative to capo
## Intro p1
d#|----------------------------------------|
A#|----------------------------------------|
@th3james
th3james / gist:8829607
Last active August 29, 2015 13:56
Sinon fake server example
test(".save persists the model to the server and adds the ID to the attributes", function() {
var page, persistenceId, server;
// Create a fake server to block AJAX requests
server = sinon.fakeServer.create();
// Upon receiving a request to '/pages', respond with an ID
persistenceId = 6;
server.respondWith(new RegExp("/pages"), JSON.stringify({
id: persistenceId
@th3james
th3james / gist:8829034
Created February 5, 2014 17:30
Mocha Stubbed save
suite("Page");
test(".addSection adds a section and saves the page", function() {
var page, pageSaveStub;
page = new Page({
sections: []
});
// page.save would call the server, stub it with a successful query
pageSaveStub = sinon.stub(page, 'save', function(attributes, options) {
@th3james
th3james / gist:8827198
Last active August 29, 2015 13:56
View rendering test
suite('ReportView');
test("Renders a given report's title", function() {
var report, view;
report = {
title: "My Lovely Report"
};
view = new ReportView(report);
view.render();
@th3james
th3james / gist:8827009
Last active August 29, 2015 13:56
DOM event listener test
suite("PickUserView");
test('when clicking the close button, the view gets closed', function() {
var view, viewCloseSpy;
// The function we expect to be called on close
viewCloseSpy = sinon.spy(ChooseUserView.prototype, 'close');
view = new ChooseUserView();
view.render();