Skip to content

Instantly share code, notes, and snippets.

View danhodge's full-sized avatar

Dan Hodge danhodge

View GitHub Profile
@danhodge
danhodge / .rbenv-version
Last active August 29, 2015 14:07
EXIF parsing
1.9.3-p429
@danhodge
danhodge / certs.rb
Last active August 29, 2015 14:19
Dealing with Certificates
require "base64"
require "digest"
require "openssl"
# openssl req -in <csr_file> -noout -pubkey
def extract_public_key_from_csr(csr_file)
csr = OpenSSL::X509::Request.new(File.read(csr_file))
csr.public_key.to_pem
end
@danhodge
danhodge / scan.rb
Created January 13, 2016 21:42
Search for a string in a stream of data that is read in chunks
def each_chunk(io, chunk_size = 1024 * 1024)
return enum_for(:each_chunk, io, chunk_size) unless block_given?
loop do
break if io.eof?
yield io.read(chunk_size)
end
end
def scan(io, search_term, buffer_size)
@danhodge
danhodge / tricks.rb
Last active July 1, 2020 20:24
Ruby Tricks
# Define a method as both an instance & class method
class Foo
def self.bar
end
# Note: this requires Rails/Active Support - not sure if it can be done with Forwardable
delegate :bar, to: "self.class"
end
@danhodge
danhodge / xmlnl.sh
Created April 9, 2018 17:11
Insert line breaks into XML data
#!/bin/sh
# Takes a file path as input and echoes it back to STDOUT with a newline inserted between every adjacent > and < characters
sed 's/\>\</\>\
\</g' < $1
@danhodge
danhodge / elisp.el
Created September 14, 2018 17:13
Emacs LISP Scratchpad
; Trying to figure out how namespaced elisp functions work: https://www.greghendershott.com/2017/02/emacs-themes.html
(defun dh/foo (x)
"adds 4 to x"
(+ x 4))
(dh/foo 1)
(defun dh/bar (x, fn)
"adds 5 to fn(x)"
(+ 5 (fn x)))
(dh/bar 7 #'dh/foo)
@danhodge
danhodge / resque.rb
Last active October 24, 2018 13:32
Resque Basics
# Add following to enable debug logging of Resque internals
Resque.logger = Logger.new(STDOUT)
Resque.logger.level = Logger::DEBUG
# Use resque-retry plugin for automatic retries
class SomeJob
extend Resque::Plugins::Retry
end
# The scheduler process must be running to perform retries - if not running, the job
@danhodge
danhodge / faraday.rb
Created October 15, 2018 15:34
Faraday Notes
# How to stub out Faraday for testing
# The last middleware in the list is the innermost one and must be the HTTP adapter.
# Generally, it will be a single argument (a symbol), such as :net_http
#
# For example:
#
Faraday.new(url: 'http://example.com') do |conn|
# POST/PUT params encoders:
conn.request :multipart
@danhodge
danhodge / rails.rb
Last active June 7, 2021 14:22
Rails Conditional Routing
# Conditional Routing in Rails
# Send requests matching the same path to different controller actions based on information in the request
# http://bjedrocha.com/rails/2015/03/18/role-based-routing-in-rails/
class ThingsConstraint
def initialize(*names)
@matches = names
end
def matches?(request)