Skip to content

Instantly share code, notes, and snippets.

@esaborit4code
esaborit4code / credentials.rake
Last active August 20, 2024 16:28
Rake tasks to manage credentials
# frozen_string_literal: true
# These are custom Rake tasks to decrypt credentials in plain files and encrypt them back after changes (useful for editing in your local IDE if the development environment is Docker, where `rails credentials:edit` can't open your IDE):
# ```bash
# # Decrypt credentials
# bin/rails credentials:decrypt
# # Encrypt credentials
# bin/rails credentials:encrypt
@esaborit4code
esaborit4code / rspec_mock_kwargs_spec.rb
Last active February 27, 2024 10:58
Demonstration of issue in rspec-mocks. All these tests should fail, but the misbehavior makes "with double with `have_received`" pass.
require 'spec_helper'
class Dummy
def go(a:)
end
end
RSpec.describe 'test' do
# All these tests should fail, but the misbehavior makes "with double with `have_received`" pass.
subject { Dummy.new.go(args) }
@esaborit4code
esaborit4code / bm.rb
Created August 9, 2018 10:33
Cheap Benchmark
class BM
def self.bm(name)
@stats ||= {}
@stats[name] ||= { time: 0, times_called: 0, avg_time: 0 }
start_time = Time.zone.now
yield
@stats[name][:time] += (Time.zone.now - start_time)
@stats[name][:times_called] += 1
@stats[name][:avg_time] = @stats[name][:time].to_f / @stats[name][:times_called]
end
@esaborit4code
esaborit4code / running_apps.rb
Last active July 17, 2018 14:23
Heroku running apps
p `heroku apps -o barkibu`.split("\n").drop(1).map { |app| app.gsub(/\s\(.*\)/, '') }.each_with_object({}) { |app, collector| collector[app] = `heroku ps -a #{app}` }.keep_if { |k,v| !(v =~ /No dynos/) }.keys
@esaborit4code
esaborit4code / remove_unedited.rb
Last active February 3, 2023 19:23
Sloppy script to remove unedited photos from iPhotos export
#!/usr/bin/ruby
require 'fileutils'
def basename(file)
File.basename(file, File.extname(file))
end
def all
Dir.glob('*').map
@esaborit4code
esaborit4code / copywise.rb
Last active September 28, 2018 18:47
Ignore or rename files with duplicated name
#!/usr/bin/env ruby
require 'fileutils'
require 'byebug'
VERBOSE = false
DRY_RUN = false
class Conflict
SUFFIX_SEPARATOR = '-'
@esaborit4code
esaborit4code / rediparser.rb
Created June 10, 2016 12:06
Locale redirections parser
LOG_FILENAME = 'heroku-logs'
REQUEST_START_REGEX = /(.+)Started (.+) \"(.+)\"/
REDIRECT_REGEX = /(.+)Redirected to (.+barkibu.com)(.+)/
log_lines = File.readlines(LOG_FILENAME)
redirects = []
last_request = {}
log_lines.each do |line|
@esaborit4code
esaborit4code / rediparser.rb
Created June 10, 2016 11:20
Redirection parser
LOG_FILENAME = 'heroku-logs'
REQUEST_START_REGEX = /(.+)Started (.+) \"(.+)\"/
REDIRECT_REGEX = /(.+)Redirected to (.+barkibu.com)(.+)/
log_lines = File.readlines(LOG_FILENAME)
redirects = []
last_request = {}
log_lines.each do |line|
#!/usr/bin/env ruby
require 'json'
exec 'rspec --format json --out rspec_results.json' unless File.exists? 'rspec_results.json'
results = JSON.parse(File.read 'rspec_results.json')
total_runtime = results['summary']['duration']
total_examples = results['examples']
@esaborit4code
esaborit4code / db_change_on_the_fly
Last active August 29, 2015 14:11
DB server change on the fly
mysqldump -h origin.server.com -u the_origin_user -p the_origin_database --ignore-table=the_origin_database.that_huge_table | tee dump.sql | mysql the_database -u the_user --password=ThePassword
mysqldump -h origin.server.com -u the_origin_user -p the_origin_database that_huge_table --no-data | mysql the_database -u the_user --password=ThePassword
mysqldump -h origin.server.com -u the_origin_user -p the_origin_database that_huge_table | mysql the_database -u the_user --password=ThePassword