Skip to content

Instantly share code, notes, and snippets.

View vladzloteanu's full-sized avatar

Vlad Zloteanu vladzloteanu

View GitHub Profile
def test_auto_closing_tags
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
<xml><city>Paris</city><state/></xml>
eoxml
assert_equal [false, false, nil, nil, true, nil],
reader.map{|node| node.empty_element? if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE}
end
@vladzloteanu
vladzloteanu / gist:1286780
Created October 14, 2011 10:33
Replace newlines in a file (using tr or perl)
perl -i -p -e 's/\n//' file
tr -d '\n' < file
@vladzloteanu
vladzloteanu / gist:1767050
Created February 8, 2012 09:15
Inheritance: Class variables and constants in Ruby
class C1
SOME_CONST=3
@@some_class_var = 3
end
class C2<C1
SOME_CONST=7
end
class C3<C1
@vladzloteanu
vladzloteanu / gist:1834755
Created February 15, 2012 09:31
[ruby] run shell command, read output (from Bundler code)
def sh(cmd, &block)
out, code = sh_with_code(cmd, &block)
code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
end
def sh_with_code(cmd, &block)
cmd << " 2>&1"
outbuf = ''
Bundler.ui.debug(cmd)
Dir.chdir(base) {
@vladzloteanu
vladzloteanu / gist:2000372
Created March 8, 2012 10:48
OSX Lion: SSL verification issue: Reinstall Ruby with OpenSSL support (with rvm)
# Bug report: http://www.ruby-forum.com/topic/208026
#
## Reproduce bug
require 'socket'
require 'openssl' # Should return true
s = TCPSocket.new 'bugzilla.redhat.com', 443
ctx = OpenSSL::SSL::SSLContext.new
@vladzloteanu
vladzloteanu / gist:2017546
Created March 11, 2012 18:24
MySQL goodies
# Total size of data + indexes for *ALL* DBs
SELECT table_schema "DB Name", ROUND(SUM(data_length) / 1024 / 1024, 2) "Data size (MB)", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2)
"Data + Indexes size (MB)" FROM information_schema.TABLES GROUP BY table_schema ;
# Total size of data + indexes for *ALL* Tables in a DB
SELECT TABLE_NAME, table_rows, data_length, index_length, round(((data_length + index_length) / 1024 / 1024),2) "Total size (MB)"
FROM information_schema.TABLES WHERE table_schema = "db_name";
@vladzloteanu
vladzloteanu / gist:2020974
Created March 12, 2012 09:54
Performance profiling
# Log all slow Rails queries
grep -E -i "Load|Update|SQL|COUNT" answers.log | grep -E '\([2-9][0-9]{3,}\.[0-9]*ms\)' | sed 's/.*(\([0-9]*\)\.[0-9]*ms)/\1/g' > /home/vlad/profiling/slow_answers_queries.sql
grep -E -i "Load|Update|SQL|COUNT" auth.log | grep -E '\([1-9][0-9]{3,}\.[0-9]*ms\)' | sed 's/.*(\([0-9]*\)\.[0-9]*ms)/\1/g' > /home/vlad/profiling/slow_auth_queries.sql
## Ideas app
# test normal download
curl "https://domain-test.ideas.dimelo.com" --silent --silent --write-out "size_download=%{size_download}\n" --output /dev/null
/* Wed 14-32-41_user-jeremy-dai-334314.custom_stylesheet.e7d5.txt */
#bd .last_feedbacks_widget div.feedback div.metadata dl.vote dd.controls li.score-control-plus {left:3px;}
#bd .last_feedbacks_widget div.feedback,
#bd .most_consulted_feedbacks_widget div.feedback {margin-bottom:30px;height:200px;}
#bd .small ul.feedbacks li.feedback, #bd div.sliding_panel ul.feedbacks li.feedback {margin-bottom:0;}
.link-to-feedback {display:none;}
@vladzloteanu
vladzloteanu / gist:3793566
Created September 27, 2012 11:39
Mongoid persistence mode (fire and forget by default)
class MyModel
include Mongoid::Document
include Mongoid::Timestamps
field :value, :type => String
index :value, :unique => true
attr_accessible :value
end
@vladzloteanu
vladzloteanu / gist:8695023
Created January 29, 2014 19:24
Flask test client using full URL
import unittest
from flask import Flask, request
app = Flask(__name__)
app.testing = True
@app.route('/action')
def action():
return ""