Skip to content

Instantly share code, notes, and snippets.

💭
I may be slow to respond.

David david-pm

💭
I may be slow to respond.
Block or report user

Report or block david-pm

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@david-pm
david-pm / reset_push
Last active Jan 25, 2019
bash script that resets and force pushes
View reset_push
#!/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=$?
@david-pm
david-pm / protection_proxy.rb
Last active Dec 29, 2018
Protection Proxy Pattern example that respects Interface Segregation
View protection_proxy.rb
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 / convert_to_snakecase
Created Dec 21, 2018
a sed script to convert camelcase and or pascalcase to snakecase
View convert_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 / pre-push
Created Dec 10, 2018
pre-push git hook that runs rubocop on latest commit
View pre-push
#!/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 / clear_logs.rb
Created Dec 9, 2018
clear dev logs on app boot
View clear_logs.rb
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 / snippets.txt
Created Dec 7, 2018
chat app demo code snippets
View snippets.txt
> rails g controller home index
> rails g channel chat
-------
config/routes.rb:
-------
root 'home#index'
@david-pm
david-pm / wat2wasm.txt
Created Jun 5, 2018
A wat2wasm tutorial
View wat2wasm.txt
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 / blockchain.rb
Created May 29, 2018
blockchain poc in ruby
View blockchain.rb
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 / binance.rb
Created May 7, 2018
Binance REST API - signed request in Ruby
View binance.rb
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 Apr 24, 2018
Null Object Pattern
View active_nothing.rb
# 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)
You can’t perform that action at this time.