Skip to content

Instantly share code, notes, and snippets.

View timonv's full-sized avatar
🚀

Timon Vonk timonv

🚀
View GitHub Profile
@timonv
timonv / Postgresql search hstore values
Last active August 29, 2015 14:20
Postgresql search hstore values
-- 0. Create a bunch of id => vector results for every definition
WITH unnested_keys_as_vector AS (
SELECT id as term_id, to_tsvector((each(definitions)).value) as term
FROM glossary_terms
WHERE glossary_id = 10
)
-- 2. Select the rank so we can sort by it (default order is order of terms in table)
SELECT *, ts_rank_cd(term, to_tsquery('I | like | bananas')) as rank
FROM glossary_terms
-- 1. Join the result of the tsvector search on the terms
@timonv
timonv / dispatcher.rs
Last active August 29, 2015 14:20
Dispatcher
use self::DispatchType::{ChangeCurrentChannel, OutgoingMessage, IncomingMessage};
use std::collections::HashMap;
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
#[derive(PartialEq, Debug, Clone)]
enum DispatchType {
ChangeCurrentChannel,
OutgoingMessage,
IncomingMessage
@timonv
timonv / object.rb
Last active August 29, 2015 14:19
Easily measure methods with Appsignal
class Object
def self.measure(method, category_name=nil)
alias_method "#{method}_unmeasured", method
define_method method do |*args|
cat_name = if category_name.respond_to?(:call)
# If proc eval in current context and pass args
self.instance_exec args, &category_name
elsif category_name && category_name.respond_to?(:to_s)
# Stringify that thing you gave me
category_name.to_s
fn request_temp_code() -> TempCode {
Command::new("xdg-open").arg(format!("https://slack.com/oauth/authorize?client_id={}", SLACK_CLIENT_ID)).output().unwrap();
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 9999);
let (tx, rx) = channel();
let mtx = Mutex::new(tx);
let mut guard = server.listen(move |req: Request, res: Response| {
match req.uri {
AbsolutePath(path) => {
fn main() {
// Command::new("xdg-open").arg("https://slack.com/oauth/authorize?client_id=2334733471.3592055147").output().unwrap();
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 9999);
let (tx, rx) = channel();
// tx.send("Test".to_string());
// rx.recv();
let mut _guard = server.listen(move |req: Request, res: Response| {
match req.uri {
AbsolutePath(path) => {
use std::io::{TcpStream, BufferedWriter};
use std::io::Stream;
use std::sync::{RWLock, Arc};
use std::collections::HashMap;
pub struct ChatServer<T: 'static + Reader + Writer> {
users: Arc<RWLock<HashMap<String, T>>>
}
// Generics are kinda viral...
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
@timonv
timonv / application.rb
Last active December 17, 2015 14:49
Show UserID in log files with UUID
# Hack around the middleware stack a bit to get warden session correctly in
config.middleware.delete(ActionDispatch::Cookies)
config.middleware.delete(ActionDispatch::Session::CookieStore)
config.middleware.insert_before(Rails::Rack::Logger, ActionDispatch::Session::CookieStore)
config.middleware.insert_before(ActionDispatch::Session::CookieStore, ActionDispatch::Cookies)
# Rotate two files, max 5 mb each
config.logger = ActiveSupport::TaggedLogging.new(Logger.new(config.paths['log'].first, 2, 5 * 1024 * 1024))
config.log_tags = [
@timonv
timonv / test.rb
Created April 9, 2013 13:03
Private class methods in Ruby
class Person
def self.get_name
persons_name
end
def self.persons_name
"Sam"
end
private_class_method :persons_name
class Person
def self.get_name
persons_name
end
def self.persons_name
"Sam"
end
private_class_method :persons_name