Skip to content

Instantly share code, notes, and snippets.

View hakanensari's full-sized avatar

Hakan Ensari hakanensari

  • Amsterdam
  • 23:04 (UTC +02:00)
View GitHub Profile
@hakanensari
hakanensari / honeybadger.rb
Created January 26, 2013 23:14
Honeybadger initializer
Honeybadger.configure do |config|
config.api_key = '12345678'
config.ignore.push "SignalException::SIGTERM",
"Excon::Errors::ServiceUnavailable",
"Excon::Errors::SocketError",
"Excon::Errors::Timeout"
config.async do |notice|
WorkingBadger.perform_async(notice.to_json) unless notice.ignore?
require 'capybara/dsl'
require 'capybara-webkit'
require 'headless'
Capybara.default_driver = :webkit
Capybara.run_server = false
class Page
include Capybara::DSL
require 'celluloid'
require 'excon'
module Excon
class Connection
def prequest(ary)
futures = ary.map do |params|
future.request(params)
end
@hakanensari
hakanensari / gist:4194831
Created December 3, 2012 12:49
A git commit-msg hook that extracts ticket number from branch name and appends it to the commit message
#!/usr/bin/env ruby
# A minimal commit message hook that extracts a ticket number from the branch
# name and appends it to the commit message.
#
# The commit message remains unchanged if the branch name doesn't include a
# ticket number or the message already includes a reference to it.
#
# Let's assume you have a story that outlines that you want to deliver foo
# value. You start by checking out a branch:
#
@hakanensari
hakanensari / gist:3937759
Created October 23, 2012 09:03
Can't instance_exec curried proc in JRuby?
foo = lambda { |x, y| x + y }
instance_exec 1, 2, &foo # => 3
bar = foo.curry.call(1)
instance_exec 2, &bar # NameError: undefined local variable or method `arity' for main:Object
@hakanensari
hakanensari / benchmark.rb
Created October 3, 2012 18:47
PostgreSQL update strategies in and around Rails
require 'active_record'
require 'activerecord-import'
require 'benchmark'
require 'pg'
include ActiveRecord
Base.establish_connection adapter: 'postgresql',
encoding: 'unicode',
pool: 5,
@hakanensari
hakanensari / gist:3493494
Created August 27, 2012 23:49
How to build a testable scraper

Separate concerns. You should probably encapsulate the following responsibilities in separate classes:

  1. Get a URL.
  2. Parse the response body.
  3. Build a Hash or PORO that represents the payload.

You're not hitting an API. Be prepared to fail. Use an HTTP library like Excon that handles retries. Don't stub when testing. Your tests should not hide changes in the external URL.

Keep business logic out of your parsers. When testing them, simply assert if the parsing methods return something.

@hakanensari
hakanensari / 01-vagrant.sh
Created August 2, 2012 16:03
Set up an Ubuntu 12.04 Precise box with Vagrant for Ruby and NodeJS development
gem install vagrant
vagrant box add precise64 http://files.vagrantup.com/precise64.box
vagrant init
vagrant up
@hakanensari
hakanensari / to_curly
Created August 2, 2012 12:04
Convert do..end blocks to curly braces
:s/do[\n ]\+\(.*\)[\n ]\+end/{ \1 }/
@hakanensari
hakanensari / 01_stream.rb
Created July 12, 2012 08:29
Stream HTTP through an IO pipe
require 'excon'
def get(url)
rd, wr = IO.pipe
Thread.new {
Excon.get url, response_block: ->(chunk, remaining, total) {
wr << chunk
wr.close if remaining == 0
}