Skip to content

Instantly share code, notes, and snippets.

View marcosgz's full-sized avatar

Marcos G. Zimmermann marcosgz

View GitHub Profile
@marcosgz
marcosgz / jquery_event_delegator.coffee
Created October 23, 2024 15:53 — forked from kaspermeyer/jquery_event_delegator.coffee
Dispatch jQuery events as regular DOM events
# ~ Dispatch jQuery events as regular DOM events ~
#
# Delegated events are given a new name in the format `jquery:<original event name>`.
# If you delegate `ajax:send` you will be able to listen for `jquery:ajax:send`
# on native event listeners such as Stimulus actions and `EventTarget.addEventListener`.
#
# Notes:
# * The first parameter must be called "event".
# * The parameters can be accessed as members on the `event.detail` object.
#
@marcosgz
marcosgz / stat_activity_query.sql
Created August 3, 2023 12:25
View active queries in Postgres
SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '1 minutes' and state<>'idle'
order by pg_stat_activity.query_start desc;
@marcosgz
marcosgz / esse-active_record-index-prune.rb
Last active August 23, 2022 15:26
Prune orphaned documents from esse index that uses the active_record plugin
# Remove orphaned items (documents without an equivalent database record) from elasticsearch/opensearch index
index = AccountsIndex # Target index
index.search(query: { match_all: {} }, _source: false).scroll_hits do |hits|
print(".")
es_ids = hits.map { |hit| hit.fetch("_id").to_i }
db_ids = index.repo.dataset.except(:includes, :preload).where(id: es_ids).pluck(:id)
prune_ids = es_ids - db_ids
next if prune_ids.none?
@marcosgz
marcosgz / docker-ssh-deploy.sh
Created May 11, 2022 15:35
SSH Agent forward in order to execute Capistrano deploy
docker-compose run --rm -v $(readlink -f $SSH_AUTH_SOCK):/ssh-agent -e SSH_AUTH_SOCK=/ssh-agent -e BRANCH=master app bundle exec cap staging deploy
@marcosgz
marcosgz / rubocop-modified-files.sh
Created May 2, 2022 21:04
Execute Rubocop Autocorrect on all modified files comparing current branch with staging
git diff --name-only HEAD..staging | grep -E -i 'rake|*.rb|*.erb' | perl -ne 'chomp(); if (-e $_) {print "$_\n"}' | xargs rubocop -A
module AdapterOne
def instance_method
':one instance method'
end
alias one instance_method
end
module AdapterTwo
def instance_method
':two instance method'
@marcosgz
marcosgz / openssl-genrsa.rb
Created June 23, 2021 22:16
RSA crypt/decrypt example using ruby and openssl
# Generate keys
# $ openssl genrsa -des3 -out private.pem 2048
# $ openssl rsa -in private.pem -outform PEM -pubout -out public.pem
# $ irb
require 'openssl'
require 'base64'
# Encrypt
public_key = OpenSSL::PKey::RSA.new(File.read('public.pem'))
@marcosgz
marcosgz / console.rb
Created June 19, 2021 14:31
Simple demonstration about md5 login
>> authenticate?(3, 'one')
=> false
>> authenticate?(1, 'one1')
=> false
>> authenticate?(1, 'one')
=> true
@marcosgz
marcosgz / encryption-decryption.rb
Created June 19, 2021 14:13 — forked from gevans/encryption-decryption.rb
A couple examples of using asymmetric RSA signing and encryption using Ruby's OpenSSL libraries.
require 'openssl'
key = OpenSSL::PKey::RSA.new(2048)
p encrypted_string = key.public_encrypt('my plaintext string', OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
p decrypted_string = key.private_decrypt(encrypted_string, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
@marcosgz
marcosgz / kmeans.rb
Last active March 24, 2021 22:50
RGB clustering using k-Means algorithm
#!/usr/bin/env ruby
# It's ODM (Object Document Mapper) to the RGB data
class RGB
COLORS = {
r: ->(val) { "\e[31m#{val}\e[0m" },
g: ->(val) { "\e[32m#{val}\e[0m" },
b: ->(val) { "\e[34m#{val}\e[0m" },
}.freeze