Skip to content

Instantly share code, notes, and snippets.

@rafops
rafops / traverse.rb
Created February 23, 2012 14:19
Traversing a Ruby Hash
class Hash
def traverse *args
args.inject(self) { |h,k| h = h[k] }
end
end
h = { a: { b: { c: "foobar" } } }
h.traverse :a, :b, :c # "foobar"
@rafops
rafops / mock_polymorphic.rb
Created March 29, 2012 15:40
Mock polymorphic associations with Mocha
def mock_polymorphic
mock().tap do |p|
p.expects(:class).returns(p).at_least_once
p.expects(:base_class).returns(p).at_least_once
p.expects(:name).returns('Mock').at_least_once
p.expects(:id).returns((rand * 10e5).round).at_least_once
p.expects(:blank?).returns(false).at_least_once
p.expects(:destroyed?).returns(false).at_least_once
p.expects(:new_record?).returns(false).at_least_once
end
@rafops
rafops / svn_logs_to_sqlite3.rb
Created July 25, 2012 14:49
Save SVN logs to Sqlite3
require "sqlite3"
db = SQLite3::Database.new "logs.db"
db.execute "DROP TABLE IF EXISTS logs"
db.execute <<SQL
CREATE TABLE logs (
project VARCHAR(100),
revision INT,
contributor VARCHAR(100),
@rafops
rafops / capybara_google_hello_world.rb
Created July 26, 2012 19:17
Capybara Google Hello World with screenshot
#sudo apt-get install libxslt-dev libxml2-dev
#gem install nokogiri
#gem install capybara
#
#sudo apt-get install libqt4-dev
#sudo apt-get install g++
#
#gem install poltergeist
#
#--------------------------------------------------------------------------------
@rafops
rafops / oauth2_zend.php
Created August 31, 2012 10:51
OAuth2 Controller Zend
<?php
class AdminController extends Zend_Controller_Action
{
public function init()
{
$this->_googleConfig = Zend_Registry::get('google');
$this->_model = new Application_Model_Manager();
}
@rafops
rafops / google.ini
Created August 31, 2012 10:52
Example of Zend ini for Google OAuth2
[production]
oauth2_url = "https://accounts.google.com/o/oauth2"
api_url = "https://www.googleapis.com/oauth2/v1"
scope = "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
[staging : production]
[testing : production]
[development: production]
@rafops
rafops / index.php
Created August 31, 2012 10:53
Loading ini on Zend's index.php
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define path to vendor directory
defined('VENDOR_PATH')
|| define('VENDOR_PATH', realpath(dirname(__FILE__) . '/../vendor'));
@rafops
rafops / freak_show_01.php
Created September 6, 2012 17:46
PHP Freak Show Series - Ed 01
//===================================
//FAZ A CONEXÃO COM O BANCO==========
//===================================
$MySQL = new MySQL($DBHost,$DBUser,$DBPass);
$MySQL->DBConnect($DBDataBase);
if ( ! isset($_GET['canal']) && empty($_GET['canal']) ){
die('$_GET[canal] is empty');
}
@rafops
rafops / sshfs.txt
Created September 20, 2012 12:08
SSHFS
apt-get install sshfs
sshfs -o uid=1000,gid=1000,workaround=rename [email protected]:/home/myuser ./somedir
@rafops
rafops / restart_nfs.txt
Created September 20, 2012 12:09
Restart NFS
service nfs stop
service portmap stop
service portmap start
service nfs start
service nfs stop && service portmap stop && service portmap start && service nfs start