Skip to content

Instantly share code, notes, and snippets.

@zigzag
zigzag / MailFetcher.rb
Created December 3, 2009 12:37
A MailFetcher fecthing mails via IMAP and parsing it with Tmail. A enhanced module is included to fitler the mail by some condition and also dump the mail content and attachment to the file system.
require 'net/imap'
require 'tmail_mail_extension'
class MailFetcher
cattr_accessor :email_download_path
self.email_download_path = File.join(RAILS_ROOT, 'public','emails_download')
def initialize(setting)
@setting = setting
end
@zigzag
zigzag / Tpie_should_work_like...
Created December 6, 2009 07:13
Target DSL of TPIE
<% ... %>
<table border="1" cellspacing="5" cellpadding="5">
<tr><th>Info</th><th>Value</th></tr>
<tr><td>Client</td><td><%= customerName %></td></tr>
<tr><td>Project Name</td><td><%= name %></td></tr>
<tr><td>Description</td><td><%= desc %></td></tr>
</table>
<% ... %>
<h1>Employees</h1>
<table border="1" cellspacing="5" cellpadding="5">
@zigzag
zigzag / newpost.rb
Created December 12, 2009 13:38 — forked from al3x/newpost.rb
New post command shell for Jekyll
#!/usr/bin/ruby
unless ARGV[0]
puts 'Usage: np "the post title"'
exit(-1)
end
title = ARGV.join ' '
date_prefix = Time.now.strftime("%Y-%m-%d")
postname = title.strip.downcase.gsub(/ /, '-')
@zigzag
zigzag / i18n_extractor.rb
Created December 13, 2009 15:50
a handy tool for extract text from erb files for i18n
class I18nExtractor
SKIP_INLINE_TAG = [/<%(.*?)%>/,/<(.*?)>/,/<%(.*)$/,/^(.*)%>/]
SEPERATOR = '_@@@_'
SKIP_TAGS = [[/<script/i,/<\/script>/i],[/<%/,/%>/],[/<style/i,/\/style>/i]]
def initialize(filename)
p '-----------------------------------------'
p filename
p '-----------------------------------------'
@filename = filename
<table width="100%">
<tr>
<td valign="top">
<h1><%=t(:text_groups)%></h1>
<br/>
<table class="data" id="groups">
<thead>
<tr>
<th class="left"><%=t(:text_group_name)%></th>
<th class="left"><%=t(:text_description)%></th>
find . -type f |grep "\.txt$" | xargs cat | wc -l
ruby -e 'p Dir.glob("./**/*.txt").reduce(0){|sum,f| sum += File.readlines(f).size}'
543
@zigzag
zigzag / simplest_proxy.rb
Created February 12, 2010 11:31
The simplest proxy server written in Ruby
myport = 80
hostname = 'xxx.xxx.xxx.xxx'
port = 80
server = TCPServer.open(myport)
while true
Thread.start(server.accept) do |client|
s = TCPSocket.open(hostname, port)
req_content_length = 0
#Dir.glob("**/*.java").map{|it| it.split('/').last[0..-6]}.map{|it| [it, it.length]}.sort{|a, b| b[1] <=> a[1]}[0..5]
# A bit short version:
Dir['**/*'].grep(/(\w*)\.java/){$1}.sort_by(&:length).reverse.first(5).map{|it| [it,it.length]}