Skip to content

Instantly share code, notes, and snippets.

View ekampf's full-sized avatar

Eran Kampf ekampf

View GitHub Profile
@ekampf
ekampf / nested_dict.py
Last active August 29, 2015 14:01
Python Nested Dictionary
class Nil(object):
def __getitem__(self, key):
return Nil()
def __nonzero__(self):
return False
class NestedDict(dict):
def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)
module ActiveSupport
class TimeWithZone
alias_method :to_int, :to_i # For some reason this is requires after upgrading to ruby 1.9.3-p429 - https://github.com/rails/rails/pull/10686
end
end
@ekampf
ekampf / setup_kafka.sh
Last active November 29, 2017 17:41
Setup Kafka (0.8.0) on OSX
brew install sbt
cd /tmp
wget http://apache.spd.co.il/kafka/0.8.0/kafka_2.8.0-0.8.0.tar.gz
tar -zxvf kafka_2.8.0-0.8.0.tar.gz -C /usr/local/
cd /usr/local/kafka_2.8.0-0.8.0
sbt update
sbt package
@ekampf
ekampf / gist:7721226
Created November 30, 2013 16:39
Code to convert temperature from Arduino's temperature sensor into heatmap RGB
const int rLED = 11;
const int gLED = 9;
const int bLED = 10;
const int sensor = A0;
const float minTemp = 10.0;
const float maxTemp = 50.0;
int redV = 0;
int greenV = 0;
@ekampf
ekampf / created_at_scopes.rb
Last active December 12, 2015 09:39
Add scopes to query by created_at\updated_at to models
module Concerns
module CreatedAtScopes
extend ActiveSupport::Concern
included do
%w(created_at updated_at).each do |prop|
verb = prop.gsub('_at', '')
scope "#{verb}_last_h", lambda { where("#{prop} >= ?", (Time.now-1.hour).to_s(:db)) }
scope "#{verb}_last_6h", lambda { where("#{prop} >= ?", (Time.now-6.hour).to_s(:db)) }
@ekampf
ekampf / initializer.rb
Created March 21, 2012 12:55
How to limit access to a certain mountable engine
class CanAccessSomeGem
def self.matches?(request)
current_user = request.env['warden'].user
return false if current_user.blank?
return < some condition on current_user >
end
end
@ekampf
ekampf / gist:2000771
Created March 8, 2012 12:23
Basically, the has_many association controls how the "join model" (Subscription) is created and the has_many_through association controls how the :through model is created. So instead of having one has_many :subscriptions I split it to two...
has_many :subscriptions_members,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::MEMBER},
:dependent => :destroy
has_many :subscriptions_followers,
:class_name => "Subscription",
:conditions => {:kind => Subscription::SubscriptionKinds::FOLLOWER},
@ekampf
ekampf / gist:847741
Created February 28, 2011 18:13
A ruby snippet to sanitize Html (and specifically Microsoft Word's messy HTML) (based on https://gist.github.com/139987)
# This function cleans up messy HTML that was pasted by a user to a WYSIWYG editor.
# Specifically it also handles messy Word\Outlook generated HTML while keeping its original formattings.
require 'rubygems'
require 'sanitize'
def clean_up_document(html)
elements = %w[p b h1 h2 h3 h4 h5 h6 strong li ul ol i br div pre p]
attributes = {
'a' => ['href', 'title'],
@ekampf
ekampf / RssResult.cs
Created January 16, 2011 12:55
ASp.NET MVC action result that renders an RSS
public class RssResult : FileResult
{
private readonly SyndicationFeed _feed;
/// <summary>
/// Creates a new instance of RssResult
/// </summary>
/// <param name="feed">The feed to return the user.</param>
public RssResult(SyndicationFeed feed)
: base("application/rss+xml")
@ekampf
ekampf / gist:772597
Created January 10, 2011 09:56
Show the current Git branch in the command line prompt
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOUR="\[\033[0m\]"
PS1="$GREEN\u@machine$NO_COLOUR:\w$YELLOW\$(parse_git_branch)$NO_COLOUR\$ "