Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
#!/usr/bin/env ruby
# SweDB XMLTV Grabber for EyeTV
# by Henrik Nyh <http://henrik.nyh.se> under the MIT License.
#
# INSTALLATION
#
# Configure the list of channels below and run this script as a cron job, e.g.
#
# 0 */12 * * * ruby /path/to/this/script.rb
require 'net/http'
require 'digest/md5'
# Is there a Gravatar for this email?
def gravatar?(email)
hash = Digest::MD5.hexdigest(email.to_s.downcase)
response = Net::HTTP.new('www.gravatar.com', 80).request_head("/avatar/#{hash}?size=1&default=.")
response.code != '302'
end
@henrik
henrik / Ensure Image Extension.scpt
Created November 21, 2008 18:52
Fix missing/incorrect image file extension on download. See http://henrik.nyh.se/2008/11/extension-action for details.
on adding folder items to myFolder after receiving myFiles
repeat with myFile in myFiles
set myPath to (POSIX path of myFile)
set myType to do shell script "file --mime-type -br " & (quoted form of myPath)
if myType is "image/jpeg" and myPath does not end with ".jpg" and myPath does not end with ".jpeg" then
set newExtension to ".jpg"
else if myType is "image/gif" and myPath does not end with ".gif" then
set newExtension to ".gif"
@henrik
henrik / unsimple_format.rb
Created November 28, 2008 09:04
Like Rails' simple_format but avoids <p>/<br /> around some block tags.
# From Rails
def simple_format(text, html_options={})
start_tag = '<p>'
text = text.to_s.dup
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
text.insert 0, start_tag
text << "</p>"
end
@henrik
henrik / init.rb
Created November 28, 2008 13:42
Simple Settings object for Merb.
Merb::BootLoader.before_app_loads do
# This will get executed after dependencies have been loaded but before your app's classes have loaded.
require 'config/settings'
end
@henrik
henrik / .bashrc
Created December 3, 2008 17:56
Git branch and dirty state in Bash prompt.
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
@henrik
henrik / rails_ext.rb
Created December 5, 2008 11:32
Make Rails' parameterize remove apostrophes.
module ActiveSupport::Inflector
# Parameterize "Foo's and Charles's bar" into "foos-and-charles-bar"
# instead of into "foo-s-and-charles-s-bar".
def parameterize_with_apostrophe_removal(string, sep = '-')
string = string.dup
string.gsub!(%r{s's\b}i, 's')
string.gsub!(%{'}, "")
parameterize_without_apostrophe_removal(string, sep)
end
#!/bin/bash
ssh hyper "osascript -e 'tell app \"iTunes\" to back track'"
@henrik
henrik / .irbrc
Created December 23, 2008 23:10
# http://pablotron.org/software/wirble/
# Provides "ri", history survives restart etc
require 'rubygems'
require 'wirble'
Wirble.init
# http://www.quotedprintable.com/2007/9/13/my-irbrc
# Show rails root in script/console prompt
require 'irb/completion'
# Make Apache with enabled mod_proxy forward http://ak.dev/foo
# to http://ak.dev:3000/foo for use in local Rails development.
<VirtualHost *:80>
ServerName ak.dev
RewriteEngine on
RewriteRule /(.*) http://localhost:3000/$1 [P]
</VirtualHost>