Skip to content

Instantly share code, notes, and snippets.

@nunosilva800
nunosilva800 / README.md
Last active September 28, 2017 12:35
A Widget on Rails

This is an example of how to implement a "widget" with Ruby on Rails.

The widget is added to a webpage with the example on test_widget.html.erb :

<div id="my-widget"></div>
<script src="https://my-domain.com/widget.js" type="text/javascript"></script>

This will request the JS code from the server, ensuring you can change it without having to update the webpage were the widget is.

@nunosilva800
nunosilva800 / interfaces.rb
Created May 22, 2018 11:44
ruby interfaces example
module APIProfile
def profile
raise NotImplementedError.new("You must implement '#{__method__}'.")
end
end
module APIAddress
def address
raise NotImplementedError.new("You must implement '#{__method__}'.")
end
@nunosilva800
nunosilva800 / git_delete_merged.sh
Last active July 6, 2018 13:30
cleaning git repo
# To delete all branches that are already merged into the currently checked out branch:
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
@nunosilva800
nunosilva800 / interleave.rb
Last active September 6, 2019 09:35
Interleave arrays in ruby
# Given a set of arrays with potentially infinite lengths
# 1. interleave the elements from input collections into a
# final 1-dimensional array
# 2. ensure the interleaving follows a frequency, as example:
# - 1 element from collection A (frequency: 1)
# - 2 element from collection B (frequency: 2)
# - 3 element from collection C (frequency: 3)
# - 4 element from collection D (frequency: 4)
# helper to generate infinite enumerators
### Keybase proof
I hereby claim:
* I am nunosilva800 on github.
* I am nunosilva800 (https://keybase.io/nunosilva800) on keybase.
* I have a public key ASCDtm17fxY-jO_t_Z1415ELEZZVwZKWx0y0WM7NiNcEnQo
To claim this, I am signing this object:
@nunosilva800
nunosilva800 / service_objects.md
Created March 5, 2019 16:50
service objects in rails

Service Objects

Service Objects are classes or modules that have one public method, often named #call, and are designed to perform a single task or service.

Service objects are single business actions.

Example:

As a class

@nunosilva800
nunosilva800 / uri_field.rb
Created November 4, 2014 20:29
ensure uri fields have a valid protocol. HTTP by default
module Concerns::URIField
extend ActiveSupport::Concern
included do
def self.ensure_valid_protocol_in_uri(field, default_protocol = "http", protocols_matcher="https?")
define_method "#{field}=" do |new_uri|
if new_uri.present? and not new_uri =~ /^#{protocols_matcher}:\/\//
new_uri = "#{default_protocol}://#{new_uri}"
end
super(new_uri)
@nunosilva800
nunosilva800 / replication-planner.rb
Created January 14, 2022 15:35
Plan Kafka cluster expansion by increased replication factor. Assumes new broker ids are shifted by 5.
require 'optparse'
require 'json'
options = {}
op = OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-c", "--context REQUIRED", String, "k8s context") do |v|
options[:context] = v
end

Index read_only_allow_delete can be set to true automatically when disk space is low. This allows writing to indexes again:

curl -X PUT "http://localhost:9200/_all/_settings?pretty" -H 'Content-Type: application/json' -d'
{                
    "index.blocks.read_only_allow_delete": false
}
'