Skip to content

Instantly share code, notes, and snippets.

View stephaneliu's full-sized avatar

Stephane Liu stephaneliu

View GitHub Profile
@stephaneliu
stephaneliu / record_strategy.rb
Last active April 4, 2023 08:01
Custom RuboCop cop to prevent using VCR cassettes with record strategies that make live requests
# .rubocop.yml
require:
- ./lib/custom_cops/vcr/record_strategy.rb
# lib/custom_cops/custom_cop_base.rb
# frozen_string_literal: true
# See https://docs.rubocop.org/rubocop/1.23/development.html for more info on creating new [Rubo]cops
module CustomCops
class CustomCopBase < RuboCop::Cop::Base
@stephaneliu
stephaneliu / .solargraph.yml
Created February 28, 2023 18:34
solargraph
---
include:
- "**/*.rb"
exclude:
- spec/**/*
- test/**/*
- vendor/**/*
- ".bundle/**/*"
require: []
domains: []
#!/usr/bin/env ruby
run_check = ENV['SAFE_COMMIT'].nil?
if run_check
focus_hits = []
binding_hits = []
# Find all filenames in spec directory that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
# https://replit.com/join/eugzahzgxx-stephaneliu1
module GridPrinter
def print
sleep(0.2)
system 'clear'
(1..grid_size).each do |row|
puts grid[row][1..grid_size].join(' ')
end
end
require 'net/http'
require 'uri'
Net::HTTP.get(URI.parse('https://somewhere.com'))
# Gemfile
group :test do
gem "vcr", require: false
gem "webmock"
end
# spec/support/vcr.rb
# frozen_string_literal: true
require "vcr"
class Test < Thor
include Thor::Actions
desc "insert", "an example"
def insert
content = <<~EOL
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }\n\n
EOL
insert_into_file("spec/rails_helper.rb", content, before: "RSpec.configure do")
class CreateMailer
def initialize(event_catalog, ccir_mailer_policy_class)
@event_catalog = event_catalog
@ccir_mailer_policy = ccir_mailer_policy_class.new(event_catalog)
end
def send
return false unless ccir_mailer_policy.send_email?
# ...
end
class Event < ApplicationRecord
# Before
def cateorized?
event.category.present? ? event.category.name != 'none' : false
end
# Refactor - simplify with Null test with AND operator
def categorized?
event.category && event.category != 'none'
end
# Before
class CustomQuery
def levels
@levels ||= { 'a' => :low, 'b' => :med, 'c' => :high }
end
def current(level)
levels.each_with_index { |avail_level, index| return avail_level[index] if level == avail_level[index] }
:low
end