Skip to content

Instantly share code, notes, and snippets.

Troels Knak-Nielsen troelskn

Block or report user

Report or block troelskn

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View remove-image-background.sh
# 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
View application_controller.rb
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 Aug 29, 2015
identity_cache benchmark
View gist:fca41034ab3ebed4f507
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 Jul 3, 2014
IdentityCacheMiddleware for ThinkingSphinx
View identity_cache_middleware.rb
require 'thinking_sphinx/middlewares'
module ThinkingSphinx
module Middlewares
class IdentityCacheMiddleware < ActiveRecordTranslator
def call(contexts)
contexts.each do |context|
Inner.new(context).call
end
View geoip.rb
require 'ipaddr'
require 'net/http'
require 'uri'
require 'json'
module GeoIP
def self.configuration
@configuration ||= {}
end
@troelskn
troelskn / Rakefile
Created Sep 26, 2014
Simple deployment via ftp
View Rakefile
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
View keybase.md

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:

View simple_xls_writer.php
<?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";
View attr_initializer.rb
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 = {})
View install_gems
#!/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
You can’t perform that action at this time.