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 <Sudoku puzzle on one line/no spaces with 0's being the blanks>
#  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 <path>
#      Output files will be dumped into <path>
#
# 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 <path>
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 <amount><from-code><to-code>
# Where:  <from-code>,<to_code> -- 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 '<amount><from-code> <to-code>'
  puts '<from-code>,<to-code> -- 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 <plaintext file>\n\n\n"
  raise
end

Encrypt a File *Blowfish*

// A simple ruby script to encrypt a file

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

begin
#take in the file name to encrypt as an argument
  filename = ARGV.shift
  puts filename
  c = "Encrypted_#{filename}"
#User specifies a key from 1-56 bytes (Don't forget this or your stuff is history)
  print 'Enter your encryption key (1-56 bytes): '
  kee = gets
#initialize the encryption method using the user input key
  blowfish = Crypt::Blowfish.new(kee)
  blowfish.encrypt_file(filename.to_str, c)
#encrypt the file
  puts 'Encryption SUCCESS!'
rescue
#if the script busts for any reason this will catch it
  puts "\n\n\nSorry the encryption was unsuccessful"
  puts "USAGE: ruby encryption.rb <plaintext file>\n\n\n"
  raise
end

Simple Temperature Converter

// Temperature Converter

#
#   A simple ruby script to convert temperatures
#    Written by:  Studlee2 at gmail dot com
#

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

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