Skip to content

Instantly share code, notes, and snippets.

@jaydorsey
jaydorsey / example.rb
Created February 16, 2024 14:39
Storing images as base64 representations
require 'base64'
file = File.open('path/to/file.ext', 'rb') # Read the image as binary
base64 = Base64.strict_encode64(file.read) # This gives you a string you can assign to a constant/variable
tmpfile = Tempfile.new(Base64.strict_decode64(base64)) # Decode and create a temporary file
Digest::MD5.file(tmpfile).base64digest # This is one way to create a checksum on the file
File.size(tmpfile) # You can also get a file size
@jaydorsey
jaydorsey / my_stuff.rb
Created January 26, 2024 14:06
RSpec Rails.logger block test
# frozen_string_literal: true
class MyStuff
OOPS = 'Oops'
def self.call
Rails.logger.warn { OOPS }
end
end
@jaydorsey
jaydorsey / example_spec.rb
Created November 15, 2022 13:47
Using stub_const to hijack a constant
# Lines marked with pry are just for testing in a rails console
require 'rspec/mocks' # pry
include RSpec::Mocks::ExampleMethods # pry
# This bypasses the warning that the method isn't called inside of a test
RSpec::Mocks.with_temporary_scope do # pry
stub_const(
'MyClass',
instance_double(
@jaydorsey
jaydorsey / bar.rb
Last active August 19, 2022 14:41
Ruby modules, classes, and instance methods
# An example of class/instance methods w/ modules
module Foo
def self.included(base)
base.extend(ClassMethods)
end
def foo
puts 'Bar.new.foo prints this'
end
@jaydorsey
jaydorsey / gist:fbd6d569b702ce9fbe10c806fefe2eb3
Created August 12, 2022 03:05
Examples of gem/bundle install w/ cflags
# these are examples; maybe not the right flags
gem install ffi -v '1.12.2' -- -- with-cflags="-Wno-error=implicit-function-declaration"
bundle config build.ffi --with-cflags=\"-Wno-error=implicit-function-declaration\"
@jaydorsey
jaydorsey / unused.md
Created July 20, 2022 13:11
Finding unused code in ruby and ruby on rails projects

This is a strategy I use to find unused code in ruby projects. These instructions are for macOS, but you only need access to the tools for this to work.

  1. Install unused following the instructions here
  2. Install universal-ctags. I use the brew instructions here
  3. Generate your tag file from your project root, including your library methods as well. I typically run /usr/local/bin/ctags . $(bundle list --paths) to add all of my library methods for the project. This has some other benefits, like allowing code-jumping in vim

    Bonus: You can set this command up as a git hook to run every time you make changes to your code locally. I have an example of this here

  4. Once this command is done, you can look at the tags file in your repository root to make sure it's populated with data correctly (`cat tags | w
@jaydorsey
jaydorsey / integration.yml
Created June 7, 2022 18:17
Using hurl.dev for integration tests
---
name: Run Integration Tests
on: [workflow_dispatch]
# .github/workflows/integration.yml
#
# You manually trigger this from your Github actions. Maybe after merging to main, and triggering
# a deploy out to your pre-production environment
#
@jaydorsey
jaydorsey / heroku_secret_inventory.rb
Created June 2, 2022 14:59
Heroku secret inventory
# frozen_string_literal: true
# This script uses the platform-api gem to retrieve secrets from Heroku apps, pipelines, and test
# environments. This can be used to generate an inventory of all your secrets, so you can cross-reference
# it to determine if you had any pipeline/test secrets which were compromised as part of the recent
# Heroku breach
#
# I suggest importing these CSVs into a database, adding a couple metadata fields (compromised:boolean,
# notes:text, environment:string, status:string) and writing some SQL to come up with a list of secrets
# that were re-used across different environments
@jaydorsey
jaydorsey / private_bundle_install_gist.md
Last active April 28, 2025 23:49
Setting up a github/bundle token for privately hosted repos

If your Gemfile has a privately hosted package referenced similar to this:

gem 'sekret', git: 'https://github.com/my-private-group/sekret.git', branch: 'main'

You may see a prompt when running bundle install, or other bundler commands, to enter your github username & password.

To resolve this, you need to generate a token and add it to your system.

Generating a token

@jaydorsey
jaydorsey / 20211227213338_create_users.rb
Last active December 27, 2021 22:17
Converting a boolean column to an enum in ActiveRecord/Rails
# frozen_string_literal: true
# db/migrate/20211227213338_create_users.rb
#
# This is an example of what the Users table might have been created as
# or would look like before the migration from a boolean field to an enum
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :name, null: false