Skip to content

Instantly share code, notes, and snippets.

View vivien's full-sized avatar

Vivien Didelot vivien

  • Montréal
View GitHub Profile
@vivien
vivien / coderwall.rb
Created June 4, 2011 04:50
Simple and Stupid Ruby API for Coderwall.com
# Simple and Stupid Ruby API for Coderwall.com
# Vivien Didelot <vivien@didelot.org>
require "open-uri"
require "json"
module CoderWall
class Achievement
attr_reader :name, :badge, :description
@vivien
vivien / mail.rb
Created August 8, 2011 19:36
Sample script for the mail gem.
#!/usr/bin/env ruby
require "mail"
require "optparse"
imap_opts = {
:address => "imap.gmail.com",
:port => 993,
:enable_ssl => true
}
@vivien
vivien / insert_into.rb
Created August 10, 2011 23:00
Insert text after a given line of a file
def insert_into file, lineno, text
new_text = File.readlines(file).insert(lineno, text + "\n").join
File.open(file, 'w') { |f| f.write new_text }
end
@vivien
vivien / Rakefile
Created August 11, 2011 23:43
Passing arguments from one rake task to another
task :first, :arg1, :arg2 do |t, args|
puts "first"
Rake::Task[:second].invoke(args.arg1, args.arg2)
end
task :second, :arg1, :arg2 do |t, args|
puts "second"
puts args.arg1
puts args.arg2
end
@vivien
vivien / coderay_tokens_patch.rb
Created October 21, 2011 04:55
CodeRay: Iterate on each token with its kind, and its starting line number
require 'coderay'
class CodeRay::Tokens
def each_token
lineno = 1
self.each_slice(2) do |token, kind|
yield token, kind, lineno
lineno += token.count("\n") if token.is_a?(String)
end
end
@vivien
vivien / tld.rb
Created November 10, 2011 23:49
List of (ascii only) Internet top-level domains (TLD)
#!/usr/bin/env ruby
# An approximative Wikipedia/Mechanize version of:
# curl -s http://data.iana.org/TLD/tlds-alpha-by-domain.txt | grep -v XN | sed -e 1d -e 's/\(.*\)/\L\1/'
# Vivien Didelot <vivien@didelot.org>
require 'mechanize'
wikipedia_page = "http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains"
agent = Mechanize.new
From 2ac63f597648408c225ea43a447363f7b913a035 Mon Sep 17 00:00:00 2001
From: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Date: Wed, 6 Jun 2012 12:24:47 -0400
Subject: [PATCH] io: use system() if a command contains a pipe
Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
---
io.c | 4 ++++
1 file changed, 4 insertions(+)
@vivien
vivien / to_hash.rb
Last active December 14, 2015 01:39
Module to mixin into a DataMapper model to add a #to_hash method returning a hash with loaded properties.
module ToHash
def to_loaded_hash
fields = properties.select { |p| p.loaded?(self) }.map(&:name)
hash = {}
fields.each { |f| hash[f] = self[f] }
hash
end
end
@vivien
vivien / twitter.rb
Last active December 27, 2015 02:08
Fetch last few tweets, unauthenticated.
# Fetch last few tweets, unauthenticated.
#
# I just want the last few tweets of a user, I really don't want to do this:
# http://stackoverflow.com/a/15314662
require 'open-uri'
require 'cgi'
require 'openssl'
module Twitter
@vivien
vivien / i3-next-workspace.sh
Last active March 23, 2018 11:05
Jump to the next available workspace, on the current screen. If you use 1, 2 and 5, it'll jump to workspace 3.
#!/bin/bash
map_num () {
tr , '\n' | grep '"num":' | cut -d: -f2
# equivalent to jshon -a -e num
echo 11
}
next_min () {
sort -n | cat -n | awk '{ if ($1 != $2) { print $1; exit } }'