Skip to content

Instantly share code, notes, and snippets.

View marcosgz's full-sized avatar

Marcos G. Zimmermann marcosgz

View GitHub Profile
@marcosgz
marcosgz / puma.monitrc
Created December 13, 2017 15:56 — forked from sudara/puma.monitrc
Example config needed to use monit with puma, monitoring workers for mem.
# this monit config goes in /etc/monit/conf.d
check process puma_master
with pidfile /data/myapp/current/tmp/puma.pid
start program = "/etc/monit/scripts/puma start"
stop program = "/etc/monit/scripts/puma stop"
group myapp
check process puma_worker_0
with pidfile /data/myapp/current/tmp/puma_worker_0.pid
@marcosgz
marcosgz / ruby-memoization.rb
Last active May 14, 2018 15:01
Ruby Memoization using Singleton Method
class A
def name
@name ||= rand
end
end
class B
# Ruby Memoization using Singleton Method
def name
def self.name; @name; end
module ViewAssigns
def self.included(base)
puts "Included #{ self }/#{self.singleton_class} into #{base}/#{base.singleton_class}"
class << base
attr_accessor :assigns
def assign(name, value = nil)
self.assigns ||= {}
self.assigns[name] = value
@marcosgz
marcosgz / ruby-sdk-2-batch-update-acl.rb
Created August 8, 2018 21:55
Recursive add ACL of all files from S3 bucket
# Aws::VERSION
# => "2.11.97"
credentials = Aws::Credentials.new(ENV['AWS_KEY'], ENV['AWS_SECRET'])
client = Aws::S3::Client.new(credentials: credentials)
s3 = Aws::S3::Resource.new(credentials: credentials)
s3_bucket = s3.bucket(ENV['BUCKET'])
# https://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#put_object_acl-instance_method
s3_bucket.objects(prefix: 'path/to/directory').each do |obj|
client.put_object_acl(bucket: s3_bucket.name, key: obj.key, acl: 'public-read').to_h
@marcosgz
marcosgz / boolean_value.rb
Created August 23, 2018 21:04 — forked from attenzione/boolean_value.rb
Ruby: casting to boolean value
class BooleanValue
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/column.rb#L8
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']
def self.from_string(value)
Rails.present? ? with_rails(value) : without_rails(value)
end
private
require 'nokogiri'
require 'cgi'
xml = <<~XML
<export>
<row>
<CODIGO>13213215</CODIGO>
<MERCADORIA>Exemplo <1</MERCADORIA>
<II></II>
<PIS></PIS>
@marcosgz
marcosgz / postgres_queries_and_commands.sql
Created April 1, 2020 19:27 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@marcosgz
marcosgz / knn.rb
Last active March 17, 2021 00:39
K-NN algorithm using RGB samples
#!/usr/bin/env ruby
# It's ODM (Object Document Mapper) to the RGB data
class RGB
# @overload initialize(r, g, b, value)
# @param r [Integer] Value for R
# @param g [Integer] Value for G
# @param b [Integer] Value for B
# @param value [Object] describe value param
# @overload initialize(r, g, b)
@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
@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)