Skip to content

Instantly share code, notes, and snippets.

View ekampf's full-sized avatar

Eran Kampf ekampf

View GitHub Profile
@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 / resource_route.py
Last active December 17, 2015 18:39
webapp2.py (Google AppEgnine) Resource Route to describe RESTful resources
from webapp2 import Route
from webapp2_extras.routes import MultiRoute
from inflection import singularize, pluralize
__author__ = 'ekampf'
# pylint:disable=C0326,R0902
class ResourceRoute(MultiRoute):
@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:13916578123fedb60977
Last active August 29, 2015 14:22
Python NotificationsManager and observer
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class observes(object):
def __init__(self, name):
@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