Skip to content

Instantly share code, notes, and snippets.

View slemiere's full-sized avatar

Sylvain Lemiere slemiere

View GitHub Profile
@slemiere
slemiere / gist:7abd9475874e77010b50
Created December 17, 2014 20:17
redis nice slow log
r = Redis.new(url: 'redis://foo:bar@foo.com:123')
queries = r.slowlog('get', 1000)
puts queries.map{|q| "#{Time.at(q[1])} - #{q[2]/1000}ms - #{q[4].first} : #{q[4][1..-1]}"}.join("\n")
require 'httparty'
class Wink
include HTTParty
CLIENT_ID = 'CHANGE_ME'
CLIENT_SECRET = 'CHANGE_ME'
base_uri 'https://winkapi.quirky.com'
@slemiere
slemiere / gist:a59fdbfadbd3726eb622
Last active May 18, 2016 01:39
Blink + PagerDuty
#!/usr/bin/env ruby
require 'httparty'
require 'blink1'
class PagerDuty
include HTTParty
TOKEN = "YOUR_PAGERDUTY_API_TOKEN"
PAGERDUTY_DOMAIN = "YOUR_PAGERDUTY_DOMAIN"
base_uri "https://#{PAGERDUTY_DOMAIN}.pagerduty.com/api/v1"
### Keybase proof
I hereby claim:
* I am slemiere on github.
* I am slemiere (https://keybase.io/slemiere) on keybase.
* I have a public key whose fingerprint is 2B47 E503 DCF4 124B 9DB8 214D 12F1 E851 633A F5BD
To claim this, I am signing this object:
@slemiere
slemiere / yield.rb
Created April 18, 2013 15:46
yield vs block.call
define_method("hello_yield") { yield }
define_method("hello_block_call") { |&block| block.call}
hello_yield { puts "hello Yield"}
# LocalJumpError: no block given (yield)
hello_block_call { puts "Hello Block Call"}
# Hello Block Call
module Mongoid
module Document
def instantiate(attrs = nil, criteria_instance_id = nil)
attributes = attrs || {}
doc = allocate
doc.criteria_instance_id = criteria_instance_id
doc.instance_variable_set(:@attributes, attributes)
doc.apply_defaults
IdentityMap.set(doc) if identity_map_set_possible?(criteria_instance_id)
doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty?
@slemiere
slemiere / syntax.rb
Created April 17, 2012 15:46
redis queues syntax
all_queues = ["test", "schedule", "process"]
QUEUES = "*"
# ["test", "schedule", "process"]
QUEUES = "test,schedule"
# ["test", "schedule"]
QUEUES = "*~test"
# ["schedule", "process"]
(function(){function Asteroids(){function Vector(x,y){if(typeof x=='Object'){this.x=x.x;this.y=x.y;}else{this.x=x;this.y=y;}};Vector.prototype={cp:function(){return new Vector(this.x,this.y);},mul:function(factor){this.x*=factor;this.y*=factor;return this;},mulNew:function(factor){return new Vector(this.x*factor,this.y*factor);},add:function(vec){this.x+=vec.x;this.y+=vec.y;return this;},addNew:function(vec){return new Vector(this.x+vec.x,this.y+vec.y);},sub:function(vec){this.x-=vec.x;this.y-=vec.y;return this;},subNew:function(vec){return new Vector(this.x-vec.x,this.y-vec.y);},rotate:function(angle){var x=this.x,y=this.y;this.x=x*Math.cos(angle)-Math.sin(angle)*y;this.y=x*Math.sin(angle)+Math.cos(angle)*y;return this;},rotateNew:function(angle){return this.cp().rotate(angle);},setAngle:function(angle){var l=this.len();this.x=Math.cos(angle)*l;this.y=Math.sin(angle)*l;return this;},setAngleNew:function(angle){return this.cp().setAngle(angle);},setLength:function(length){var l=this.len();if(l)this.mul(lengt
@slemiere
slemiere / causes.rb
Created September 1, 2011 19:23
Causes Puzzle
require 'text'
def compute_friends(word, words)
words.reject{|potential_friend| Text::Levenshtein.distance(word, potential_friend) > 1}
end
def naive_word_social_network(word, non_friend_words)
last_social_network_step = []
new_friends = [word]
current_social_network_step = [word]
# written by Pradeep Gatram, www.masplantiz.com
# no restrictions whatsoever on usage.
require 'rubygems'
require 'mq'
EM.run do
def replicate source, destination, topic
source_q = MQ::Queue.new(source, "replicator for #{topic}")
source_q.bind(MQ::Exchange.new(source, :topic, topic))