Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🏳️‍⚧️
Protect Trans Rights

Tim Uruski timuruski

🏳️‍⚧️
Protect Trans Rights
View GitHub Profile
@timuruski
timuruski / nested_exception.rb
Last active January 1, 2016 18:59
Nested exception mixin for Ruby
# Adapted from from: http://nestegg.rubyforge.org/
module NestedException
# Public: Initializes a new nested error, the original error defaults
# to the global error variable so that it doesn't need to be explicity
# provided when re-raising an exception.
def initialize(message = nil, cause = $!)
@cause = cause
super(message || cause && cause.message)
end
@timuruski
timuruski / filter.rb
Created December 23, 2013 23:46
Operates a bit like sed without having to rebuild a string from an array of lines.
LOG_PATTERN = /^(INFO|DEBUG)\t[-0-9]+ [:0-9]+\t/
def filter_and_format(log, level)
log.each_line.with_object("") { |line, filtered|
next unless line.start_with?(level)
filtered << line.sub(LOG_PATTERN, '')
}
end
require 'date'
module Blackboxx
class Logger
# TODO false value supresses logging
def initialize(output, options = {})
@output = output
@echo_to_stdout = options.fetch(:echo_to_stdout) { output != STDOUT }
end
/**
* {@internal Missing Short Description}}
*
* {@internal Missing Long Description}}
*
* @since 1.5.0
*
* @param unknown_type $queries
* @param unknown_type $execute
* @return unknown
@timuruski
timuruski / env.php
Created December 3, 2013 20:51
Is there any way to get this sort of thing working in PHP?
Environment::build( function () {
$api_key = 'ABC123';
$partials_path = array('app/views/');
});
// Dumps something like this:
// => array('api_key' = 'ABC123', $partials_path => array('app/views/'))
// The method should be able to digest things like $api_key into an actual API connection instance.
class UserEmitter
include Enumerable
def initialize(path)
@table = CSV.table(path, skip_blanks: true)
@enumerator = Fiber.new do
@table.each do |row|
Fiber.yield UserImporter.import(merchant, row)
end
end
@timuruski
timuruski / base64.txt
Last active December 27, 2015 17:18
This output is confusing to me.
$ ruby -rbase64 -e "puts Base64.encode64('foo:bar')"
> Zm9vOmJhcg==
$ echo "foo:bar" | base64
> Zm9vOmJhcgo=
$ curl -u foo:bar -v http://httpbin.org
> GET / HTTP/1.1
> Authorization: Basic Zm9vOmJhcg==
#! /bin/bash
# Summons a Quicklook window and then returns focus to the terminal.
#
# TODO: Support other terminal emulators
#
# Usage:
# $ ql ~/example.jpg
set -e
require 'awesome_print'
# require 'map_by_method'
if require 'hirb'
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || Pry::DEFAULT_PRINT.call(output, value)
end
Hirb.enable
end
@timuruski
timuruski / eliot.rb
Last active December 22, 2015 09:18
# Eliot, two parts of ETL.
#
# Yes, in the movie it's Eliott (with TWO Ts), but no one wants to
# type that every time.
#
#
# Handles extracting data from multiple formats, CSV, JSON, YAML, XML?
# Handles transforming records into models using attributes hash or
# assignment.
#