Skip to content

Instantly share code, notes, and snippets.

View thbar's full-sized avatar

Thibaut Barrère thbar

View GitHub Profile
@thbar
thbar / iban_test.rb
Last active November 23, 2017 21:09
Calling a Rust crate from Ruby minitest
require_relative 'test_helper'
require 'helix_runtime'
require 'etl-test/native'
class MyTest < Minitest::Test
def test_valid
assert_equal true, IbanChecker.check('TL380080012345678910157')
end
@thbar
thbar / plug_demo.exs
Last active October 18, 2017 16:30
Start a small Elixir server from a script, then query it from inside the same script
#
# to run and restart continuously when the code changes:
#
# find scripts lib mix.* | entr -rc mix run --no-halt scripts/plug_demo.exs
#
defmodule MySimpleServerPlug do
# https://hexdocs.pm/plug/Plug.Conn.html
import Plug.Conn
def recurly_tax_info(request)
# dump from https://api.recurly.com/js/v1/tax?vat_number=&country=FR&currency=USD&version=3.0.10&key=my_key&callback=__jp1
country = request.uri.query_values['country']
currency = request.uri.query_values['currency']
vat_number = request.uri.query_values['vat_number']
expect(country).to eq('FR')
expect(currency).to eq('USD')
expect(vat_number).to eq('')
@thbar
thbar / .bash_profile
Created June 25, 2017 09:51
If you switch between outdoor and indoor terminal work
function darkback {
osascript -e "tell application \"Terminal\" to set background color of window 1 to {0,0,0}"
}
function lightback {
osascript -e "tell application \"Terminal\" to set background color of window 1 to {65535,65535,65535}"
}
@thbar
thbar / gem_update.rb
Last active June 10, 2017 12:24
A lo-tech gem auto-updated script
# Important: please make sure to still carefully read each gem changelog!
require 'yaml'
def system!(cmd)
raise "Cannot run command!!!" unless system(cmd)
end
gems = YAML.load(IO.read('outdated.yml')).fetch('gems')
gems.each do |gem|
@thbar
thbar / 005_add_vat_rates.rb
Last active December 17, 2019 10:01
Creating a foreign data wrapper with Postgres and Sequel
# NOTE: several improvements to be made but I'm in a VAT preparation hurry
# - use "up" and "down" to make the migration reversible
# - use Sequel methods for extension/server create
Sequel.migration do
change do
file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'vat_rates.csv'))
run 'CREATE EXTENSION file_fdw;'
run 'CREATE SERVER file_fdw_server FOREIGN DATA WRAPPER file_fdw;'
create_table(:vat_rates, foreign: 'file_fdw_server', options: {format: 'csv', header: 'true', filename: file, delimiter: ','}) do
String :month, null: false
@thbar
thbar / rvm_spec.rb
Created March 15, 2017 14:07
Using ServerSpec to verify per-user RVM setup
def command_as(command, as:)
command(%Q{su -c #{Shellwords.escape(command)} --login #{as}})
end
%w(my_user).each do |user|
describe "RVM setup for user #{user}" do
%w(ruby-2.4.0).each do |ruby_version|
let(:subject) { command_as('rvm list strings', as: user) }
it "has #{ruby_version} installed" do
expect(subject.stdout.split("\n")).to include(ruby_version)
@thbar
thbar / Gemfile
Created March 10, 2017 16:05
A way to log errors in red with Ruby's Logger & the colorize gem
source 'https://rubygems.org'
gem 'colorize'
@thbar
thbar / class_method_instrumentation.rb
Created March 8, 2017 14:27
A cheap class method call tracing instrumentation (without using doubles)
# Example of use to detect a class method call throughout a whole test suite
# This could also be achieved with a RSpec double.
class Dir
old_method = method(:[])
define_singleton_method(:[]) do |*args|
# Use failure in a test-suite where you'd need to detect use
fail "FIXME - Dir[] called with args #{args.inspect}"
# Or call through using this, if you only need to trace
# old_method.call(*args)