Skip to content

Instantly share code, notes, and snippets.

View ostinelli's full-sized avatar

Roberto Ostinelli ostinelli

View GitHub Profile
@ostinelli
ostinelli / stub_environment.rb
Created September 10, 2015 14:10
Stub Rails environment
def stub_rails_env_as(env)
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new(env.to_s))
end
@ostinelli
ostinelli / simulator.sh
Created March 22, 2016 10:11
Open iOS simulator directly from console
alias simulator='open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app'
@ostinelli
ostinelli / import.sh
Created November 10, 2015 17:24
Heroku Import database
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d {database} sql.dump
@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 / resource_monitor.sh
Last active November 19, 2017 01:01
Monitor server CPU and memory.
#!/usr/bin/env bash
# settings
OUTPUT_FILE=monitor.csv
# build command
cmd=""
cpu_headers=""
for (( i=4; i<=$(nproc)+4; i++ ))
do
@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 / 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 / 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)