<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Biodegradable Geek &#187; Snippets</title>
	<atom:link href="http://biodegradablegeek.com/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://biodegradablegeek.com</link>
	<description></description>
	<lastBuildDate>Tue, 22 Jun 2010 21:52:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Calculate Your GPA Using this Bash Script</title>
		<link>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/</link>
		<comments>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/#comments</comments>
		<pubDate>Thu, 21 May 2009 10:18:18 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Code example]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=433</guid>
		<description><![CDATA[This Bash script is used to calculate your Grade Point Average (GPA) on the command line. Usage might not be intuitive. Please see the usage function or just run the script without passing it any arguments. The gval function should be edited to reflect your own region or university. It has been written and tested [...]]]></description>
			<content:encoded><![CDATA[<p>This Bash script is used to calculate your Grade Point Average (GPA) on the command line. Usage might not be intuitive. Please see the usage function or just run the script without passing it any arguments.</p>
<p>The <strong>gval</strong> function should be edited to reflect your own region or university. It has been written and tested on Bash 3.2.48.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/sh
#
# Bash GPA calculator
#
# Isam | r0cketjump@yahoo.com | biodegradablegeek.com
# 05/21/2009 - Just another 4 AM project

function usage {
  echo -e &quot;\nBASH GPA Calculator&quot;
  echo
  echo -e &quot;\tAccepts an even # of arguments in the form of C G C G C G ...&quot;
  echo -e &quot;\t (C = number of credits, G = grade for the course)&quot;
  echo
  echo -e &quot;\tExample: You got a B+ in a 4 credit course, &quot;
  echo -e &quot;\t         an A in a 3 credit course, etc..&quot;
  echo
  echo -e &quot;\tUSAGE: $0 4 B+ 3 A 3 F 3 B-&quot;
  echo
  echo &quot;Acceptable grades are A B C D F WU (eq to F)&quot;
  echo
}

function calc {
  echo `echo &quot;scale=3; $1&quot; | bc`
}

function gval {
  grade=`echo &quot;$1&quot; | tr [a-z] [A-Z]`
  case $grade in
    A+ ) echo '4.3';;
    A ) echo '4';;
    A- ) echo '3.7';;

    B+ ) echo '3.3';;
    B ) echo '3.00';;
    B- ) echo '2.7';;

    C+ ) echo '2.3';;
    C ) echo '2.0';;
    C- ) echo '1.7';;

    D+ ) echo '1.3';;
    D ) echo '1.0';;
    D- ) echo '0.7';;

    F ) echo '0';;
    WF ) echo '0';;
    WU ) echo '0';;
  esac
}

# check # of arguments. is it even?
let MOD=$#%2
if [ ! $MOD -eq 0 ]; then
  usage
  exit
elif [ $# -eq 0 ]; then
  usage
  exit
fi

args=($@)
n=${#args[@]}

points=0
credits=0

for ((i=0;i&lt;$n-1;i+=2)); do
  k=${i}

  creds=${args[$k]}
  cgrade=${args[$k+1]}

  # convert cgrade (C-) to a number
  grade=`gval $cgrade`
  pts=`calc $grade*$creds`

  echo &quot;$creds * $cgrade ($grade) = $pts&quot;

  points=`calc $points+$pts`
  credits=`calc $credits+$creds`
done

gpa=`calc $points/$credits`
echo &quot;------------&quot;
echo &quot;Total points  = $points&quot;
echo &quot;Total credits = $credits&quot;
echo &quot;------------&quot;
echo &quot;** GPA (pts/crd) = $gpa&quot;
echo &quot;------------&quot;
</pre>
<p>(Script uses <strong>bc</strong> as the calculator. Change that in the calc function if you need to.)</p>
<p>I&#8217;ll never get used to Bash&#8217;s ugly ass syntax.  &#8230; esac?</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Maintain Static Sites with Git &amp; Jekyll</title>
		<link>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/</link>
		<comments>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 04:10:00 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=371</guid>
		<description><![CDATA[Static sites in this context just means non-database driven sites. Your static site can be an elaborate PHP script or just a few markup and image files. For this I am using Jekyll &#8211; A neat Ruby gem that makes your static sites dynamic. It lets you create layouts and embed custom variables in your [...]]]></description>
			<content:encoded><![CDATA[<p>Static sites in this context just means non-database driven sites. Your static site can be an elaborate PHP script or just a few markup and image files. For this I am using <strong><a href="http://github.com/mojombo/jekyll/tree/master">Jekyll</a> &#8211; A neat Ruby gem that makes your static sites dynamic.</strong> It lets you create layouts and embed custom variables in your HTML (this is a &#8220;prototype&#8221; of the site). </p>
<p>Jekyll tackles all the nuisances involved in creating static pages (I used to add just enough PHP to make a layout). It works by running your prototype through some parsers and outputs plain static HTML/XML (RSS feeds) etc. It&#8217;s perfect for lightweight sites that would be impractical on WordPress, like a few static pages of information, landing pages, portfolio/resume pages, and parked domains. </p>
<p>Git takes care of keeping your development (local) and production (remote) environments synced. Git might be a little confusing if you&#8217;re learning it with the mindset that it works like Subversion. </p>
<p><strong>I&#8217;ll update this post when the guide is done. For now, the following will assume you&#8217;re familiar with Jekyll (or at least have an empty file in the prototype directory) and git. This Bash script simplifies creating the remote git repository:</strong></p>
<p>** please read through the code and make sure you know what this does, and what you&#8217;re doing. As of now, this is bias towards my own Apache/vhost setup. It&#8217;s trivial to edit for your specific needs. <strong>You&#8217;re using this at your own risk</strong>.</p>
<p>(<a href="http://code.biodegradablegeek.com/repogen.sh" target="_blank">direct link &#8211; repogen.sh</a>)</p>
<pre lang="bash">
#!/bin/sh
#
# 04/01/2009 | http://biodegradablegeek.com | GPL
#
# You should be in site (NOT public) root (be in same dir as public/ log/ etc)
# proto/ is created and will house the jekyll prototype
# public/ will be the generated static site
# the public/ folder will be REMOVED and regenerated on every push
# 

if [ -z "$1" ]; then
  echo "Usage: ./repogen.sh domain.comn"
  exit
fi

# optional. will make it easier to copy/paste cmd to clone repo
SSHURL="ssh.domain.com"
URL="$1"

echo "** creating tmp repo"
mkdir proto
cd proto
git init
touch INITIAL
git add INITIAL
git commit -a -m "Initial Commit"

echo "** creating bare repo"
cd ..
git clone --bare proto proto.git
mv proto proto.old
git clone proto.git
rm -rf proto.old

echo "** generating hook"
HOOK=proto.git/hooks/post-update

mv $HOOK /tmp
echo '#!/bin/sh' >> $HOOK
echo '# To enable this hook, make this file executable by "chmod +x post-update".' >> $HOOK
echo '#exec git-update-server-info' >> $HOOK
echo '' >> $HOOK
echo '' >> $HOOK
echo 'URL='"$URL" >> $HOOK
echo 'PROTO="/home/$USER/www/$URL/proto"' >> $HOOK
echo 'PUBLIC="/home/$USER/www/$URL/public"' >> $HOOK
echo  '' >> $HOOK
echo 'export GIT_DIR="$PROTO/.git"' >> $HOOK
echo 'pushd $PROTO > /dev/null' >> $HOOK
echo 'git pull' >> $HOOK
echo 'popd > /dev/null' >> $HOOK
echo '' >> $HOOK
echo "echo -----------------------------" >> $HOOK
echo "echo '** Pushing changes to '$URL" >> $HOOK
echo "echo '** Moving current public to /tmp'" >> $HOOK
echo 'mv "$PUBLIC" "/tmp/'$URL'public-`date '+%m%d%Y'`"' >> $HOOK
echo 'echo "** Generating new public"' >> $HOOK
echo 'jekyll "$PROTO" "$PUBLIC"' >> $HOOK

echo "** enabling hook"
chmod a+x $HOOK 

echo "** clone repo on local machina. example:"
echo "git clone ssh://$USER@$SSHURL/~$USER/www/$SSHURL/proto.git"
</pre>
<p><strong>Usage</strong></p>
<p>Your site structure might be different. <strong>repogen.sh</strong> is made by pasting the above code in a new file, and then chmod a+x to make it executable. This should be done on the remote server.</p>
<pre lang="bash">
cd www/domain.com/

ls
public/ private/ log/ cgi-bin/

./repogen.sh domain.com
</pre>
<p>Now on your local machine, clone the new repo, move your files in, and push:</p>
<pre lang="bash">
git clone ssh://[username]@ssh.domain.com/~[username]/www/domain.com/proto.git
cd proto/
cat "hello, world" > index.htm
git add index.htm
git commit -a -m 'first local commit'
git push
</pre>
<p>After you push your changes, the post-update hook will delete the public/ directory (the root of the site). This dir and its contents are automatically generated and will get wiped out on EVERY push. Keep this in mind. All your changes and content should reside in proto/. </p>
<p>The proto/ repo will pull in the new changes, and then Jekyll will be invoked to generate the updated site in public/ from the prototype.</p>
<p>Should you need to edit it, the <strong>post-update hook</strong> is in the bare git repo (proto.git/hooks/)</p>
<p>Thanks to the authors in the posts below for sharing ideas. I first read this git method on dmiessler&#8217;s site. </p>
<p><strong>Resources:</strong><br />
<a href="http://dmiessler.com/blog/using-git-to-maintain-your-website">dmiessler.com &#8211; using git to maintain static pages</a><br />
<a href="http://toroid.org/ams/git-website-howto">toroid.org &#8211; using git to manage a web site</a><br />
<a href="http://github.com/mojombo/jekyll/tree/master">Jekyll @ GitHub</a><br />
<a href="http://media.pragprog.com/titles/tsgit/chap-005-extract.html">git info</a><br />
<a href="http://www.nardol.org/2009/2/19/git-basics-reversing-the-git-sucks-effect">more git info</a></p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Refactoring Tip: Eliminating Model.find(params[:id]) Duplication</title>
		<link>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/</link>
		<comments>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 15:59:36 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[refactoring tips]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=293</guid>
		<description><![CDATA[In a controller, you&#8217;ll commonly have a method that requires you have an instance variable containing the object you&#8217;re working with. An example would be the show, edit, update, and destroy methods (REST). To eliminate having find(params[:id]) in multiple methods, you can use before_filter, like this: class Admin::PostsController < Admin::ApplicationController before_filter :find_post, :only => [:show, [...]]]></description>
			<content:encoded><![CDATA[<p>In a controller, you&#8217;ll commonly have a method that requires you have an instance variable containing the object you&#8217;re working with. An example would be the <strong>show</strong>, <strong>edit</strong>, <strong>update</strong>, and <strong>destroy</strong> methods (REST).</p>
<p>To eliminate having find(params[:id]) in multiple methods, you can use before_filter, like this:</p>
<pre lang="rails">
class Admin::PostsController < Admin::ApplicationController
  before_filter :find_post, :only => [:show, :edit, :update, :destroy]
  rescue_from(ActiveRecord::RecordNotFound) { |e| render :text => "
<h2>Post not found</h2>

" }

  def index
    @posts = Post.find(:all)
  end

  def show
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new
  end

  def edit
  end

  def update
  end

  def destroy
  end

protected
  def find_post(id = params[:id])
    @post = Post.find(id)
  end
end
</pre>
<p>(Thanks Jon)</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Javascript to Populate Forms During Development</title>
		<link>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/</link>
		<comments>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 15:43:41 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=291</guid>
		<description><![CDATA[During development, working with forms quickly gets annoying because you have to constantly fill in each field, sometimes with unique info. One way around this is to write a little Javascript code that just populates the fields. I use something like this on the bottom of the form. I had jQuery no-conflict mode on in [...]]]></description>
			<content:encoded><![CDATA[<p>During development, working with forms quickly gets annoying because you have to constantly fill in each field, sometimes with unique info. One way around this is to write a little Javascript code that just populates the fields. I use something like this on the bottom of the form. I had jQuery no-conflict mode on in this case. In your app you might be able to get away replacing _j() with $():</p>
<pre lang="rails">
<% if ENV['RAILS_ENV']=='development' -%>
<!-- Generate random field data -->
<script lang="text/javascript">
  jQuery(function() {
    if (typeof(_j)=='undefined') _j = jQuery;
    function randstr() {
      return Math.floor(Math.random()*99999)
    }

    _j('#name').val('Chuck Norris');
    _j('#company_name').val('Roundhouse LLC');
    _j('#email').val('moo'+randstr()+'@yawhoo.com');
    _j('#login').val('user'+randstr());
    _j('#password').val('admin');
    _j('#password_confirmation').val('admin');
  })
</script>
<% end -%>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to POST Form Data Using Ruby</title>
		<link>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/</link>
		<comments>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 15:43:09 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scraping]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/2008/04/24/how-to-post-form-data-using-ruby/</guid>
		<description><![CDATA[POSTing data on web forms is essential for writing tools and services that interact with resources already available on the web. You can grab information from your Gmail account, add a new thread to a forum from your own app, etc. The following is a brief example on how this can be done in Ruby [...]]]></description>
			<content:encoded><![CDATA[<p><strong>POST</strong>ing data on web forms is essential for writing tools and services that interact with resources already available on the web. You can grab information from your Gmail account, add a new thread to a forum from your own app, etc. </p>
<p>The following is a brief example on how this can be done in Ruby using <a href="http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html" target="_blank">Net::HTTP</a>and <a href="http://www.interlacken.com/webdbdev/ch05/formpost.asp" target="_blank">this POST form example</a>.</p>
<p>Looking at the source (interlacken.com/webdbdev/ch05/formpost.asp):</p>
<pre class="brush: xml; title: ; notranslate">
&lt;form method=&quot;POST&quot; action=&quot;formpost.asp&quot;&gt;
&lt;p&gt;&lt;input type=&quot;text&quot; name=&quot;box1″ size=&quot;20″ value=&quot;&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;button1″&gt;&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>We see two attributes are sent to the formpost.asp script when the user hits the submit button: A textbox named <strong>box1</strong> and the value of the submit button, named <strong>Submit</strong>. If this form used a GET method, we would just fetch the URL postfixed with (for example) <strong>?box1=our+text+here</strong>. Fortunately, Ruby&#8217;s Net::HTTP makes posting data just as easy.</p>
<p>The Ruby code: </p>
<pre class="brush: ruby; title: ; notranslate">
#!/usr/bin/ruby

require &quot;uri&quot;
require &quot;net/http&quot;

params = {'box1′ =&gt; 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1′ =&gt; 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

# Uncomment this if you want output in a file
# File.open('out.htm', 'w') { |f| f.write x.body }
</pre>
<p>Sending the value of button1 is optional in this case, but sometimes this value is checked in the server side script. One example is when the coder wants to find out if the form has been submitted &#8211; as opposed to it being the user&#8217;s first visit to the form &#8211; without creating a hidden attribute to send along w/ the other form fields. Besides, there&#8217;s no harm in sending a few more bytes.</p>
<p>If you&#8217;re curious about URI.parse, it simply makes the URI easier to work with by separating and classifying each of its attributes, effectively letting the methods in Net::HTTP do their sole job only, instead of having to analyze and parse the URL. More info on this in the <a href="http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html#M009241" target="_blank">Ruby doc</a>.</p>
<p>Assuming no errors, running this example (<em>ruby postpost</em> or <em>chmod a+x postpost.rb; ./postpost.rb</em>) yields:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;form method=&quot;POST&quot; action=&quot;formpost.asp&quot;&gt;
&lt;p&gt;&lt;input type=&quot;text&quot; name=&quot;box1″ size=&quot;20″ value=&quot;NOTHING IS LESS
IMPORTANT THAN WHICH FORK YOU USE. ETIQUETTE IS THE
SCIENCE OF LIVING. IT EMBRACES EVERYTHING. IT IS ETHICS.
IT IS HONOR. -EMILY POST&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;button1″&gt;&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>In practice, you might want to use a more specialized library to handle what you&#8217;re doing. Be sure to check out <a href="http://mechanize.rubyforge.org/mechanize/" target="_blank">Mechanize</a> and <a href="http://github.com/adamwiggins/rest-client/tree/master" target="_blank">Rest-client</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

