Skip to content

Instantly share code, notes, and snippets.

View troelskn's full-sized avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
# Requires ImageMagick
# Converting the source from JPEG to PNG - if necessary
convert my_src_image.jpg my_src_image.png
# Option A
# - Requires a temporary intermediate file
# - Drill more than 10 might result in poor results
@troelskn
troelskn / application_controller.rb
Created March 26, 2014 09:56
Rails preferred_layout
class ApplicationController < ActionController::Base
layout :conditionally_select_layout
# Like `layout`, but allows conditional overriding in app controller
def self.preferred_layout(value = nil)
@preferred_layout = value unless value.nil?
@preferred_layout
end
private
@troelskn
troelskn / gist:fca41034ab3ebed4f507
Last active August 29, 2015 14:03
identity_cache benchmark
I benchmarked identity_cache with memcached vs. default active_record against mysql.
Setup is running on AWS, same availability zone. I used RDS (Multi AZ setup) for MySql and ElastiCache for memcached. The test server is on EC2. Instance types are m3.large for MySql, m3.xlarge for EC2 and r3.large for memcached.
The database is a copy of our production db. The products table holds about 1.4M entries.
The cache has been pre-heated with all 1.4M entries prior to the test.
During the test, I ran 10000 fetches:
@troelskn
troelskn / identity_cache_middleware.rb
Created July 3, 2014 11:43
IdentityCacheMiddleware for ThinkingSphinx
require 'thinking_sphinx/middlewares'
module ThinkingSphinx
module Middlewares
class IdentityCacheMiddleware < ActiveRecordTranslator
def call(contexts)
contexts.each do |context|
Inner.new(context).call
end
require 'ipaddr'
require 'net/http'
require 'uri'
require 'json'
module GeoIP
def self.configuration
@configuration ||= {}
end
@troelskn
troelskn / Rakefile
Created September 26, 2014 09:49
Simple deployment via ftp
require 'rubygems'
require 'rake'
require 'ostruct'
require 'fileutils'
module Ftp
def self.invoke(config = {}, &block)
commands = []
commands << "set ftp:list-options -a"
commands << "set ftp:ssl-allow no" if config.disable_ssl

Keybase proof

I hereby claim:

  • I am troelskn on github.
  • I am troelskn (https://keybase.io/troelskn) on keybase.
  • I have a public key whose fingerprint is FCE4 73F5 52AA C2B0 B73D FEC2 680B 1145 00F1 D09E

To claim this, I am signing this object:

<?php
/**
* Write with file extension xls and you should be fine.
* This format is called SpreadsheetML
*/
class SimpleXlsWriter {
protected $filename;
protected $handle;
protected $title = "No Title";
protected $author = "No Author";
@troelskn
troelskn / attr_initializer.rb
Created June 3, 2009 15:05
attr_initializer
class Class
# Adds a initialize method, which takes attributes as a hash
# Optionally call with symbol-names, to additionally create attribute accessors with `attr_accessor`
def attr_initializer(*args)
attr_accessor *args unless args.empty?
module_eval <<-END
def initialize(attributes = {})
initialize_attributes(attributes)
end
def initialize_attributes(attributes = {})
#!/usr/bin/env ruby
def parse_missing_required(input)
matches = input.match(/Missing these required gems:([\s\S]*)You're running:/)
matches[1].strip.split("\n").map do |line|
m = line.match(/^\s*(\S+)\s+(\S+\s+[0-9.]+)/)
p line if m.nil?
{:name => m[1], :version => m[2]}
end
end