Skip to content

Instantly share code, notes, and snippets.

View ostinelli's full-sized avatar

Roberto Ostinelli ostinelli

View GitHub Profile
@ostinelli
ostinelli / activerecord_callback.md
Last active July 6, 2018 01:57
ActiveRecord callback when connection is made

The ConnectionAdapter defines two callbacks :checkout (connect) and :checkin (disconnect).

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.set_callback :checkout, :after do
  raw_connection
end

Source.

@ostinelli
ostinelli / reloader.rb
Last active July 1, 2018 08:18
Reload an initializer file on every request in Rails.
# add this into application.rb
initializer_file = Rails.root.join('config', 'initializers', 'my_initializer.rb')
reloader = ActiveSupport::FileUpdateChecker.new([initializer_file]) do
load initializer_file
end
ActiveSupport::Reloader.to_prepare do
reloader.execute
end
@ostinelli
ostinelli / aws_acl_updater.rb
Last active January 11, 2017 15:33
Batch update ACL permissions on AWS.
# add AWS credentials to ENV variables or modify the script to pass in credentials
require 'thread'
require 'aws-sdk'
# settings
region = 'my-region'
bucket = 'my-bucket'
prefix = 'my-prefix'
acl = "private"
parallel = 25
@ostinelli
ostinelli / Preferences.Sublime-settings
Created April 10, 2017 13:04
Sublime Preferences.
{
"ensure_newline_at_eof_on_save": true,
"font_size": 12.0,
"trim_trailing_white_space_on_save": true
}
@ostinelli
ostinelli / simple_api_client.rb
Last active December 13, 2018 15:04
Simple JSON API Ruby Client
require 'net/http'
require 'json'
class Client
def call(method, url, path=nil, headers={}, body=nil, timeout=60)
net_class = Object.const_get("Net::HTTP::#{constantize(method)}")
uri = URI("#{url}#{path}")
@ostinelli
ostinelli / biggest_file_by_ext.rb
Last active September 5, 2019 19:17
List biggest file size by extension on a directory.
require 'find'
biggest_file_by_ext = {}
Find.find('.') do |path|
next if File.directory?(path)
# get file info
ext = File.extname(path)
next unless ext.length > 0
# get file size
size = File.size(path)
@ostinelli
ostinelli / intellij
Last active November 7, 2019 15:21
[Intellij] Run mix tests in open terminal
Program: /Users/roberto/workspace/intellij_run_cmd
Arguments: "mix test $FilePathRelativeToProjectRoot$:$SelectionEndLine$"
@ostinelli
ostinelli / erlang@19.sh
Last active December 2, 2019 13:46
How to install erlang 19 on OSX
# download erlang 19: <http://erlang.org/download/otp_src_19.3.tar.gz> & untar
# download patch: <https://github.com/erlang/otp/commit/a64c4d806fa54848c35632114585ad82b98712e8.diff>
# apply patch
patch < a64c4d806fa54848c35632114585ad82b98712e8.diff
# ensure that openssl is installed in openssl@1.1/{version}
brew install openssl
# compile
@ostinelli
ostinelli / rsa_openssl_for_python.md
Last active April 16, 2024 07:05
RSA Private / Public key pair with openssl & Python

RSA Private / Pubic key pair

To generate a private / public RSA key pair, you can either use openssl, like so:

$ openssl genrsa -out private.pem 4096  
$ openssl rsa -in private.pem -outform PEM -pubout -out public.pem  

Or, you can use the following python script:

@ostinelli
ostinelli / list_bench.erl
Last active October 23, 2021 11:20
Erlang delete double values in arrays (dict vs orddict vs sets vs ordsets vs maps vs gb_sets benchmark)
-module(list_bench).
-compile([export_all]).
main(Count) ->
Values = lists:foldl(fun(C, Acc) ->
[C, C, C, C, C, C, C, C, C, C | Acc]
end, [], lists:seq(1, Count)),
{TimeDict0, ResultDict} = timer:tc(?MODULE, count_dict, [Values]),