Skip to content

Instantly share code, notes, and snippets.

View joaocarmo's full-sized avatar
🍺
Beer

João Carmo joaocarmo

🍺
Beer
View GitHub Profile
@joaocarmo
joaocarmo / uniform_image.rb
Last active June 11, 2018 17:15
Using Ruby's RMagick to process (crop and/or resize) an image given simple inputs (new ratio/size)
#!/usr/bin/env ruby
require "rmagick"
# Define class UniformImage
class UniformImage
def initialize(image, suffix="_uniform", extension="jpg")
# Instance variables
@image = image
if self.exists?
@joaocarmo
joaocarmo / getNestedValue.js
Last active September 11, 2018 09:32
Get nested value from an object
/*
Usage: getNestedValue(<object>, <array>)
const object = {
key1: {
key11: 'nested-value'
}
}
const nestedValue = getNestedValue(object, ['key1', 'key11'])
@joaocarmo
joaocarmo / services_robot.sh
Created September 6, 2018 16:35
Robot watcher for Linux services
#!/usr/bin/env bash
##this script will check mongodb and apache
##if that service is not running
##it will start the service and send an email to you
##if the restart does not work, it sends an email and then exits
##set the path ##this works for Ubuntu 14.04 and 16.04
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@joaocarmo
joaocarmo / http_request.rb
Last active September 7, 2018 17:38
Execute HTTP requests with Ruby's net/http
require "net/http"
require "json"
# Execute HTTP requests
# How to use
# http_request({
# "uri" => "...", # Use URI::Parser
# "method" => "...",
# "payload" => "...",
# "headers" => "...",
@joaocarmo
joaocarmo / get_nested_value.py
Created September 11, 2018 09:39
Get nested value from an object (Python 2.x and 3.x compatible)
#!/usr/bin/env python
try:
from functools import reduce
except ImportError:
pass
# How to use
# foo = { "a": "a1", "b": "a2", "c": { "d": "a3" } }
# bar = ["c", "d"]
# nested_value = get_nested_value(foo, bar)
@joaocarmo
joaocarmo / .bash_profile
Created November 19, 2018 11:45
How to use math in the terminal
# Just include this function in the .bash_profile (or equivalent) file
math() {
# Use python to perform mathematics
if [ -z $1 ]; then
echo "Usage: math [expression]"
return 1
fi
python -c "print $1"
}
@joaocarmo
joaocarmo / setNestedValue.js
Created July 1, 2019 11:19
Set an object's nested value
/*
Usage: setNestedValue(<object>, <array>, <value>)
const object = {
key1: {
key11: 'nested-value'
}
}
const newObject = setNestedValue(object, ['key1', 'key12'], 'another-nested-value')
@joaocarmo
joaocarmo / python.sh
Last active July 31, 2019 12:27
Python wrapper to select the best interpreter available in the system
#!/usr/bin/env bash
################################################################################
# Python Wrapper
# ------------------------------------------------------------------------------
# Author: Joao Carmo
# License: MIT
# ------------------------------------------------------------------------------
# This wrapper script will test each available Python interpreter in the system
# to find the one which has the most critical modules installed and then execute
@joaocarmo
joaocarmo / tasks.json
Last active January 10, 2020 10:26
VSCode Custom Task: Preview as HTML in Chrome
{
"version": "2.0.0",
"tasks": [
{
"label": "Preview as HTML in Chrome",
"type": "shell",
"osx": {
"command": "cp ${file} /tmp/${fileBasename}_preview.html && /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome /tmp/${fileBasename}_preview.html && sleep 5 && rm -f /tmp/${fileBasename}_preview.html",
}
}
@joaocarmo
joaocarmo / git-delete-interactive-local-branch.sh
Last active September 13, 2021 12:14
Interactively delete local branches (git)
#!/bin/bash
################################################################################
# Interactive Git Delete
# ------------------------------------------------------------------------------
# Author: Joao Carmo
# License: MIT
# ------------------------------------------------------------------------------
# This script will list all available local branches and you can select one or
# more to delete using numbers (from the list).