<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">
<channel>
	<title>Ruby and Rails Snippets</title>
	<description>Ruby and Rails Snippets Feed Digest</description><link>http://app.feed.informer.com/digest3/rubyonbr_snippets.html</link>
											<copyright>Respective post owners and feed distributors</copyright>
											<generator>http://feed.informer.com/</generator>

<item>
	<title>Using conditional assignment in Ruby</title>
	<description>Use conditional assignment when you want a fail safe method to reference a variable's value. In other words if the variable didn't previously contain a value then it will be initialised with one.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;a = 'crazy'
&lt;br /&gt;a ||= 'attentive'
&lt;br /&gt;puts 'my current mood is ' + a
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;=&gt; my current mood is crazy
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;a ||= 'attentive'
&lt;br /&gt;puts 'my current mood is ' + a
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;=&gt; my current mood is attentive
&lt;br /&gt;
&lt;br /&gt;References:
&lt;br /&gt; - &lt;a href="http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators"&gt;Ruby Programming/Syntax/Operators&lt;/a&gt; [wikibooks.org]
&lt;br /&gt; - &lt;a href="http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case"&gt;DABlog A short-circuit (||=) edge case&lt;/a&gt; [rubypal.com]&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/368379057" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/368379057/5946</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/368379057/5946?</guid>
	<pubDate>Mon, 18 Aug 2008 13:44 GMT</pubDate>

</item>

<item>
	<title>Logging with Ruby</title>
	<description>The Logger class in the Ruby standard library, helps write log messages to a file or stream. It supports time- or size-based rolling of log files. Messages can be assigned severities, and only those messages at or above the logger's current reporting level will be logged.
&lt;br /&gt;
&lt;br /&gt;When you write code, you simply assume that all the messages will be logged. At runtime, you can get a more or a less verbose log by changing the log level. A production application usually has a log level of Logger::INFO or Logger::WARN. From least to most severe, the instance methods are Logger.debug, Logger.info, Logger.warn, Logger.error, and Logger.fatal.
&lt;br /&gt;
&lt;br /&gt;The DEBUG log level is useful for step-by-step diagnostics of a complex task. The ERROR level is often used when handling exceptions: if the program can't solve a problem, it logs the exception rather than crash and expects a human administrator to deal with it. The FATAL level should only be used when the program cannot recover from a problem, and is about to crash or exit.
&lt;br /&gt;
&lt;br /&gt;If your log is being stored in a file, you can have Logger rotate or replace the log file when it gets too big, or once a certain amount of time has elapsed:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;require 'logger'  
&lt;br /&gt;# Keep data for the current month only  
&lt;br /&gt;Logger.new('this_month.log', 'monthly')  
&lt;br /&gt;# Keep data for today and the past 20 days.  
&lt;br /&gt;Logger.new('application.log', 20, 'daily')  
&lt;br /&gt;# Start the log over whenever the log exceeds 100 megabytes in size.  
&lt;br /&gt;Logger.new('application.log', 0, 100 * 1024 * 1024)  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;The code below, uses the application's logger to print a debugging message, and (at a higher severity) as part of error-handling code.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;#logex.rb  
&lt;br /&gt;require 'logger'  
&lt;br /&gt;$LOG = Logger.new('log_file.log', 'monthly')  
&lt;br /&gt;def divide(numerator, denominator)  
&lt;br /&gt; $LOG.debug("Numerator: #{numerator}, denominator #{denominator}")  
&lt;br /&gt; begin  
&lt;br /&gt;   result = numerator / denominator  
&lt;br /&gt; rescue Exception =&gt; e  
&lt;br /&gt;   $LOG.error "Error in division!: #{e}"  
&lt;br /&gt;   result = nil  
&lt;br /&gt; end  
&lt;br /&gt; return result  
&lt;br /&gt;end  
&lt;br /&gt;divide(10, 2)  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;The contents of the file log_file.log is:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;# Logfile created on Tue Mar 18 17:09:29 +0530 2008 by /  
&lt;br /&gt;D, [2008-03-18T17:09:29.216000 #2020] DEBUG -- : Numerator: 10, denominator 2  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;Now try to call the method by:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;divide(10, 0)  &lt;/code&gt;
&lt;br /&gt;Source: &lt;a href="http://rubylearning.com/satishtalim/ruby_logging.html"&gt;Ruby Logging: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial&lt;/a&gt; [rubylearning.com]
&lt;br /&gt;The contents of the file log_file.log is:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;# Logfile created on Tue Mar 18 17:09:29 +0530 2008 by /  
&lt;br /&gt;D, [2008-03-18T17:09:29.216000 #2020] DEBUG -- : Numerator: 10, denominator 2  
&lt;br /&gt;D, [2008-03-18T17:13:50.044000 #2820] DEBUG -- : Numerator: 10, denominator 0  
&lt;br /&gt;E, [2008-03-18T17:13:50.044000 #2820] ERROR -- : Error in division!: divided by 0 
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;To change the log level, simply assign the appropriate constant to level:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;$LOG.level = Logger::ERROR  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Now our logger will ignore all log messages except those with severity ERROR or FATAL. The contents of the file log_file.log is:
&lt;br /&gt;
&lt;br /&gt;   1. E, [2008-03-18T17:15:59.919000 #2624] ERROR -- : Error in division!: divided by 0  
&lt;br /&gt;
&lt;br /&gt;E, [2008-03-18T17:15:59.919000 #2624] ERROR -- : Error in division!: divided by 0
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/368359062" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/368359062/5945</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/368359062/5945?</guid>
	<pubDate>Mon, 18 Aug 2008 13:25 GMT</pubDate>

</item>

<item>
	<title>Serializing objects in Ruby</title>
	<description>Source: &lt;a href="http://rubylearning.com/satishtalim/object_serialization.html"&gt;Object Serialization: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial&lt;/a&gt; [rubylearning.com]
&lt;br /&gt;&lt;snip&gt;
&lt;br /&gt;Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. Ruby calls this kind of serialization marshaling.
&lt;br /&gt;
&lt;br /&gt;We will write a basic class p051gamecharacters.rb just for testing marshalling.
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;# p051gamecharacters.rb  
&lt;br /&gt;class GameCharacter  
&lt;br /&gt; def initialize(power, type, weapons)  
&lt;br /&gt;   @power = power  
&lt;br /&gt;   @type = type  
&lt;br /&gt;   @weapons = weapons  
&lt;br /&gt; end  
&lt;br /&gt; attr_reader :power, :type, :weapons  
&lt;br /&gt;end  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;The program p052dumpgc.rb creates an object of the above class and then uses Marshal.dump to save a serialized version of it to the disk.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;# p052dumpgc.rb  
&lt;br /&gt;require 'p051gamecharacters.rb'  
&lt;br /&gt;gc = GameCharacter.new(120, 'Magician', ['spells', 'invisibility'])  
&lt;br /&gt;puts gc.power.to_s + ' ' + gc.type + ' '  
&lt;br /&gt;gc.weapons.each do |w|  
&lt;br /&gt; puts w + ' '  
&lt;br /&gt;end  
&lt;br /&gt; 
&lt;br /&gt;File.open('game', 'w+') do |f|  
&lt;br /&gt; Marshal.dump(gc, f)  
&lt;br /&gt;end 
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;The program p053loadgc.rb uses Marshal.load to read it in.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;# p053loadgc.rb  
&lt;br /&gt;require 'p051gamecharacters.rb'  
&lt;br /&gt;File.open('game') do |f|  
&lt;br /&gt; @gc = Marshal.load(f)  
&lt;br /&gt;end  
&lt;br /&gt; 
&lt;br /&gt;puts @gc.power.to_s + ' ' + @gc.type + ' '  
&lt;br /&gt;@gc.weapons.each do |w|  
&lt;br /&gt; puts w + ' '  
&lt;br /&gt;end  
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Marshal only serializes data structures. It can't serialize Ruby code (like Proc objects), or resources allocated by other processes (like file handles or database connections). Marshal just gives you an error when you try to serialize a file.
&lt;br /&gt;
&lt;br /&gt;&lt;/snip&gt;&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/368359063" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/368359063/5944</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/368359063/5944?</guid>
	<pubDate>Mon, 18 Aug 2008 13:12 GMT</pubDate>

</item>

<item>
	<title>Getting you shoes into motion</title>
	<description>Source: &lt;a href="http://shoooes.net/tutorial/"&gt;Shoes • The Tutorial Walkthrough&lt;/a&gt; [shoooes.net]
&lt;br /&gt;
&lt;br /&gt;The black star will follow the users' mouse movement.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;Shoes.app do
&lt;br /&gt;  @shape = star :points =&gt; 5
&lt;br /&gt;  motion do |left, top|
&lt;br /&gt;    @shape.move left, top
&lt;br /&gt;  end
&lt;br /&gt;end
&lt;br /&gt;&lt;/code&gt;&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/367430918" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/367430918/5937</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/367430918/5937?</guid>
	<pubDate>Sun, 17 Aug 2008 12:24 GMT</pubDate>

</item>

<item>
	<title>Animate with shoes</title>
	<description>Source: &lt;a href="http://shoooes.net/tutorial/"&gt;Shoes • The Tutorial Walkthrough&lt;/a&gt; [shoooes.net]
&lt;br /&gt;The shoes logo will appear to move diagonally from the top left towards the bottom right and the follow the same path back up.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;Shoes.app do
&lt;br /&gt;  @shoes = image "http://shoooes.net/shoes.png",
&lt;br /&gt;    :top =&gt; 10, :left =&gt; 10
&lt;br /&gt;  offset = 1
&lt;br /&gt;  bounced = false
&lt;br /&gt;  animate do |i|
&lt;br /&gt;    if not bounced then
&lt;br /&gt;      offset += 1 
&lt;br /&gt;    else
&lt;br /&gt;      offset -= 1
&lt;br /&gt;    end
&lt;br /&gt;
&lt;br /&gt;    @shoes.top =  offset * 4 
&lt;br /&gt;    @shoes.left = offset * 5 
&lt;br /&gt;
&lt;br /&gt;    if offset &gt; 100 and not bounced then 
&lt;br /&gt;       bounced = true
&lt;br /&gt;    elsif offset &lt; 0 and bounced then
&lt;br /&gt;       bounced = false
&lt;br /&gt;    end
&lt;br /&gt;  end
&lt;br /&gt;end
&lt;br /&gt;&lt;/code&gt;&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/367373501" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/367373501/5934</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/367373501/5934?</guid>
	<pubDate>Sun, 17 Aug 2008 10:44 GMT</pubDate>

</item>

<item>
	<title>Putting on your Ruby shoes</title>
	<description>Source: &lt;a href="http://shoooes.net/tutorial/"&gt;Shoes • The Tutorial Walkthrough&lt;/a&gt; [shoooes.net] via &lt;a href="http://www.rubyinside.com/whys-shoes-grows-up-1014.html"&gt;Shoes - Ruby’s Cross Platform GUI App Toolkit - Grows Up&lt;/a&gt; [RubyInside.com]
&lt;br /&gt;
&lt;br /&gt;Installation (Ubuntu):
&lt;br /&gt;Prior to installation ensure the following packages are installed:
&lt;br /&gt;libcairo2-dev libpixman-1-dev libpango1.0-dev libungif4-dev libjpeg62-dev libgtk2.0-dev vlc libvlc0-dev libsqlite3-dev libcurl4-openssl-dev ruby1.8-dev rake
&lt;br /&gt;
&lt;br /&gt; - 1) Download the source code from http://shoooes.net/downloads/
&lt;br /&gt; - 2) tar zxvf shoes* 
&lt;br /&gt; - 3) cd shoes*
&lt;br /&gt; - 4) make
&lt;br /&gt; - 5) sudo make install
&lt;br /&gt;
&lt;br /&gt;file: firstapp.rb
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;Shoes.app {
&lt;br /&gt;  @push = button "Push me"
&lt;br /&gt;  @note = para "Nothing pushed so far"
&lt;br /&gt;
&lt;br /&gt;  @push.click {
&lt;br /&gt;    @note.replace "Aha! Click!"
&lt;br /&gt;  }
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;running the code: type shoes firstapp.rb
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;output: &lt;a href="http://twitxr.com/image/88938/"&gt;firstapp.rb application&lt;/a&gt; [twitxr.com]
&lt;br /&gt;
&lt;br /&gt;Reference: &lt;a href="http://www.juixe.com/techknow/index.php/2007/08/27/running-with-shoes-a-mini-gui/"&gt;Running with Shoes - A Mini GUI Toolkit&lt;/a&gt; [juixe.com]
&lt;br /&gt; - Shoes reference manual (from the command line type shoes -m) &lt;a href="http://twitxr.com/image/88950/"&gt;Shoes Reference Manual screenshot&lt;/a&gt; [twitxr.com]&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/367304501" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/367304501/5933</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/367304501/5933?</guid>
	<pubDate>Sun, 17 Aug 2008 08:38 GMT</pubDate>

</item>

<item>
	<title>Monitor network traffic with libpcap-ruby</title>
	<description>Source: &lt;a href="http://arstechnica.com/articles/columns/linux/linux-20051002.ars"&gt;Monitoring network traffic with Ruby and Pcap&lt;/a&gt; [arstechnica.com]
&lt;br /&gt;
&lt;br /&gt;I used this script on an Ubuntu server running Asterisk to monitor the SIP traffic on port 5060.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;#!/usr/bin/ruby
&lt;br /&gt;
&lt;br /&gt;# this line imports the libpcap ruby bindings
&lt;br /&gt;require 'pcaplet'
&lt;br /&gt;
&lt;br /&gt;# create a sniffer that grabs the first 1500 bytes of each packet
&lt;br /&gt;$network = Pcaplet.new('-s 1500')
&lt;br /&gt;
&lt;br /&gt;# create a filter that uses our query string and the sniffer we just made
&lt;br /&gt;$filter = Pcap::Filter.new('udp and dst port 5060', $network.capture)
&lt;br /&gt;
&lt;br /&gt;# add the new filter to the sniffer
&lt;br /&gt;$network.add_filter($filter)
&lt;br /&gt;
&lt;br /&gt;# iterate over every packet that goes through the sniffer
&lt;br /&gt;for p in $network
&lt;br /&gt;  # print packet data for each packet that matches the filter
&lt;br /&gt;  puts p.udp_data if $filter =~ p
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;This script was run as root eg. sudo ./packet-filter.rb
&lt;br /&gt;
&lt;br /&gt;sample output:
&lt;br /&gt;
&lt;br /&gt;NOTIFY sip:doorphone@192.168.1.104:5060 SIP/2.0
&lt;br /&gt;From: &lt;sip:doorphone@192.168.1.254:5060&gt;;tag=c0a801fe-13c4-3-fbc-41cc
&lt;br /&gt;To: &lt;sip:doorphone@192.168.1.104:5060&gt;
&lt;br /&gt;Call-ID: 60a9185d--063b--4da3--a40f--587a6bdcdb6d@192.168.1.104
&lt;br /&gt;CSeq: 2256 NOTIFY
&lt;br /&gt;Via: SIP/2.0/UDP 92.236.96.224:5060;branch=z9hG4bK-21087-8109408-15e8
&lt;br /&gt;Subscription-State: active
&lt;br /&gt;Event: keep-alive
&lt;br /&gt;Max-Forwards: 70
&lt;br /&gt;User-Agent: TA612V-V1.2_73
&lt;br /&gt;Supported: timer,replaces
&lt;br /&gt;Contact: &lt;sip:doorphone@192.168.1.254:5060&gt;
&lt;br /&gt;Content-Length: 0
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;Note: I could have set debugging on from the Asterisk console however this script allows me to filter the flow of network traffic in a more refined manner.
&lt;br /&gt;
&lt;br /&gt;Reference: &lt;a href="http://home.insight.rr.com/procana/"&gt;Ethereal/Wireshark Capture Filters&lt;/a&gt; [insight.rr.com]&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/366837191" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/366837191/5931</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/366837191/5931?</guid>
	<pubDate>Sat, 16 Aug 2008 16:48 GMT</pubDate>

</item>

<item>
	<title>List HTTP Status Codes in Rails</title>
	<description>If you follow the Rails way of RESTful controllers you should familiarize yourself with all the return status codes available in the HTTP protocol.
&lt;br /&gt;
&lt;br /&gt;The following is a handy list, like "rake routes", for listing out all the HTTP codes that you can return in your controller logic.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;desc 'Lists ActionController::StatusCodes::STATUS_CODES like routes'
&lt;br /&gt;task :status_codes =&gt; :environment do
&lt;br /&gt;  puts "Status - Name"
&lt;br /&gt;  ActionController::StatusCodes::STATUS_CODES.to_a.sort.each { |code, message| 
&lt;br /&gt;    puts "#{code}    - #{message.gsub(/ /, "").underscore.to_sym}"
&lt;br /&gt;  } if ActionController::StatusCodes.constants.include?('STATUS_CODES')
&lt;br /&gt;end
&lt;br /&gt;&lt;/code&gt;&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/366717264" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/366717264/5930</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/366717264/5930?</guid>
	<pubDate>Sat, 16 Aug 2008 13:15 GMT</pubDate>

</item>

<item>
	<title>Strip the line numbers from code snippets</title>
	<description>Source: &lt;a href="http://saaridev.blogspot.com/2007/12/ruby-one-liner-to-strip-line-number.html"&gt;Saari Development: Ruby: One liner to strip the line number from code posted&lt;/a&gt; [blogspot.com]
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;ruby -lne 'puts $_.gsub(/^\s+?\d+\s/,"")' ali.rb &gt; ali.rb
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Here's a page that allows you to &lt;a href="http://rorbuilder.info/r/strip-line-numbers/index.html"&gt;strip line numbers without any script&lt;/a&gt; [rorbuilder.info].&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/366461994" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/366461994/5929</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/366461994/5929?</guid>
	<pubDate>Sat, 16 Aug 2008 05:49 GMT</pubDate>

</item>

<item>
	<title>Simplify a ProjectX request using projectx-helper.cgi</title>
	<description>This Ruby CGI script is for use as an alternative to having the client prepare a ProjectX XML request against projectx.cgi.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;#!/usr/bin/ruby
&lt;br /&gt;# projectx-helper.cgi
&lt;br /&gt;
&lt;br /&gt;require 'cgi'
&lt;br /&gt;
&lt;br /&gt;cgi = CGI.new
&lt;br /&gt;
&lt;br /&gt;h = cgi.params
&lt;br /&gt;
&lt;br /&gt;puts "Content-Type: text/xml"
&lt;br /&gt;puts
&lt;br /&gt;
&lt;br /&gt;def pparam(h,var)
&lt;br /&gt;  val = ''
&lt;br /&gt;  if not h[var].empty? then
&lt;br /&gt;    val = h[var]
&lt;br /&gt;    h.delete(var)
&lt;br /&gt;  end
&lt;br /&gt;  val
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;def format_param(var, val)
&lt;br /&gt;    "#{' '*8}&lt;param var='#{var}'&gt;#{val}&lt;/param&gt;\r"
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;def pparam_remainder(h)
&lt;br /&gt;  buffer = ''
&lt;br /&gt;  h.each do |param|
&lt;br /&gt;    buffer &lt;&lt; format_param(param[0],param[1])
&lt;br /&gt;  end
&lt;br /&gt;  buffer
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;xml_project =&lt;&lt;XML_PROJECT
&lt;br /&gt;&lt;project name='#{pparam(h,'project')}'&gt;
&lt;br /&gt;  &lt;methods&gt;
&lt;br /&gt;    &lt;method name='#{pparam(h,'method')}'&gt;
&lt;br /&gt;      &lt;params&gt;
&lt;br /&gt;#{pparam_remainder(h)}
&lt;br /&gt;      &lt;/params&gt;
&lt;br /&gt;    &lt;/method&gt;
&lt;br /&gt;  &lt;/methods&gt;
&lt;br /&gt;&lt;/project&gt;
&lt;br /&gt;XML_PROJECT
&lt;br /&gt;
&lt;br /&gt;puts xml_project
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;example input url: http://yourwebsite.com/p/projectx-helper.cgi?project=abc&amp;ethod=create&amp;th=mdynarex&amp;xyz=123
&lt;br /&gt;example output: 
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&lt;project name='abc'&gt;
&lt;br /&gt;  &lt;methods&gt;
&lt;br /&gt;    &lt;method name='create'&gt;
&lt;br /&gt;      &lt;params&gt;
&lt;br /&gt;        &lt;param var='xyz'&gt;123&lt;/param&gt;
&lt;br /&gt;        &lt;param var='path'&gt;mdynarex&lt;/param&gt;
&lt;br /&gt;      &lt;/params&gt;
&lt;br /&gt;    &lt;/method&gt;
&lt;br /&gt;  &lt;/methods&gt;
&lt;br /&gt;&lt;/project&gt;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;CGI Reference: &lt;a href="http://www.rubycentral.com/book/web.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide&lt;/a&gt; [rubycentral.com]&lt;img src="http://feeds.dzone.com/~r/snippets/ruby/~4/365995106" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/365995106/5928</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/365995106/5928?</guid>
	<pubDate>Fri, 15 Aug 2008 15:32 GMT</pubDate>

</item>


</channel></rss>

