Skip to content

Instantly share code, notes, and snippets.

@kapcod
kapcod / deep_freeze.rb
Last active July 6, 2017 08:32
Ruby Deep Freeze for Hash and Array classes
# Adds deep_freeze! method to Hash
Hash.class_eval do
def deep_freeze!
each_value{|val| val.deep_freeze! if val.respond_to?(:deep_freeze!) }
freeze
end
end
# Adds deep_freeze! method to Array
Array.class_eval do
@kapcod
kapcod / find-commit.rb
Created May 1, 2016 15:43
Finds git commit that merged other commit into master
commit = ARGV[0]
master = ARGV[1] || 'origin/master'
unless commit
puts "Usage: find-commit.rb commit [master-branch]"
puts "Will show commit that merged <commit> into <master-branch>"
exit 1
end
parents = `git rev-list #{commit}..#{master} --reverse --first-parent --merges`.split("\n")
@kapcod
kapcod / ilya.track_sleep.plist
Created May 24, 2016 10:46
Track sleep of the computer with Mac OSX agent config for autoload
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ilya.track_sleep</string>
<key>Program</key>
<string>/Users/sa/track_sleep.rb</string>
<key>WorkingDirectory</key>
<string>/Users/sa</string>
@kapcod
kapcod / fix_cookies_escaping.rb
Created January 5, 2017 12:57
Fix Rack::Utils.set_cookie_header! and delete_cookie_header! escaping
class << Rack::Utils
alias_method :escape_with_plus, :escape
def escape_without_plus(s)
URI.encode_www_form_component(s).gsub('+', '%20')
end
def mock_escape_for_cookies
singleton_class.send :alias_method, :escape, :escape_without_plus
yield
@kapcod
kapcod / case_sensitive_headers_post.rb
Created October 16, 2017 20:09
Hack to send request using Net::HTTP with preserved headers case
class CaseSensitiveHeadersPost < Net::HTTP::Post
def initialize_http_header(headers)
@header = {}
headers.each do |k,v|
@header[k] = [v]
end
end
def capitalize(name)
name
@kapcod
kapcod / README.md
Created August 25, 2020 15:33 — forked from skwashd/README.md
Copy AWS SSM Parameter Store Path

This Python (3.6+) script is for migrating Amazon AWS System Manager (SSM) Parameter Store keys from one path to another.

Quick Start

To install the script do the following:

  • Configure your AWS credentials
  • Grab the code from this gist
  • Make it executable (chmod +x /path/to/copy-ssm-ps-path.py)
  • pip install boto3 (if you don't have it installed already)
@kapcod
kapcod / action_rate_limit.rb
Last active April 9, 2021 20:05
Simple action rate limit module for Ruby on Rails, intended to be included in ApplicationController
# frozen_string_literal: true
module ActionRateLimit
LIMIT_BY_OPTIONS = {
user: proc { request.cookies['user_id'] },
ip: proc { request.remote_ip },
}.freeze
def self.included(klass)
klass.extend ClassMethods
# frozen_string_literal: true
# SimpleThreadPool gives a simple implementation for producer/consumers pattern
# Example:
#
# pool = SimpleThreadPool.new(10, max_queue: 5) do |user_params|
# Net::HTTP.post(UPDATE_API, user_params)
# end
# users.each do |user|
# pool << user.update_api_params
@kapcod
kapcod / cleanup_airflow_logs.rb
Created October 10, 2021 18:27
Airflow DAG log cleaner. Assumes log structure /var/log/airflow/<dag>/<operator>/<date>. Call: `cleanup_airflow_logs.rb 7` to retain only last week of logs
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'fileutils'
require 'time'
class AirflowLogCleaner
attr_reader :list, :to_delete
def list_folders
@kapcod
kapcod / export_athena_usage.rb
Created December 29, 2021 11:17
Export AWS Athena usage history as CSV
require 'csv'
require 'aws-sdk-athena'
def export_athena_usage(limit:, region:, filter_scanned: 10**9)
athena = Aws::Athena::Client.new region: region
next_token = nil
fields = %w[query_execution_id query work_group]
stats_fields = %w[data_scanned_in_bytes total_execution_time_in_millis]
scanned = 0
found = 0