Skip to content

Instantly share code, notes, and snippets.

@stevenkaras
stevenkaras / url_helpers_env.rb
Last active August 29, 2015 14:00
Environment variable based URL helper
class URLHelpers::Env
include ActionController::UrlFor
include Sprockets::Rails::Helper
include Rails.application.routes.url_helpers
def self.config
Rails.application.config
end
def self.app
# Copyright Steven Karas 2013 - 2014
# Licensed under MIT terms
# Represents a robots.txt file
class Robots
# Parse the given content as a robots.txt file
#
# @see http://www.robotstxt.org/
def initialize(content)
@stevenkaras
stevenkaras / email_validator.rb
Last active August 29, 2015 14:07
Email Validation class
require 'resolv'
require 'net/smtp'
class EmailValidator
# Note that obsolete syntax support has been stripped
Pattern = %r{
# From RFC 5322 s 3.2.1
(?<quoted-pair> \\ [\x21-\x7e \t]){0}
# From RFC 5322 s 3.2.3
@stevenkaras
stevenkaras / migrate.rb
Created January 19, 2015 18:05
Rails Migration/Copy/Merge Tool
# migrate.rb Migration/Copying/Merging Tool
# By Steven Karas
# MIT License
require 'csv'
require 'logger'
class Migration
def initialize(path, logger = Logger.new(nil))
@path = path
@stevenkaras
stevenkaras / routing_bug.rb
Last active August 29, 2015 14:27
Rails bug when routing escaped entities
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
@stevenkaras
stevenkaras / manacher.rb
Created September 15, 2015 16:09
Manacher's Algorithm for finding palindromic sequences in Ruby
# Detect all the palindromic sequences in a given string
#
# @return [<Range>] a list of all the palindromes in the given string
def all_palindromes(string)
preprocessed = "^#{string.chars.join("#")}$"
palindromes = [0] * preprocessed.length
center = 0
right = 0
for i in 1...preprocessed.length
mirror = center * 2 - i
@stevenkaras
stevenkaras / keybase.md
Created October 2, 2015 11:34
Keybase identity proof

Keybase proof

I hereby claim:

  • I am stevenkaras on github.
  • I am stevenkaras (https://keybase.io/stevenkaras) on keybase.
  • I have a public key whose fingerprint is E751 0CE5 AFA6 EC89 3B84 74BE 65A0 9530 C7A4 9BD2

To claim this, I am signing this object:

@stevenkaras
stevenkaras / run_services.bash
Last active October 12, 2015 10:38
Local service instrumentation
#!/usr/bin/env bash
# This script runs services in the background, logs their output, and spins them down gently
#
# I use it in development, since I don't want to have databases and message brokers running
# all the time on my laptop.
#
# Reads a list of services either from ARGV or from a ".services" file
# Alternatively, can take a list of services (from either source) in label=command format
@stevenkaras
stevenkaras / gist:4506406
Created January 10, 2013 22:33
Snippet for rakefiles. When this task is provided on the command line, it will disable prerequisites for rake. You should ensure this snippet is at the end of your rakefile
task :skip_prereqs
if Rake.application.top_level_tasks.include?(:skip_prereqs)
Rake::Task.tasks.each do |task|
Rake::Task[task].clear_prerequisites
end
end
@stevenkaras
stevenkaras / .profile
Last active July 10, 2022 19:52
asdf POSIX bootstrap
case "$SHELL" in
*fish)
[ -f "$HOME/.asdf/asdf.fish" ] && . "$HOME/.asdf/asdf.fish"
;;
*zsh)
[ -f "$HOME/.asdf/asdf.sh" ] && . "$HOME/.asdf/asdf.sh"
;;
*bash)
if [ "${BASH:-no}" != no ]; then
[ -f "$HOME/.asdf/asdf.sh" ] && . "$HOME/.asdf/asdf.sh"