Skip to content

Instantly share code, notes, and snippets.

View david-pm's full-sized avatar
💭
I may be slow to respond.

David McDonald david-pm

💭
I may be slow to respond.
View GitHub Profile
@david-pm
david-pm / binance.rb
Created May 7, 2018 03:12
Binance REST API - signed request in Ruby
require 'net/http'
require 'openssl'
# https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#general-api-information
# example curl request:
# `curl -H "X-MBX-APIKEY: #{api_key}" -X GET "https://api.binance.com/api/v3/account?recvWindow=100000&timestamp=#{current_time}&signature=#{signature}"`
api_key = '<replace this with your api_key generated on binance>'
secret_key = '<replace this with your secret_key generated on binance>'
@david-pm
david-pm / active_nothing.rb
Created April 24, 2018 03:43
Null Object Pattern
# Active Nothing - Null Object Pattern
class Bot
FAUX_DATABASE = [{ id: 1, name: 'KITT' }, { id: 3, name: 'GERTY' }].freeze
attr_reader :name
def self.find(id)
row = FAUX_DATABASE.find{ |r| r[:id] == id }
return nil if row.nil?
new(row)
--------r u b y
require 'csv'
require 'faker'
CSV.open("./users.csv", 'a+', write_headers: false) do |csv|
50_000.times do |n|
csv << [n, Faker::FunnyName.two_word_name, Faker::Internet.email, Faker::Address.city]
end
@david-pm
david-pm / tables_last_vacuumed_analyzed.sql
Last active January 16, 2018 02:41
psql - see when tables were last vacuumed and analyzed
SELECT relname, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze
FROM pg_stat_all_tables
WHERE schemaname = 'public';
@david-pm
david-pm / random_password
Last active February 22, 2017 06:11
generate a random password from cmdline
#!/bin/bash
#
# example usage
#
# > random_password 10
if [ $# -lt 1 ]; then
echo "This command requires an integer for the password length"
else
echo -n "password: "; base64 /dev/urandom | head -c $1
@david-pm
david-pm / rand
Created June 19, 2016 01:47
a guessing game disguised as a random number generator
#!/bin/bash
rand=$RANDOM
const=$((1 + rand % 100))
yellow="\033[33;40m"
none="\033[0m"
function game {
echo -e $yellow"Welcome to the guessing game!"$none
echo
@david-pm
david-pm / char_count
Created June 18, 2016 23:38
character count in each file in directory with and without special chars
#!/bin/bash
for i in $(ls)
do
without=$(cat $i | wc -c)
with=$(cat -vet $i | wc -c)
echo -n "chars including special chars in: $i "
echo "$with - $without"
done
@david-pm
david-pm / view_connections
Created June 11, 2016 18:47
See how many active connections there are to postgresql
SELECT count(*)
FROM pg_stat_activity
WHERE pid != pg_backend_pid()
AND usename = current_user;
@david-pm
david-pm / list_views
Created June 11, 2016 04:04
get a list of all custom SQL views in postgres
SELECT table_name
FROM INFORMATION_SCHEMA.views
WHERE table_schema = ANY (current_schemas(false));
@david-pm
david-pm / mem.sh
Last active August 13, 2016 07:09
monitor memory usage over time
#!/bin/bash
#
# example usage
#
# > mem 0.1 100
for i in `seq 1 $2`
do
free -m
cat /proc/meminfo | grep MemFree