Skip to content

Instantly share code, notes, and snippets.

Avatar

Eran Kampf ekampf

View GitHub Profile
@ekampf
ekampf / gist:772597
Created Jan 10, 2011
Show the current Git branch in the command line prompt
View gist:772597
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\$ "
@ekampf
ekampf / RssResult.cs
Created Jan 16, 2011
ASp.NET MVC action result that renders an RSS
View RssResult.cs
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:847741
Created Feb 28, 2011
A ruby snippet to sanitize Html (and specifically Microsoft Word's messy HTML) (based on https://gist.github.com/139987)
View gist:847741
# 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 / gist:2000771
Created Mar 8, 2012
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...
View gist:2000771
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 / initializer.rb
Created Mar 21, 2012
How to limit access to a certain mountable engine
View initializer.rb
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 / created_at_scopes.rb
Last active Dec 12, 2015
Add scopes to query by created_at\updated_at to models
View created_at_scopes.rb
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 / gist:7721226
Created Nov 30, 2013
Code to convert temperature from Arduino's temperature sensor into heatmap RGB
View gist:7721226
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 / setup_kafka.sh
Last active Nov 29, 2017
Setup Kafka (0.8.0) on OSX
View setup_kafka.sh
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
View fix_time_with_zone.rb
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 / nested_dict.py
Last active Aug 29, 2015
Python Nested Dictionary
View nested_dict.py
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)
You can’t perform that action at this time.