<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Life's End</title>
    <link>http://lifesend.com/</link>
    <description>Worship</description>
    <language>en-us</language>
    <item>
      <title>Ruby snipits</title>
      <description>&lt;!-- 
Found 0 labeled equations 
eq ids: 
--&gt;
&lt;!-- 
Found 0 figures 
fig ids: 
--&gt;
&lt;style type="text/css"&gt;pre.code {   background-color: #DDD;   color: #112;   padding: 10px;   font-size: 90%;   overflow: auto;   margin: 4px 0px;   width: 95%;}pre .normal {}pre .comment { color: #005; font-style: italic; }pre .keyword { color: #A00; font-weight: bold; }pre .method { color: #077; }pre .class { color: #074; }pre .module { color: #050; }pre .punct { color: #447; font-weight: bold; }pre .symbol { color: #099; }pre .string { color: #944; }pre .char { color: #F07; }pre .ident { color: #004; }pre .constant { color: #07F; }pre .regex { color: #B66; }pre .number { color: #F99; }pre .attribute { color: #5bb; }pre .global { color: #7FB; }pre .expr { color: #227; }pre .escape { color: #277; }div .aao_footer{ font-size: 80%; }&lt;/style&gt;
   &lt;div id=header&gt;
   &lt;h2&gt;Ruby Snipits&lt;/h2&gt;
Aaron Radke &lt;br&gt;
2008-05-22 &lt;br&gt;
&lt;em&gt;A collection of ruby snippits that contain some chunk of code to get something working.&lt;/em&gt;&lt;p&gt;
&lt;/div&gt;
&lt;div id=toc&gt;&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#simple_definition_test"&gt;   Simple Definition Test&lt;/a&gt;
&lt;ol&gt;
&lt;/ol&gt;
&lt;li&gt;&lt;a href="#super_test"&gt;   Super Test&lt;/a&gt;
&lt;ol&gt;
&lt;/ol&gt;
&lt;li&gt;&lt;a href="#database"&gt;   Database&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#active_record"&gt;   Active Record&lt;/a&gt;
&lt;ol&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;li&gt;&lt;a href="#user_interaction"&gt;   User Interaction&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#highline"&gt;   Highline&lt;/a&gt;
&lt;ol&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;/div
&lt;h2&gt;&lt;a id="simple_definition_test"&gt;&lt;/a&gt; Simple definition test 
&lt;/h2&gt;
&lt;pre class="prettyprint"&gt;
def test
   puts "this is a test"
end
&lt;/pre&gt;

&lt;h2&gt;&lt;a id="super_test"&gt;&lt;/a&gt; Super Test 
&lt;/h2&gt;
&lt;pre class="prettyprint"&gt;
class YammDB &lt; Hash
   def initialize(opts={})
      puts "making YammDB #{opts.inspect}"
      super
   end
end
class Sims &lt; YammDB
   def initialize(opts={})
      puts "making Sims with opts: #{opts.inspect}"
      super(opts)
   end
end
   
Sims.new(:blah)
Sims.new()
&lt;/pre&gt;

&lt;h2&gt;&lt;a id="database"&gt;&lt;/a&gt; Database 
&lt;/h2&gt;

&lt;h3&gt;&lt;a id="active_record"&gt;&lt;/a&gt; Active Record&lt;/h3&gt;
&lt;pre class="prettyprint"&gt;
#!/usr/bin/ruby
require 'rubygems'
require 'sqlite3'
require 'activerecord'
   
# connect to database.  This will create one if it doesn't exist
MY_DB_NAME = File.dirname(__FILE__) + "/my.db"
MY_DB = SQLite3::Database.new(MY_DB_NAME)
   
# get active record set up
ActiveRecord::Base.establish_connection(:adapter =&gt; 'sqlite3', :database =&gt; MY_DB_NAME)
ActiveRecord::Base.logger = Logger.new(File.open("db.log","a"))
ActiveRecord::Base.colorize_logging = false
#ActiveRecord::Base.logger = Logger.new(STDERR)
   
# create your AR class
class Routing &lt; ActiveRecord::Base
   has_and_belongs_to_many :destinations
   has_and_belongs_to_many :sources
end
class Source &lt; ActiveRecord::Base
   has_many :routings
   has_many :subsystems
end
class Destinations &lt; ActiveRecord::Base
   has_many :routings
   has_many :subsystems
end
class Subsystem &lt; ActiveRecord::Base
   has_many :destinations
   has_many :sources
end
   
class CreateTable01 &lt; ActiveRecord::Migration
   def self.up
      create_table :routings do |t|
         t.column :name, :string, :null =&gt; false
         t.column :description, :string
         t.column :xml_file, :string, :null =&gt; false
      end
      create_table :subsystems do |t|
         t.column :name, :integer
         t.column :version, :integer
      end
      create_table :sources do |t|
         t.column :subsystem_id, :integer
      end
      create_table :destinations do |t|
         t.column :subsystem_id, :integer
      end
      create_table :sources_routings, id =&gt; false do |t|
         t.column :routing_id, :integer
         t.column :subsystem_id, :integer
      end
      create_table :destinations_routings, id =&gt; false do |t|
         t.column :subsystem_id, :integer
      end
   end
      
   def self.down
      drop_table :routings
      drop_table :sources
      drop_table :destinations
      drop_table :sources_routings
      drop_table :destinations_routings
      drop_table :subsystems
   end
end
   
      
#CreateTable01.migrate(:down)
#CreateTable01.migrate(:up)
   
r = Routing.create(:name =&gt; 'test', :description =&gt; 'test routing', :xml_file =&gt; 'test.xml')
r = Routing.find_by_name('test')
   
puts r.name
   
&lt;/pre&gt;

&lt;h2&gt;&lt;a id="user_interaction"&gt;&lt;/a&gt; User interaction 
&lt;/h2&gt;

&lt;h3&gt;&lt;a id="highline"&gt;&lt;/a&gt; Highline&lt;/h3&gt;
&lt;pre class="prettyprint"&gt;
#!/usr/bin/ruby
require 'rubygems'
require 'highline'
   
      
@hl = HighLine.new
   
@hl.choose do |menu|
   menu.prompt = "Please choose your favorite programming language?  "
      
   menu.choice(:ruby) { @hl.say("Good choice!") }
   menu.choices(:python, :perl) { @hl.say("Not from around here, are you?") }
end
   
&lt;/pre&gt;
&lt;div class=aao_footer&gt;
&lt;hr&gt;

&lt;/div&gt;
</description>
      <author>Aaron Radke</author>
      <pubDate>Thu, 22 May 2008 12:18:18 +0000</pubDate>
      <link>&lt;a href="/feed/code"&gt;/ruby_snipits&lt;/a&gt;</link>
      <guid>&lt;a href="/feed/code"&gt;/ruby_snipits&lt;/a&gt;</guid>
    </item>
    <item>
      <title>rpass</title>
      <description>&lt;!-- 
Found 0 labeled equations 
eq ids: 
--&gt;
&lt;!-- 
Found 0 figures 
fig ids: 
--&gt;
&lt;style type="text/css"&gt;pre.code {   background-color: #DDD;   color: #112;   padding: 10px;   font-size: 90%;   overflow: auto;   margin: 4px 0px;   width: 95%;}pre .normal {}pre .comment { color: #005; font-style: italic; }pre .keyword { color: #A00; font-weight: bold; }pre .method { color: #077; }pre .class { color: #074; }pre .module { color: #050; }pre .punct { color: #447; font-weight: bold; }pre .symbol { color: #099; }pre .string { color: #944; }pre .char { color: #F07; }pre .ident { color: #004; }pre .constant { color: #07F; }pre .regex { color: #B66; }pre .number { color: #F99; }pre .attribute { color: #5bb; }pre .global { color: #7FB; }pre .expr { color: #227; }pre .escape { color: #277; }div .aao_footer{ font-size: 80%; }&lt;/style&gt;
   &lt;div id=header&gt;
   &lt;h2&gt;Rpass&lt;/h2&gt;
Aaron Radke &lt;br&gt;
2008-05-22 &lt;br&gt;
&lt;em&gt;An all ruby system to easily store and retrieve encrypted information.&lt;/em&gt;&lt;p&gt;
&lt;/div&gt;
Download: &lt;a href='/static/file/rpass.rb'&gt;rpass.rb&lt;/a&gt;&lt;pre class="prettyprint"&gt;
#!/usr/bin/ruby
#title:rpass
#date: 2008-05-19
#author:Aaron Radke
# TODO: add an optional encrypted file to use
require 'rubygems'
require 'crypt/rijndael'
require 'base64'
require 'stringio'
require 'crypt/blowfish'
#require 'highline/import'
require 'highline'
   
      
class RPass
   def initialize(opts={})
      @highline = HighLine.new
      @opts = opts
      @opts[:file] = File.dirname(__FILE__) + "/stored.aes" unless @opts.key?(:file)
      @key = @highline.ask("key for #{@opts[:file]}? ") { |q| q.echo = false}
      @rijndael = Crypt::Rijndael.new("%-32.32s" % @key)
      self.decrypt
   end
      
   def decrypt
      if File.exists?(@opts[:file])
         puts "Decrypting ..."
         #---decrypt
         encryptedString = ""
         File.open(@opts[:file], "r").each{|line|
            encryptedString &lt;&lt; line
         }
         # TODO?   Need to add the proper logic if the string did not decrypt
         f = File.open(@opts[:file], "r")
         begin
            @decryptedString = @rijndael.decrypt_string(encryptedString)
         rescue RuntimeError
            puts "Error: Could not decrypt: " + $!
            exit
         end
            
         if decryptedString.class != String
            puts "Error: could not decrypt unkown type"
            exit
         end
            
         #some other key will cause the header to be scrambled
         if decryptedString =~ /^title:rpass/
            @decryptedLines = decryptedString.split(/\n/)[1..-1]
         else
            puts "Error: could not decrypt"
            exit
         end
            
      else
         key2 = @highline.ask("new file: renter key ? ") { |q| q.echo = false}
         if key2 != @key
            puts "Error: Setting up new key failed"
            exit
         else
            @decryptedLines = []
         end
      end
   end
      
   def encrypt
      puts "Saving encrypted version..."
      plainString = @decryptedLines.join("\n")
      plainString = "title:rpass\n" + plainString
      f = File.open(@opts[:file],"w")
      print @rijndael.encrypt_stream(plainString)
      f.close
   end
      
   def insert(text, index = -1)
      @decryptedLines.insert(index, text)
   end
      
   def replace(text, index = -1)
      @decryptedLines[index] = text
   end
   def delete(index = -1)
      @decryptedLines.delete_at(index)
   end
      
   def grep(pattern)
      @decryptedLines.each_with_index{|line,i|
         if line =~ /#{pattern}/i
            puts "#{i}: #{line}"
         end
      }
   end
      
   def ask
      command = @highline.ask("rpass&gt; ", String)
         
      command.sub!(/^\s+/,'')
      command.sub!(/\s+$/,'')
         
      if command =~ /^h(elp)?$/
         puts "Available commands:"
         puts %{---------
            (h)elp
            (l)ist
            exit
            (s)ave
            reload
            (g)rep pattern
            (d)elete index
            (i)nsert index text
            (a)dd text
            newkey key
            (r)eplace index text
         }.gsub(/\t+/,'')
      elsif command =~ /^(exit|quit)$/
         encrypt
         return false
      elsif command =~ /^s(ave)?$/
         encrypt
      elsif command =~ /^reload$/
         decrypt
      elsif command =~ /^l(ist)?$/
         grep(".?")
      elsif command =~ /^g(rep)?\s+(.*)$/
         grep($2)
      elsif command =~ /^d(elete)?\s+(-?\d+)$/
         delete($2.to_i)
      elsif command =~ /^i(nsert)?\s+(-?\d+)\s+(.*)$/
         insert($3,$2.to_i)
      elsif command =~ /^a(dd)?\s+(.*)$/
         insert($2,-1)
         grep($2)
      elsif command =~ /^r(eplace)?\s+(-?\d+)\s+(.*)$/
         replace($3,$2.to_i)
      elsif command =~ /^newkey$/
         key1 = @highline.ask("Enter a new key? ") { |q| q.echo = false}
         key2 = @highline.ask("Renter new key? ") { |q| q.echo = false}
         if key1 == key2
            puts "Setting up new key..."
            @key = key1
            @rijndael = Crypt::Rijndael.new(@key)
         else
            puts "Error, key not duplicated.  New key not set."
         end
         replace($2,$1.to_i)
      else
         grep(command)
      end
         
      return true
   end
      
end
   
      
if ARGV.size == 1
   rpass = RPass.new(:file =&gt; ARGV[0])
else
   rpass = RPass.new
end
   
while rpass.ask
end
&lt;/pre&gt;
&lt;div class=aao_footer&gt;
&lt;hr&gt;

&lt;/div&gt;
</description>
      <author>Aaron Radke</author>
      <pubDate>Thu, 22 May 2008 12:17:59 +0000</pubDate>
      <link>&lt;a href="/feed/code"&gt;/rpass&lt;/a&gt;</link>
      <guid>&lt;a href="/feed/code"&gt;/rpass&lt;/a&gt;</guid>
    </item>
  </channel>
</rss>
