Skip to content

Instantly share code, notes, and snippets.

@kgrz
kgrz / is_valid?.rb
Created December 1, 2011 11:32
checking for file extension using split vs regex
# The is_valid?(filename) checks if the filename has either a .txt.gz or a .txt extension
def is_valid?(filename)
arr = filename.split(/\.|\/|\\/)
return true if arr.last == "txt"
if arr.last = "gz"
return true if arr[-2, 1] == "txt"
end
return false
end
@kgrz
kgrz / gist:2843072
Created May 31, 2012 12:29
sinatra-reloader test
TestApp::Application.routes.draw do
class Api2 < Sinatra::Base
configure :development do
register Sinatra::Reloader
end
configure :production {}
get '/hello' do
"Hello World!"
end
@kgrz
kgrz / gist:2880883
Created June 6, 2012 09:24 — forked from kaiwren/gist:1283905
Steve Yegge's SOA post

From: https://raw.github.com/gist/933cc4f7df97d553ed89/24386c6a79bb4b31fb818b70b34c5eab7f12e1ff/gistfile1.txt

Stevey's Google Platforms Rant

I was at Amazon for about six and a half years, and now I've been at Google for that long. One thing that struck me immediately about the two companies -- an impression that has been reinforced almost daily -- is that Amazon does everything wrong, and Google does everything right. Sure, it's a sweeping generalization, but a surprisingly accurate one. It's pretty crazy. There are probably a hundred or even two hundred different ways you can compare the two companies, and Google is superior in all but three of them, if I recall correctly. I actually did a spreadsheet at one point but Legal wouldn't let me show it to anyone, even though recruiting loved it.

Order.class_eval do
def define_state_machine
redefine_states!
go_to_state :address
go_to_state :delivery
go_to_state :payment, :if => lambda { payment_required? }
go_to_state :confirm, :if => lambda { confirmation_required? }
go_to_state :complete
remove_transition :from => :delivery, :to => :confirm
end
@kgrz
kgrz / binary_post.rb
Created July 7, 2012 14:51
binary post
require 'sinatra'
post '/post' do
response.body.inspect # This returns [] with the curl command given below
end
__END__
Curl command used is:
@kgrz
kgrz / binary_post_2.rb
Created July 9, 2012 14:26
Binary Post #2
require 'sinatra' # version: 1.4.0 and rack version: 1.4.1
post '/post' do
request.body.each {|x| puts x.encoding} # each because the request is wrapped inside an object
# that responds to each
end
__END__
`curl --data "Hello" http://localhost:9393/post` outputs "ASCII-8BIT"
@kgrz
kgrz / net_ssh_kill_process.rb
Created August 6, 2012 18:23
Using Net::SSH gem to kill a process in a remote server. Partial answer to SO question #11832211
If you want to use the net/ssh gem to kill the process:
require 'net/ssh'
Net::SSH.start('host', 'user', :password => "password") do |ssh|
ssh.exec "ps -ax | grep ruby" do |ch, success|
ch.on_data do |c, data|
# Too lazy to write the entire code
# The "data" object has the output that gets printed out to stdout
# when the command `ps -ax | grep ruby` gets executed.
@kgrz
kgrz / worst_implementation_statement.rb
Created August 6, 2012 18:58
Converts a Gzipped text file into a csv. Text file has a specific structure. And this is exactly how not to code in Ruby
require 'zlib'
file = *ARGV
file.each do |eachfile|
Zlib::GzipReader.open(eachfile) do |f|
account = ""
result = []
f.each_line do |line|
next unless line[/\d{17}/]
account = line[6,11]
@kgrz
kgrz / form_a2_partial.rb
Created August 6, 2012 19:04
Generates a PDF documents using the Prawn Library. This was before I came across this __EPIC__ demo of Prawn https://gist.github.com/441285. And everything changed from this point onwards and I stopped writing more of this nonsensical code.
# 18th Feb 2012
require 'prawn'
Prawn::Document.generate("Form A2.pdf", :page_size => "A4", :page_layout => :portrait) do |pdf|
pdf.font "Times-Roman"
pdf.font_size 10
pdf.text "Form A2"
pdf.text "Application cum Declaration"
pdf.text "(To be completed by the applicant)"
@kgrz
kgrz / sinatra_oo_helpers.rb
Created August 9, 2012 06:14
Sinatra OO style using helpers
require 'sinatra'
get '/' do
my_domain.say_hello
end
get '/bye' do
my_domain.say_goodbye
end