Skip to content

Instantly share code, notes, and snippets.

View pierot's full-sized avatar
🏠
Working from home

Pieter Michels pierot

🏠
Working from home
View GitHub Profile
validates_presence_of :first_name, :last_name, :email
validates_uniqueness_of :email, :case_sensitive => false
validates_format_of :email, :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates_length_of :first_name, :last_name, :minimum => 2, :maximum => 50
@pierot
pierot / validation_reflection_2.rb
Created January 22, 2011 18:25
Rails 3 / 1-line validation
validates :first_name, :presence => true, :length => { :minimum => 2, :maximum => 50 }
@pierot
pierot / makro_nokogiri.rb
Created January 23, 2011 14:00
Makro Resto + Fuel scraping
require 'sinatra'
require 'builder'
require 'nokogiri'
require 'uri'
require 'open-uri'
$: << File.join(File.dirname(__FILE__), '..', 'app')
configure do
set :root, File.join(File.dirname(__FILE__), '..')
@pierot
pierot / makro_scraper.js
Created January 23, 2011 20:19
Makro NodeJS scraping
var request = require('request'), http = require('http'), $ = require('jquery'), querystring = require('querystring');
http.createServer(function (req, res) {
var q = querystring.parse(req.url);
var locations = {'deurne': 0, 'machelen': 1, 'alleur': 2, 'eke': 3, 'sint-pieters-leeuw': 4};
request({uri: 'http://www.makro.be/Content/assortiment/benzinestation/benzineprijzen/1/index.jsp?stat=' + locations[q.l]}, function (error, response, body) {
res.writeHead(200, {'Content-Type': 'text/xml'});
var fuel_types = ['diesel', 'eurosuper', 'superplus'];
@pierot
pierot / model_relation.rb
Created January 25, 2011 08:33
Rails 3 accepts_nested_attributes_for
class Album < ActiveRecord::Base
has_many :content, :dependent => :destroy
accepts_nested_attributes_for :content, :allow_destroy => true
end
class Content < ActiveRecord::Base
belongs_to :album
end
@pierot
pierot / model_relation_good.rb
Created January 25, 2011 08:35
Rails 3 accepts_nested_attributes_for + attr_accessible
class Album < ActiveRecord::Base
has_many :content, :dependent => :destroy
accepts_nested_attributes_for :content, :allow_destroy => true
attr_accessible :content_attributes
end
@pierot
pierot / capistrano_env.rb
Created February 1, 2011 13:48
Capistrano snippet to set Rails.env
set :rails_env, "test"
set :rack_env, rails_env
task :setup_env do
run "RACK_ENV=#{rails_env}"
run "RAILS_ENV=#{rails_env}"
run "echo 'RackEnv #{rails_env}' >> #{File.join(current_path, '.htaccess')}"
run "echo 'RailsEnv #{rails_env}' >> #{File.join(current_path, '.htaccess')}"
end
@pierot
pierot / capistrano_protect.rb
Created February 1, 2011 13:51
Capistrano snippet to secure deployment environment
set :stages, %w(testing staging production)
set :default_stage, "testing"
require 'capistrano/ext/multistage'
task :protect do
# http://www.htaccesstools.com/htaccess-authentication/
run "echo 'username:$apr1$gT8Ap...$3f8RadFS7xottW8AYVX5b0' >> #{File.join(current_path, '.htpasswd')}"
run "echo 'AuthType Basic' >> #{File.join(current_path, '.htaccess')}"
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.makro.be/public/site/be/node/65832/Lnl_BE/index.html"))
doc.css('#content .Box div.Table table tr:last-child').each do |tr|
p tr.html
end