Skip to content

Instantly share code, notes, and snippets.

View kjlape's full-sized avatar
🤝
nice to meet you

Kaleb Lape kjlape

🤝
nice to meet you
View GitHub Profile
select pid, application_name from pg_stat_activity;
select pg_terminate_backend(<PID int>);
RSpec::Matchers.define :have_field_value do |selector, value:|
match do |page|
page.find(selector).value == value
end
failure_message do
<<-MESSAGE
expected #{selector} to have value #{value}, but it was not.
MESSAGE
end
class WithTranslation
def initialize(delegate, translator)
@delegate = delegate
@translator = translator
end
def method_missing(method_name, *rest)
translation = @translator.translate_attribute(@delegate, method_name)
return translation if translation.present?
module I18nHelper
def i18n
ChainableTranslation.new
end
class ChainableTranslation
attr_reader :options
def initialize(scopes = [], options = {})
@scopes = Array(scopes)
@kjlape
kjlape / Dockerfile
Created May 27, 2017 21:44
Docker container to validate a terraform config with multiple/nested modules
FROM hashicorp/terraform:light
MAINTANER "Kaleb Lape <[email protected]>"
RUN apk add --update bash
ENTRYPOINT bash -c 'find . -type f -name "*.tf" -print0 | xargs -0 -n 1 dirname | sort | uniq | xargs -t -n 1 terraform validate'
@kjlape
kjlape / convert-to-html5.sh
Last active May 16, 2017 16:14
HTML5 FFMPEG Video Conversion
#!/bin/sh
# Modified from original implementation:
# https://gist.github.com/liamcurry/2696512
# Requires ffmpeg with codec plugins
# On Mac:
# brew install ffmpeg --with-libvpx --with-theora --with-libogg --with-libvorbis --with-webp --with-rtmpdump --with-openh264 --with-fdk-aac
# Output file for HTML5 video
@kjlape
kjlape / state_handler.rb
Created February 5, 2017 05:01
State handler...
module StateHandlers
class Base
class_attribute :registered_states
class_attribute :registered_events
extended do
registered_states = {}
registered_events = Hash.new { |hash, key| hash[key] = { before: [], after: [] } }
end
@kjlape
kjlape / example_spec.rb
Created October 5, 2016 18:56
How to use fancy rspec matchers for ActiveJob args.
require 'spec_helper.rb'
describe JobEnqueuer do
it 'enqueues the jobs' do
# Use matchers like 'a_value' in your enqueued assertions.
assert_enqueued_with(job: UsefulJob, args: [resource, data, { option: a_value }]) do
subject
end
end
end
@kjlape
kjlape / csv.rb
Created September 22, 2016 19:24
Rails CSV One-liner
([headers = %i(some headers here)] + SomeModel.where(condition: :state).pluck(*headers)).each { |row| puts row.join ', ' }; nil
@kjlape
kjlape / ephemeral_port.rb
Last active August 30, 2016 15:19
Ruby hack to get an ephemeral localhost port from the system.
def ephemeral_port
unless @ephemeral_port
tcp_server = TCPServer.new('localhost', 0)
@ephemeral_port ||= tcp_server.addr[1]
tcp_server.close
end
@ephemeral_port
end