About this user

Steve www.r3lax.com

« Newer Snippets
Older Snippets »
7 total  XML / RSS feed 

Phone Trick Automation

// Automate data entry and execution of phone trick
require 'win32ole'

ie = WIN32OLE.new('InternetExplorer.Application')
ie.navigate("http://www.phonetrick.com")
ie.visible = true
sleep 1 while ie.readyState() != 4
ie.document.all["PhoneNumberToDial"].value ="1234567890"
ie.document.all["CallerID"].value ="0123456789"
ie.document.all["CallerIDname"].value ="Matz"
ie.document.all["VoiceID"].value ="3"
ie.document.all["TextToSay"].value ="Ruby Rulez!"

call = nil
ie.document.all.tags("input").each do |i|
  if i.value == "Call!"
    call = i
    break
  end
end

  if call
    call.click()
  end 

Tight Ruby Sudoku Solver

// Small Sudoku Solver

#
#   A  ruby script to solve a sudoku puzzle
#  USAGE: ruby sudoku.rb 
#  Example:ruby sudoku.rb 000201600.....09605000
#
#    Written by:  Studlee2 at gmail dot com
#
#    Using the algorithm by http://www.ecclestoad.co.uk/
#        
$p = ARGV.shift.split(//)

def s
  h=Hash.new() 
  81.times do |j|
    next if $p[j].to_i!=0
    80.times{|k|h[k.to_i/9==j/9||k.to_i%9==j%9||k.to_i/27==j/27&&k.to_i%9/3==j%9/3?$p[k.to_i]:0]=1}
    1.upto(9){|v|next if h.has_key?(v.to_s);$p[j]=v.to_s;s}
      return $p[j]=0
  end
  return (puts "\nSolution:#{$p}")
end

s

Changed File Analysis

//Search through a directory looking for modified files - uses one-way hash function. Run once to initalize files, then run at a later date for analysis.

#
#   A ruby script to detect changed/added/deleted files
#     using one-way hash function - MD5 used, but could be changed
#
#    Written by:  Studlee2 at gmail dot com
#
# Usage: changed.rb 
#      Output files will be dumped into 
#
# MUST BE ABLE TO READ AND WRITE FILES
# Must have Digest::base installed

require 'digest/md5'

#initialize all hashes, regexp, and path array
oldfile_hash = Hash.new()
newfile_hash = Hash.new()
valid = /(.*)\s{5}(\w{32})/
#This array will store each directory to traverse
dir_array = Array.new()
dir_array[0] = ARGV.shift or raise "Missing path to traverse"

#Ensure the path is correct for file output
file_report = "#{dir_array[0]}\\file_report.txt"
file_output = "#{dir_array[0]}\\changed.files"
oldfile_output = "#{dir_array[0]}\\old_changed.files"

#Determine if the script has been run on the path before, if so change the file name
  if File.exists?(file_output)
    File.rename( file_output, oldfile_output)       #archive the file to make room for a new one
    File.open(oldfile_output, 'r+b') do |infile|   #open old_file to compare to new_file
      #read in the old files and md5 sums for each line in the file
      while (old_line = valid.match(infile.gets))
       oldfile_hash[old_line[1]] = old_line[2]
      end
    end
end

    #initialize the files to be used to write to
    report = File.new(file_report, 'wb')
    changed_files = File.new(file_output, 'wb')

  #Go through the directory and compute MD5 Hash until there aren't anymore items in directory array
  begin
    p = dir_array.shift   #remove one item from directory array 
    Dir.chdir(p)            #change to new directory to search
    #for each file in the dir, compute md5 sum and add to new hash
    Dir.foreach(p) do |filename|
      next if filename == '.' or filename == '..'   #go to next folder if '.' or '..'
      unless File::directory?(filename)                    #if not a folder, then process file
        file = File.open(filename, 'rb')
        newfile_hash[filename] = Digest::MD5.new(File.open(filename, 'rb').read).hexdigest
        file.close unless file.closed?
      else
        dir_array << p + "\\" + filename      #if nat a file, put the directories into array for later
      end
    end
  end while !dir_array.empty?
#write files found to changed.files
newfile_hash.each do |file, md5|
  changed_files.write "#{file}     #{md5}\n"
end
#remove files that are the same from hash tables
newfile_hash.keys.select {|file| newfile_hash[file] == oldfile_hash[file] }.each do |file|
  newfile_hash.delete(file)
  oldfile_hash.delete(file)
end
#write files that have been changed or added, then remove from has table
newfile_hash.each do |file, md5|
  report.write "#{oldfile_hash[file] ? "Changed" : "Added"} file: #{file} #{md5}\n"
  oldfile_hash.delete(file)
end
#write files that are left over the the oldfile_hash table - these are files that weren't found in the 
oldfile_hash.each do |file, md5|
  report.write "Deleted/Moved file: #{file} #{md5}\n"
end

Currency Converter

// A simple ruby script to convert currency

#
#   A simple ruby script to convert currency
#     can be edited to check stocks, highs, lows, etc.
#
#    Written by:  Studlee2 at gmail dot com
#
# Usage: money.rb 
# Where:  , -- ISO Currency codes
#
# MUST BE CONNECTED TO THE INTERNET
# Must have ruby-finance installed
require 'finance/currency'

#Take arguments in from command line
c = ARGV

#Ensure that usage is proper
valid = /^([\+-]?\d*(?:\.\d*)?)(\S+)\s(\S+)/
if exchange = valid.match(c)
amount = exchange[1]
from_code = exchange[2]
to_code = exchange[3]

#Let Finance::Currency do its magic
puts Finance::Currency::convert(to_code.to_s, from_code.to_s, amount)
else
#if usage is invalid - inform the user
  puts '-----USAGE IS-----'
  puts ' '
  puts ', -- ISO Currency codes'
end

Decrypt a File *Blowfish*

// A simple ruby script to decrypt a file

#
#   A simple ruby script to decrypt a file
#     can be edited to decrypt directories also
#
#    Written by:  Studlee2 at gmail dot com
#
#    Using the blowfish algorithm
#         several algorithms exist, just substitute for 'blowfish'
#         make sure it matches the encryption algorithm
require 'crypt/blowfish'

begin
#take in the file name to decrypt as an argument
  filename = ARGV.shift
  puts "Decrypting #{filename}"
  p = "Decrypted_#{filename}"
#User specifies the original key from 1-56 bytes (or guesses)
  print 'Enter your encryption key: '
  kee = gets
#initialize the decryption method using the user input key  
  blowfish = Crypt::Blowfish.new(kee)
  blowfish.decrypt_file(filename.to_str, p)
#decrypt the file  
  puts 'Decryption SUCCESS!'
rescue
#if the script busts for any reason this will catch it
  puts "\n\n\nSorry the decryption was unsuccessful"
  puts "USAGE: ruby decryption.rb <span class="escape">\n\n\n</span></span><span class="punct">"</span>
  <span class="keyword">raise</span>
<span class="keyword">end</span>
</pre>
            </div>
            <div class="post-metadata">
              to <a href="/user/studlee2/tag/ruby" class="tag">ruby</a> by <a href="/user/studlee2">studlee2</a> on Jul 08, 2006 <b></b>
            </div>
          </div>
        </div>
<a name="post554" id="post554"></a>
        <div id="post554">
          <div class="post">
            <h3 class="post-title"><a href="/posts/show/554">Encrypt a File *Blowfish*</a></h3>
            <div class="post-body">
              // A simple ruby script to encrypt a file<br>
              <br>
              <pre>
<span class="comment">#</span>
<span class="comment">#   A simple ruby script to encrypt a file</span>
<span class="comment">#     can be edited to encrypt directories also</span>
<span class="comment">#</span>
<span class="comment">#    Written by:  Studlee2 at gmail dot com</span>
<span class="comment">#</span>
<span class="comment">#    Using the blowfish algorithm</span>
<span class="comment">#        several algorithms exist, just substitute for 'blowfish'</span>
<span class="comment">#</span>
<span class="ident">require</span> <span class="punct">'</span><span class="string">crypt/blowfish</span><span class="punct">'</span>

<span class="keyword">begin</span>
<span class="comment">#take in the file name to encrypt as an argument</span>
  <span class="ident">filename</span> <span class="punct">=</span> <span class="constant">ARGV</span><span class="punct">.</span><span class="ident">shift</span>
  <span class="ident">puts</span> <span class="ident">filename</span>
  <span class="ident">c</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">Encrypted_<span class="expr">#{filename}</span></span><span class="punct">"</span>
<span class="comment">#User specifies a key from 1-56 bytes (Don't forget this or your stuff is history)</span>
  <span class="ident">print</span> <span class="punct">'</span><span class="string">Enter your encryption key (1-56 bytes): </span><span class="punct">'</span>
  <span class="ident">kee</span> <span class="punct">=</span> <span class="ident">gets</span>
<span class="comment">#initialize the encryption method using the user input key</span>
  <span class="ident">blowfish</span> <span class="punct">=</span> <span class="constant">Crypt</span><span class="punct">::</span><span class="constant">Blowfish</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">kee</span><span class="punct">)</span>
  <span class="ident">blowfish</span><span class="punct">.</span><span class="ident">encrypt_file</span><span class="punct">(</span><span class="ident">filename</span><span class="punct">.</span><span class="ident">to_str</span><span class="punct">,</span> <span class="ident">c</span><span class="punct">)</span>
<span class="comment">#encrypt the file</span>
  <span class="ident">puts</span> <span class="punct">'</span><span class="string">Encryption SUCCESS!</span><span class="punct">'</span>
<span class="keyword">rescue</span>
<span class="comment">#if the script busts for any reason this will catch it</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string"><span class="escape">\n\n\n</span>Sorry the encryption was unsuccessful</span><span class="punct">"</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string">USAGE: ruby encryption.rb <plaintext file><span class="escape">\n\n\n</span></span><span class="punct">"</span>
  <span class="keyword">raise</span>
<span class="keyword">end</span>
</pre>
            </div>
            <div class="post-metadata">
              to <a href="/user/studlee2/tag/ruby" class="tag">ruby</a> by <a href="/user/studlee2">studlee2</a> on Jul 08, 2006 <b></b>
            </div>
          </div>
        </div>
<a name="post553" id="post553"></a>
        <div id="post553">
          <div class="post">
            <h3 class="post-title"><a href="/posts/show/553">Simple Temperature Converter</a></h3>
            <div class="post-body">
              // Temperature Converter<br>
              <br>
              <pre>
<span class="comment">#</span>
<span class="comment">#   A simple ruby script to convert temperatures</span>
<span class="comment">#    Written by:  Studlee2 at gmail dot com</span>
<span class="comment">#</span>

<span class="ident">puts</span> <span class="punct">"</span><span class="string">Enter a temprature: </span><span class="punct">"</span>
<span class="ident">t</span> <span class="punct">=</span> <span class="ident">gets</span>
<span class="comment">#regexp to make sure the entry is valid</span>
<span class="ident">valid</span> <span class="punct">=</span> <span class="punct">/</span><span class="regex">^([<span class="escape">\+</span>-]?)(<span class="escape">\d</span>+)([CcFf]?)</span><span class="punct">/</span>
<span class="ident">degree</span> <span class="punct">=</span> <span class="ident">valid</span><span class="punct">.</span><span class="ident">match</span><span class="punct">(</span><span class="ident">t</span><span class="punct">)</span>
<span class="comment">#determines if you input celsius or farenheit</span>
<span class="keyword">if</span> <span class="punct">(</span><span class="ident">degree</span><span class="punct">[</span><span class="number">3</span><span class="punct">]</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">c</span><span class="punct">"</span> <span class="punct">||</span> <span class="ident">degree</span><span class="punct">[</span><span class="number">3</span><span class="punct">]</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">C</span><span class="punct">")</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string">Converting from Celsius to Farenheit</span><span class="punct">"</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string"><span class="expr">#{degree[0]}</span> to <span class="expr">#{((9.0/5.0) * degree[2].to_f + 32.0).to_i}</span> F</span><span class="punct">"</span>
<span class="keyword">elsif</span> <span class="punct">(</span><span class="ident">degree</span><span class="punct">[</span><span class="number">3</span><span class="punct">]</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">f</span><span class="punct">"</span> <span class="punct">||</span> <span class="ident">degree</span><span class="punct">[</span><span class="number">3</span><span class="punct">]</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">F</span><span class="punct">")</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string">Converting from Farenheit to Celsius</span><span class="punct">"</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string"><span class="expr">#{degree[0]}</span> to <span class="expr">#{((5.0/9.0) * (degree[2].to_f - 32.0)).to_i}</span> C</span><span class="punct">"</span>
<span class="comment">#Displays the usage if you didn't enter anything correctly</span>
<span class="keyword">else</span> 
  <span class="ident">puts</span> <span class="punct">"</span><span class="string">Usage is: [+|-]<deg>[C|F]</span><span class="punct">"</span>
<span class="keyword">end</span>

</pre>
            </div>
            <div class="post-metadata">
              to <a href="/user/studlee2/tag/ruby" class="tag">ruby</a> by <a href="/user/studlee2">studlee2</a> on Jul 08, 2006 <b></b>
            </div>
          </div>
        </div>
      </div>
      <div class="box grey paginator" style="text-align: center; color: #fff">
        <div style="float: left">
          <span class="disabled">« Newer Snippets</span>
        </div>
        <div style="float: right">
          <span class="disabled">Older Snippets »</span>
        </div>7 total <a title="RSS 2.0" href="/rss/tags"><span style="border:1px solid;border-color:#FC9 #630 #330 #F96;padding:1 4px;font:bold 11px verdana,sans-serif;color:#FFF;background:#F60;text-decoration:none;margin:0"> XML / RSS feed </span></a><br>
        <div style="margin-top: 8px;">
          <form action="#">
            <select style="width: 250px" onchange="JumpToIt(this)">
              <option value="#post610">
                Phone Trick Automation
              </option>
              <option value="#post567">
                Tight Ruby Sudoku Solver
              </option>
              <option value="#post559">
                Changed File Analysis
              </option>
              <option value="#post556">
                Currency Converter
              </option>
              <option value="#post555">
                Decrypt a File *Blowfish*
              </option>
              <option value="#post554">
                Encrypt a File *Blowfish*
              </option>
              <option value="#post553">
                Simple Temperature Converter
              </option>
            </select>
          </form>
        </div>
      </div>
    </div>
<br style="clear: both">
    <div id="footer">
      <p>Snippets (<a href="#">source code soon to be available</a>) developed by Peter Cooper and powered by Ruby On Rails</p>
    </div>
  </div>
</body>
</html>