This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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>")})))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(setq tls-program '("openssl s_client -connect %h:%p -no_ssl2 -ign_eof")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
class << self | |
attr_writer :stripped_attributes | |
def stripped_attributes | |
@stripped_attributes ||= [] | |
end | |
def strip_value_of(*attrs) | |
self.stripped_attributes += attrs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |