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 / 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)
@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 / blockchain.rb
Created May 29, 2018 01:27
blockchain poc in ruby
require 'digest'
class Block
attr_reader :index, :parent, :created_at, :data, :hash
def initialize(index:, data:, parent: '')
@index = index.to_i
@data = data
@parent = parent.empty? ? '0' : parent
@created_at = Time.now.to_s
@david-pm
david-pm / wat2wasm.txt
Created June 5, 2018 02:32
A wat2wasm tutorial
INSTALL:
$ git clone --recursive https://github.com/WebAssembly/wabt
$ cd wabt
$ make clang-release
$ cd out/clang/Release
$ pwd | pbcopy (or just pwd and copy the output manually)
Add the pwd path to your shell config $PATH for ease of use.
@david-pm
david-pm / snippets.txt
Created December 7, 2018 20:59
chat app demo code snippets
> rails g controller home index
> rails g channel chat
-------
config/routes.rb:
-------
root 'home#index'
@david-pm
david-pm / clear_logs.rb
Created December 9, 2018 22:10
clear dev logs on app boot
return unless Rails.env.development?
MAX_LOG_SIZE = 2.megabytes
logs = File.join(Rails.root, 'log', '*.log')
if Dir[logs].any? { |log| File.size?(log).to_i > MAX_LOG_SIZE }
$stdout.puts "---------- \033[32m clearing dev logs \033[0m --------------"
`rails log:clear`
end
@david-pm
david-pm / pre-push
Created December 10, 2018 05:16
pre-push git hook that runs rubocop on latest commit
#!/usr/bin/env ruby
changed_files = `git diff-tree --no-commit-id --name-only --diff-filter=AM -r HEAD`.lines.map(&:chomp)
return if changed_files.empty?
results = `bundle exec rubocop --color #{changed_files.join(" ")}`
no_offenses_detected = results.include?("\e[32mno offenses\e[0m detected")
return if no_offenses_detected
@david-pm
david-pm / convert_to_snakecase
Created December 21, 2018 23:39
a sed script to convert camelcase and or pascalcase to snakecase
# sed -i -f convert_to_snakecase __yourfile__
#
# to run this within in vim:
# %s/[A-Z]/_\L&/g | %s/\<_//g
s/[A-Z]/_\L&/g; s/\b_//g
@david-pm
david-pm / protection_proxy.rb
Last active December 29, 2018 03:51
Protection Proxy Pattern example that respects Interface Segregation
class BankAccount
attr_reader :balance
def initialize(starting_balance)
@balance = starting_balance
end
# if this were #irl we'd need to worry about race conditions here
def deposit(amount)
@balance += amount
end
@david-pm
david-pm / reset_push
Last active January 25, 2019 02:41
bash script that resets and force pushes
#!/bin/bash
# resets HEAD back one commit, recommits with provided message and force pushes to origin
commitmsg=$1
# stole function from: .oh-my-zsh/lib/git.zsh
function current_git_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?