Skip to content

Instantly share code, notes, and snippets.

View remvee's full-sized avatar

Remco van 't Veer remvee

  • 52°N, 5°E
View GitHub Profile
@remvee
remvee / images.clj
Created September 15, 2010 08:47
basic clojure image operations using awt
(ns portfolio.images)
(defn scale [image [width height]]
(.getScaledInstance image width height java.awt.Image/SCALE_SMOOTH))
(defn crop [image [x y] [width height]]
(.getSubimage image x y width height))
(defn dimensions [image]
[(.getWidth image) (.getHeight image)])
@remvee
remvee / ignore_unknown_http_method.rb
Created September 28, 2010 07:39
silence ActionController::UnknownHttpMethod exceptions
# Avoid annoying ActionController::UnknownHttpMethod exceptions like:
#
# ActionController::UnknownHttpMethod) "CONNECT, accepted HTTP methods are get, head, put, post, delete, and options"
#
# Install this file in app/metal and these requests will receive a 405
# "Method Not Allowed" status and will be logged under `info'.
class IgnoreUnknownHttpMethod
def self.call(env)
[
if ActionController::Request::HTTP_METHODS.include?(env["REQUEST_METHOD"].downcase)
@remvee
remvee / web.clj
Created September 28, 2010 14:49
Ring middleware to force a SSL connection
(defn wrap-force-ssl [app]
(fn [req]
(if (= :https (:scheme req))
(app req)
(let [url (str "https://" (:server-name req) (:uri req))]
{:status 302
:headers {"Location" url, "Content-Type" "text/html"}
:body (str "<html><body><a href='" url "'>redirecting..</a.></body></html>")}))))
@remvee
remvee / util.clj
Created October 6, 2010 07:05
Ring middleware to add Last-Modified header
(defn rfc1123-date-format []
(doto (java.text.SimpleDateFormat. "EEE, dd MMM yyyy HH:mm:ss z")
(.setTimeZone (java.util.TimeZone/getTimeZone "UTC"))))
(defn wrap-if-modified-since [app mtime-fn]
(fn [req]
(let [mtime (mtime-fn req)
msince (if-let [val (get-in req [:headers "if-modified-since"])]
(.parse (rfc1123-date-format) val))]
(if mtime
@remvee
remvee / *scratch*
Created December 27, 2010 12:51
Workaround hanging SSL connections in emacs on ubuntu
(setq tls-program '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof"))
@remvee
remvee / levenshtein.clj
Created December 31, 2010 13:51
Calculate levenshtein distance between two sequences.
(defn levenshtein
"Calculate levenshtein distance between two sequences."
{:test #(are [d s1 s2] (= d (levenshtein s1 s2))
0 "soup" "soup"
3 "kitten" "sitting"
3 "sitting" "kitten"
0 [1 2 3] [1 2 3]
1 [1 2 3] [1 2 3 4])}
[s1 s2]
(((reduce (fn [d [i j]]
@remvee
remvee / stripped_attributes.rb
Created January 5, 2011 13:13
strip_value_of ActiveRecord column helper
class ActiveRecord::Base
class << self
attr_writer :stripped_attributes
def stripped_attributes
@stripped_attributes ||= []
end
def strip_value_of(*attrs)
self.stripped_attributes += attrs
@remvee
remvee / attachment.rb
Created January 7, 2011 14:54
Make a file extension to mime-type map from /etc/mime.types
File.read("/etc/mime.types").split("\n").reject{|l| l =~ /^\s*#/}.reject(&:blank?).reduce({}){|m,l| t, *x = l.split(/\s+/); x.each{|k| m[k] = t}; m}
@remvee
remvee / validations.clj
Created January 16, 2011 16:39
Basic validation framework.
(ns #^{:author "Remco van 't Veer"
:doc
"Basic validation framework for maps. The constructed validator
functions take two arguments the before- and after state of the map.
They return a map of errors where keys are, typically, keys of the
input map and values are lists of keyworded errors or maps of keyword
errors to some relevant argument. For example:
((validator (not-blank :nr)
@remvee
remvee / classify_check_box.rb
Created January 17, 2011 10:49
Add checkbox class to checkboxes rendered by ActionView::Helpers::FormHelper
# Automatically add checkbox class to check_box form elements to allow
# styling them separately in older versions of IE.
module ActionView::Helpers::FormHelper
def check_box_with_class(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
check_box_without_class(object_name, method, {:class => "checkbox"}.merge(options), checked_value, unchecked_value)
end
alias_method_chain :check_box, :class
end