Skip to content

Instantly share code, notes, and snippets.

View adamcrown's full-sized avatar

Adam Crownoble adamcrown

View GitHub Profile
@adamcrown
adamcrown / gist:662782
Created November 4, 2010 17:11
Minecraft server init file
We couldn’t find that file to show.
@adamcrown
adamcrown / .irbrc
Last active November 7, 2023 06:34
My Bundler proof .irbrc file including wirble, awesome_print, hirb, console logging and route helpers
require 'rubygems' unless defined? Gem # rubygems is only needed in 1.8
def unbundled_require(gem)
loaded = false
if defined?(::Bundler)
Gem.path.each do |gems_path|
gem_path = Dir.glob("#{gems_path}/gems/#{gem}*").last
unless gem_path.nil?
$LOAD_PATH << "#{gem_path}/lib"
@adamcrown
adamcrown / time_string_validator.rb
Created May 8, 2012 23:40
Rails validator for time strings
class TimeStringValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
require 'chronic'
record.errors[attribute] << "doesn't seem to be a valid time" unless Chronic.parse(value)
end
end
@adamcrown
adamcrown / uri_exists_validator.rb
Created May 8, 2012 23:42
Rails validator for URI existence
# Original credits: http://blog.inquirylabs.com/2006/04/13/simple-uri-validation/
# HTTP Codes: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html
require 'net/http'
class UriExistsValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin # check header response
url = URI.parse(value)
request = Net::HTTP.new(url.host, url.port)
response = request.request_head(url.path.empty? ? '/' : url.path)
@adamcrown
adamcrown / absolute_uri_validator.rb
Created May 10, 2012 19:17
Absolute URI validator
require 'uri'
class AbsoluteUriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
unless URI.parse(value).absolute?
record.errors[attribute] << 'is not an absolute URL'
end
rescue URI::InvalidURIError => error
record.errors[attribute] << 'is not a valid URL'
@adamcrown
adamcrown / relative_uri_validator.rb
Created May 10, 2012 19:19
Relative URI Validator
require 'uri'
class RelativeUriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
unless URI.parse(value).relative?
record.errors[attribute] << 'is not a relative URL'
end
rescue URI::InvalidURIError => error
record.errors[attribute] << 'is not a valid URL'
@adamcrown
adamcrown / validation_helpers.rb
Created May 10, 2012 19:24
RSpec Rails Validation Helper
# Examples:
# test_valid_attribute Link, :url => 'http://example.com'
# test_invalid_attribute Link, :url => 'OH NOES!!!1!'
module ValidationHelpers
def test_valid_attribute(model, attr_val_hash)
test_attribute(:valid, model, attr_val_hash)
end
def test_invalid_attribute(model, attr_val_hash)
@adamcrown
adamcrown / printing_from_an_oracle_function.sql
Created June 28, 2012 00:19
Printing the results of an Oracle function
-- Must me NUMBER or CHAR if you want to print it. Printing a VARCHAR is too complex for expensive databases like Oracle.
VARIABLE r CHAR (1000);
BEGIN
-- := is for assignment. vs = for equality
-- bind variables are prefixed with a colon
-- BANIST1 is the instance
-- BSK_INFO_REQUEST is the package
-- get_prel_codes() is the function
:r := BANINST1.BSK_INFO_REQUEST.get_prel_codes();
@adamcrown
adamcrown / white_pages-template.html
Created January 5, 2013 01:18
A simple Liquid template for "White Pages" reports with the applyfor app
<html>
<head>
<title>RA White Pages</title>
<style type="text/css">
* {
font-family: Arial, sans-serif;
font-size: 10pt;
}
@adamcrown
adamcrown / active_record_sessions_batch_delete.rb
Last active December 11, 2015 00:18
Deleting a large batch of session records can really overload the DB (at least MySQL). This script paces it out a bit.
def batch_session_prune(keep_after = 1.month.ago, batch_by = 1.day, pause_time = 10)
time = ActiveRecord::SessionStore::Session.order(:updated_at).first[:updated_at]
while time < keep_after
time = time + batch_by
puts "Deleting sessions from #{time.to_s(:short)}"
ActiveRecord::SessionStore::Session.where('updated_at < ?', time).delete_all
sleep pause_time