Skip to content

Instantly share code, notes, and snippets.

View jpmckinney's full-sized avatar

James McKinney jpmckinney

View GitHub Profile
@jpmckinney
jpmckinney / email_validator.rb
Created November 12, 2011 17:36
Email validation in Rails 3 or Active Model without regexp
# blog post: http://blog.slashpoundbang.com/post/12694519460/email-validation-in-rails-3-or-active-model-without
# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
# Domain must be present and have two or more parts.
def validate_each(record, attribute, value)
address = Mail::Address.new value
record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
end
end
@jpmckinney
jpmckinney / formatted_from_header.rb
Created November 12, 2011 17:58
How to safely create friendly From addresses including name and email
# blog post: http://blog.slashpoundbang.com/post/12695434343/how-to-safely-create-friendly-from-addresses-including
require 'mail'
# You already have +name+ and +email+ from the contact form.
address = Mail::Address.new email # john@example.com
address.display_name = name # John Doe
# Set the From or Reply-To header to the following:
address.format # John Doe <john@example.com>
@jpmckinney
jpmckinney / twitter-bootstrap-ie6.scss
Created November 14, 2011 23:31
SCSS additions to make Twitter Bootstrap work in IE6
// Assumes you are using <html> conditional classes as described here:
// http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
.lt-ie7 {
// blockquote small:before{content:'\2014 \00A0';}
blockquote small:before{content:"";}
// [class*="span"]{display:inline;float:left;margin-left:20px;}
.span1{display:inline;float:left;margin-left:20px;}
.span2{display:inline;float:left;margin-left:20px;}
.span3{display:inline;float:left;margin-left:20px;}
.span4{display:inline;float:left;margin-left:20px;}
@jpmckinney
jpmckinney / fingerprint.rb
Created November 17, 2011 21:46
Google Refine fingerprint clustering algorithm in Ruby
# blog post: http://blog.slashpoundbang.com/post/12938588984/google-refine-fingerprint-clustering-algorithm-in-ruby
# coding: utf-8
require 'unicode_utils/downcase'
class String
# Normalize spaces and fingerprint.
# http://code.google.com/p/google-refine/wiki/ClusteringInDepth
# http://code.google.com/p/google-refine/source/browse/trunk/main/src/com/google/refine/clustering/binning/FingerprintKeyer.java
def fingerprint
@jpmckinney
jpmckinney / remove-accents.rb
Created November 17, 2011 22:01
Remove all accents while preserving case
# blog post: http://blog.slashpoundbang.com/post/12938588984/remove-all-accents-and-diacritics-from-string-in-ruby
# coding: utf-8
string.tr(
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
@jpmckinney
jpmckinney / mixins.scss
Created December 31, 2011 20:06
CSS3 SCSS Mixins
// blog post: http://blog.slashpoundbang.com/post/15096433153/css3-scss-mixins
// Mixins ----------------------------------------------------------------------
// http://css3please.com/
@mixin background-rgba($red, $green, $blue, $opacity, $rgba) {
background-color: transparent;
background-color: rgba($red, $green, $blue, $opacity);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{$rgba}',endColorstr='#{$rgba}');
zoom: 1;
}
@jpmckinney
jpmckinney / pdftk-javac.patch
Created February 15, 2012 23:34
An incomplete patch to Pdftk to compile using javac, javah and jar instead of GCJ binaries.
diff --git a/java/Makefile b/java/Makefile
index a4e8ba6..bf627ef 100644
--- a/java/Makefile
+++ b/java/Makefile
@@ -27,8 +27,8 @@ else
endif
#
-export GCJFLAGS+= --encoding=UTF-8 --classpath="$(LIBGCJ):$(JAVALIBPATH):."
-export GCJHFLAGS+= --classpath="$(LIBGCJ):$(JAVALIBPATH):."
@jpmckinney
jpmckinney / proxy.php
Created March 19, 2012 18:34 — forked from evolvingweb/proxy.php
An AJAX-Solr proxy written in PHP.
<?php
/**
* @file
* Implements a Solr proxy.
*
* Currently requires json_decode which is bundled with PHP >= 5.2.0.
*
* You must download the SolrPhpClient and store it in the same directory as this file.
*
@jpmckinney
jpmckinney / wikipedia_api.rb
Created March 24, 2012 19:08
Wikipedia API
class Wiki
def self.parse(lang, page)
begin
json = Yajl::Parser.parse open("http://#{lang}.wikipedia.org/w/api.php?maxlag=5&redirects=1&format=json&disablepp=1&prop=text%7Cdisplaytitle%7Crevid&action=parse&section=0&page=#{CGI.escape(page)}").read
doc = Nokogiri::HTML(json['parse']['text']['*'])
# article message boxes
doc.css('.ambox').remove
doc.css('.bandeau').remove
doc.css('.bandeau-portail').remove
@jpmckinney
jpmckinney / scraperwiki_mock.rb
Created May 21, 2012 00:07
ScraperWiki mock
# blog post:
require 'scraperwiki'
require 'epoxy'
require 'sql_parser' # @todo Gem can't parse strings with escaped single quotes
require 'mongo'
module ScraperWiki
class << self
def dumpMessage(hash)