<?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>Anthony Lewis</title>
	<atom:link href="http://anthonylewis.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://anthonylewis.com</link>
	<description>Web Developer</description>
	<lastBuildDate>Mon, 25 Jul 2011 20:07:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Upcoming Events</title>
		<link>http://anthonylewis.com/2011/07/25/upcoming-events/</link>
		<comments>http://anthonylewis.com/2011/07/25/upcoming-events/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 20:07:51 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=516</guid>
		<description><![CDATA[It&#8217;s a great time to be a Rubyist in Austin. There are several upcoming events that I&#8217;m looking forward to attending. First, I&#8217;m giving a talk on Rails Application Security at the Austin on Rails meeting tomorrow evening. The meeting &#8230; <a href="http://anthonylewis.com/2011/07/25/upcoming-events/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a great time to be a Rubyist in Austin. There are
several upcoming events that I&#8217;m looking forward to
attending.</p>

<p>First, I&#8217;m giving a talk on Rails Application Security at 
the <a href="http://austinonrails.org/">Austin on Rails</a> meeting 
tomorrow evening. The meeting will also feature lightning 
talks covering a variety of beginner topics. Be sure to 
get there early.</p>

<p>Next, the second session of my GeekAustin Beginning Rails class
starts August 4.  The last class went really well and I&#8217;m looking
forward to teaching it again.  There are still seats available. 
For full details and to sign up, see the EventBright page at 
<a href="http://geekaustinrailsclass.eventbrite.com/">http://geekaustinrailsclass.eventbrite.com/</a>.</p>

<p>Then, the <a href="http://lonestarrubyconf.com/">Lone Star Ruby Conf</a> starts
on August 11 with a day of training.  Followed by the conference proper
on August 12 and 13.  The keynote speakers and session topics are all 
outstanding this year.  I am not speaking (this year), but I plan to 
be an active participant.</p>

<p>Hopefully I&#8217;ll see you around town.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/07/25/upcoming-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Your First Rails 3 Plugin</title>
		<link>http://anthonylewis.com/2011/05/13/your-first-rails-3-plugin/</link>
		<comments>http://anthonylewis.com/2011/05/13/your-first-rails-3-plugin/#comments</comments>
		<pubDate>Fri, 13 May 2011 19:32:16 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=496</guid>
		<description><![CDATA[On several different projects I have needed to find the newest and oldest record in certain models. The code to do this is pretty simple (especially in Rails 3). For example, here is a snippet to find the newest Post: &#8230; <a href="http://anthonylewis.com/2011/05/13/your-first-rails-3-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On several different projects I have needed to find the
newest and oldest record in certain models. The code
to do this is pretty simple (especially in Rails 3).</p>

<p>For example, here is a snippet to find the newest
Post:</p>

<pre><code>@post = Post.order('created_at DESC').first
</code></pre>

<p>We can make this even better by defining a method
in the Post class like this:</p>

<pre><code>def newest
  order('created_at DESC').first
end
</code></pre>

<p>With this new method our code becomes:</p>

<pre><code>@post = Post.newest
</code></pre>

<p>But now we&#8217;re faced with having to add that method
to lots of different models. This sounds like a perfect
job for a simple Rails 3 plugin.</p>

<h2>It&#8217;s Just a Gem</h2>

<p>Plugins in Rails 3 are created just like any other Ruby gem.
You can use whatever you like to create a new gem. Currently,
I&#8217;m using Bundler:</p>

<pre><code>bundle gem date_filter
</code></pre>

<p>This will create several files for you.  The most important
are the gemspec file and the contents of the lib directory.</p>

<h2>The gemspec</h2>

<p>Your gemspec file tells the world about your gem.  The first thing
you&#8217;ll want to do is edit this file and fill in the information marked
with TODO &#8211; your name, e-mail address, gem summary, and gem description.</p>

<p>The rest of the file should take care of itself. Note that the gem
version is stored in <code>date_filter/version.rb</code>.  Also, the list of files, 
test files, and executables is filled in automatically with git commands.</p>

<h2>The lib directory</h2>

<p>Inside the lib directory you&#8217;ll find a file called <code>date_filter.rb</code> and
a directory called <code>date_filter</code>.  A common practice is to keep the
contents of <code>date_filter.rb</code> pretty minimal.</p>

<p>Most of your actual code should go in separate files inside the date_filter
directory.  For example, here is my <code>date_filter.rb</code> file in its entirety:</p>

<pre><code>require 'active_record'
require File.expand_path('../date_filter/base', __FILE__)

ActiveRecord::Base.class_eval { include DateFilter::Base }
</code></pre>

<p>Note that I am requiring ActiveRecord here.  This is based on wycats advice -
<a href="http://weblog.rubyonrails.org/2010/2/9/plugin-authors-toward-a-better-future">If You Override Something, Require It</a></p>

<p>Next I require <code>base.rb</code> inside the <code>date_filter</code> directory. This contains
the source for the plugin.</p>

<p>The last line actually includes my code into <code>ActiveRecord::Base</code>.
Since I am adding class methods, I use <code>class_eval</code>.</p>

<h2>date_filter/base.rb</h2>

<p>Finally, here is the code for the plugin. It&#8217;s common practice to
separate class methods from instance methods in Rails plugins.  In this
case I have a separate module for the class methods.</p>

<p>When <code>DateFilter::Base</code> is included into <code>ActiveRecord::Base</code> the method
<code>included</code> is called.  This method then extends <code>ActiveRecord::Base</code> with
the methods in the <code>ClassMethods</code> module.</p>

<pre><code>module DateFilter
  module Base
    def self.included(base)
      base.send :extend, ClassMethods
    end

    module ClassMethods
      def newest
        order('created_at DESC').first
      end

      def oldest
        order('created_at ASC').first
      end
    end
  end
end
</code></pre>

<h2>Building and Installing</h2>

<p>If you made it this far, you&#8217;re ready to build and install your new
plugin. Bundler includes some rake tasks to help with this 
automatically. You can see these by running <code>rake -T</code></p>

<pre><code>rake build
rake install
rake release
</code></pre>

<p><code>rake build</code> will create a directory called pkg and build your new gem
into this directory. You can then install it with the gem command.</p>

<p><code>rake install</code> will build and install your new gem. To use it in a Rails
project, you will need to add it to your Gemfile and run <code>bundle install</code>.</p>

<h2>Releasing</h2>

<p>If you have an account at <a href="http://www.rubygems.org/">RubyGems.org</a> you can also
release your new gem. Check your profile page for your API key.  Include your API 
key in <code>~/.gem/credentials</code> as instructed, then run <code>rake release</code> to build and
upload your gem to the site.</p>

<p>This should get you started building your own Rails 3 plugins.  There are still a 
couple of pretty important things missing from this process &#8211; tests and documentation.
I plan to cover both of these in a future post.</p>

<p>The complete source for this plugin is up at <a href="https://github.com/anthonylewis/date_filter">github</a>.
The gem is also available at <a href="https://rubygems.org/gems/date_filter">RubyGems.org</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/05/13/your-first-rails-3-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GeekAustin Rails Class</title>
		<link>http://anthonylewis.com/2011/05/04/geekaustin-rails-class/</link>
		<comments>http://anthonylewis.com/2011/05/04/geekaustin-rails-class/#comments</comments>
		<pubDate>Wed, 04 May 2011 20:55:56 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=486</guid>
		<description><![CDATA[Starting on May 26 I will be teaching a beginning Ruby on Rails class for GeekAustin. The class will meet every Thursday from 7:00 PM &#8211; 9:00 PM at Cospace. I have developed a pretty ambitious course outline that I &#8230; <a href="http://anthonylewis.com/2011/05/04/geekaustin-rails-class/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Starting on May 26 I will be teaching a beginning Ruby on Rails class for
<a href="http://geekaustin.org/">GeekAustin</a>. The class will meet every Thursday from
7:00 PM &#8211; 9:00 PM at Cospace. I have developed a pretty ambitious course outline 
that I hope to cover in just 8 weeks.</p>

<p>The class is only $120 for 16 hours of training. The demand for Rails developers
is so high right now, this is a tiny investment to get started in a very hot market.
Someone who understands Rails inside and out can make that money back in a 
couple of hours (or less).</p>

<p>You will need to know some HTML and CSS.  Previous programming experience
would also be helpful, but is not required.  Prior experience with Ruby or Rails
is also not required.  I&#8217;m going to cover everything you need to know and provide
plenty of references to other materials to fill in any gaps in your knowledge.</p>

<p>I have really missed teaching, and I am so thankful for this opportunity. Developing 
the course outline and working on materials has been great. It seems like I&#8217;m
exercising a part of my brain I haven&#8217;t used in a while. I honestly can&#8217;t wait to get 
started teaching this.</p>

<p>This class will fill up fast.  For full details and to sign up, see the EventBright page at 
<a href="http://geekaustinrailsclass.eventbrite.com/">http://geekaustinrailsclass.eventbrite.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/05/04/geekaustin-rails-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Editing Multiple Records in Rails</title>
		<link>http://anthonylewis.com/2011/04/15/editing-multiple-records-in-rails/</link>
		<comments>http://anthonylewis.com/2011/04/15/editing-multiple-records-in-rails/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 16:50:42 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=444</guid>
		<description><![CDATA[I recently had a requirement to create a single form with data from more than one record. This is a simple request, but I had never done it before in Rails. After experimenting for a while (and reading quite a &#8230; <a href="http://anthonylewis.com/2011/04/15/editing-multiple-records-in-rails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently had a requirement to create a single form with data
from more than one record. This is a simple request, but I had
never done it before in Rails.</p>

<p>After experimenting for a while (and reading quite a few forum 
posts and StackOverflow answers) I came up with a working 
solution.  Hopefully this will save someone a little time in
the future.</p>

<p>First, let&#8217;s create a simple Rails app for managing users.  Each user
will have a first name, last name, and e-mail address. I&#8217;m using
scaffolding to generate the code.</p>

<pre><code>rails new multi_edit
cd multi_edit
bundle install
rails g scaffold User first_name:string last_name:string email:string
rake db:migrate
</code></pre>

<p>Next, add the new routes for editing all users. I tried to stick to the
RESTful convention with these. I&#8217;m just using the word &#8216;all&#8217; in place of the
:id.</p>

<pre><code>match 'users/all/edit' =&gt; 'users#edit_all', :as =&gt; :edit_all, :via =&gt; :get
match 'users/all' =&gt; 'users#update_all', :as =&gt; :update_all, :via =&gt; :put
</code></pre>

<p>Now, let&#8217;s add the <code>edit_all</code> method to our <code>UsersController</code> to get started.  The
only thing it needs to do is get all users.</p>

<pre><code>def edit_all
  @users = User.all
end
</code></pre>

<p>Next, we build the form for editing all users.</p>

<pre><code>&lt;%= form_for :user, :url =&gt; update_all_path, :html =&gt; { :method =&gt; :put } do %&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;th&gt;First Name&lt;/th&gt;
      &lt;th&gt;Last Name&lt;/th&gt;
      &lt;th&gt;E-Mail&lt;/th&gt;
    &lt;/tr&gt;
    &lt;% @users.each do |user| %&gt;
      &lt;%= fields_for "user[]", user do |user_fields| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= user_fields.text_field :first_name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= user_fields.text_field :last_name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= user_fields.email_field :email %&gt;&lt;/td&gt;
    &lt;/tr&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;/table&gt;

  &lt;div class="actions"&gt;
    &lt;%= submit_tag %&gt;
  &lt;/div&gt;
&lt;% end %&gt;
</code></pre>

<p>The interesting part of this code starts around line 9.  As expected, we
iterate over the users with <code>@users.each</code>.</p>

<p>The next line tells Rails to name the fields for each user with array notation. For example, 
<code>user_fields.text_field :first_name</code> will output a text 
field named <code>user[1][first_name]</code>.</p>

<p>The <code>params</code> hash will include a &#8216;user&#8217; key that contains a hash of information 
for each user.  The key for each of these hashes will be the user id.</p>

<p>Now that we know what the <code>params</code> will look like, it&#8217;s pretty straight-forward
to write the <code>update_all</code> method.</p>

<pre><code>def update_all
  params['user'].keys.each do |id|
    @user = User.find(id.to_i)
    @user.update_attributes(params['user'][id])
  end
  redirect_to(users_url)
end
</code></pre>

<p>We iterate over each key in <code>params['user']</code>, then find and update
the user associated with that id. I am leaving error checking as
an exercise for the reader&#8230;</p>

<p>The complete source code for this simple application is on my GitHub page at
<a href="https://github.com/anthonylewis/multi_edit">https://github.com/anthonylewis/multi_edit</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/04/15/editing-multiple-records-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Better Looking Terminal</title>
		<link>http://anthonylewis.com/2011/04/13/a-better-looking-terminal/</link>
		<comments>http://anthonylewis.com/2011/04/13/a-better-looking-terminal/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 00:28:45 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=420</guid>
		<description><![CDATA[I spend a lot of time in the Mac OS X Terminal.app. Unfortunately, the built-in color schemes are all pretty terrible. I can&#8217;t stand to look at any of them for more than a few minutes. So, one of the &#8230; <a href="http://anthonylewis.com/2011/04/13/a-better-looking-terminal/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I spend a lot of time in the Mac OS X Terminal.app. Unfortunately, the built-in
color schemes are all pretty terrible. I can&#8217;t stand to look at any of them
for more than a few minutes.</p>

<p>So, one of the first changes I make on a new Mac is to update the default
color scheme.  It&#8217;s surprisingly easy to make something much easier on the
eyes than the Basic theme.  Here&#8217;s how I do it.</p>

<ul>
<li>On the Terminal menu, click Preferences</li>
<li>Click Settings on the toolbar</li>
</ul>

<p><img src="http://anthonylewis.com/wp-content/uploads/2011/04/term-1.png" class="size-auto aligncenter"></p>

<ul>
<li>Make sure the Basic theme is selected</li>
<li>Now click the Gear, then Duplicate Settings</li>
<li><p>Type the name Metal</p></li>
<li><p>On the Text tab, change the Text and Bold Text colors to Mercury and the Selection color to Steel</p></li>
</ul>

<p><img src="http://anthonylewis.com/wp-content/uploads/2011/04/term-2.png" class="size-auto aligncenter"></p>

<ul>
<li>On the Window tab, change the Background Color to Lead</li>
</ul>

<p><img src="http://anthonylewis.com/wp-content/uploads/2011/04/term-3.png" class="size-auto aligncenter"></p>

<ul>
<li>Finally, click the Default button</li>
</ul>

<p>When you open a new terminal, you should see something that looks
more like this:</p>

<p><img src="http://anthonylewis.com/wp-content/uploads/2011/04/term-4.png" class="size-auto aligncenter"></p>

<p>Now that is something I can work with for a while.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/04/13/a-better-looking-terminal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails on Rackspace Cloud &#8211; Part 2</title>
		<link>http://anthonylewis.com/2011/04/01/rails-on-rackspace-cloud-2/</link>
		<comments>http://anthonylewis.com/2011/04/01/rails-on-rackspace-cloud-2/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 20:55:13 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=402</guid>
		<description><![CDATA[Part one covered setting up a new Ubuntu server and getting Ruby 1.9.2 up and running with rvm. Now we will finish the setup with a Firewall, MySQL, Apache2, and Phusion Passenger. Setting Up the Firewall Before we install the &#8230; <a href="http://anthonylewis.com/2011/04/01/rails-on-rackspace-cloud-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Part one covered setting up a new Ubuntu server and
getting Ruby 1.9.2 up and running with rvm.</p>

<p>Now we will finish the setup with a Firewall, MySQL,
Apache2, and Phusion Passenger.</p>

<h2>Setting Up the Firewall</h2>

<p>Before we install the database and other services,
lets get the firewall set up.  On Ubuntu, I like to
use the Uncomplicated Firewall &#8211; UFW.</p>

<pre><code>sudo apt-get install ufw
</code></pre>

<p>That will install the firewall.  Now set the defaults,
add some rules, and enable the firewall.</p>

<pre><code>sudo ufw default deny
sudo ufw allow ssh
sudo ufw allow http
sudo ufw enable
</code></pre>

<p>To see the current rules, you can check the status.</p>

<pre><code>sudo ufw status
</code></pre>

<p>Now the only ports open to the internet are SSH (22) and
HTTP (80).</p>

<h2>Installing MySQL</h2>

<p>Now that we&#8217;re a little more protected from the outside world,
lets install the database.</p>

<pre><code>sudo apt-get install mysql-server mysql-client libmysqlclient-dev
</code></pre>

<p>And install the mysql2 gem.</p>

<pre><code>gem install mysql2
</code></pre>

<p>Remember to &#8216;rvm use 1.9.2&#8242; if you didn&#8217;t set it as the default.</p>

<h2>Installing Apache and Passenger</h2>

<p>The database is ready to go at this point, now we need a web server.</p>

<pre><code>sudo apt-get install apache2 libcurl4-openssl-dev apache2-prefork-dev \
                     libapr1-dev libaprutil1-dev
</code></pre>

<p>That will install Apache2 and the extra development packages needed
by Passenger.  Now install the Passenger gem.</p>

<pre><code>gem install passenger
</code></pre>

<p>Now we&#8217;re ready to install the Passenger Apache2 module.  Note, this
doesn&#8217;t actually install anything.  It just builds the module and
gives instructions for updating the configuration.</p>

<pre><code>passenger-install-apache2-module
</code></pre>

<p>Now we need to create a new module load file to tell Apache about
Passenger.</p>

<pre><code>sudo vim /etc/apache2/mods-available/passenger.load
</code></pre>

<p>Here are the contents of passenger.load.  The first two lines should
be all on one line.</p>

<pre><code>LoadModule passenger_module /home/testapp/.rvm/gems/ruby-1.9.2-p180/gems/
    passenger-3.0.5/ext/apache2/mod_passenger.so
PassengerRoot /home/testapp/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5
PassengerRuby /home/testapp/.rvm/wrappers/ruby-1.9.2-p180/ruby
</code></pre>

<p>If we did everything right, we can enable the Passenger module now.</p>

<pre><code>sudo a2enmod passenger
</code></pre>

<p>Now we need to set up our virtual host under sites-available.</p>

<pre><code>sudo vim /etc/apache2/sites-available/testapp
</code></pre>

<p>Here are the contents of that file.  You will obviously need to 
replace the xx.xx.xx.xx with the IP address of your server.</p>

<pre><code>&lt;VirtualHost *:80&gt;
  ServerName xx.xx.xx.xx
  DocumentRoot /home/testapp/testapp/public
  &lt;Directory /home/testapp/testapp/public&gt;
    AllowOverride all
    Options -MultiViews
  &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</code></pre>

<p>Disable the default web site and enable our new test application.</p>

<pre><code>sudo a2dissite default
sudo a2ensite testapp
</code></pre>

<h2>Setup a simple Rails App</h2>

<p>Create a simple Rails application in your home directory just
to make sure everything is working.</p>

<pre><code>rails new ~/testapp
cd ~/testapp
bundle install
rake db:migrate RAILS_ENV=production
</code></pre>

<p>Finally, reload the Apache2 configuration.</p>

<pre><code>sudo /etc/init.d/apache2 reload
</code></pre>

<p>Go to your server&#8217;s IP address in your browser and you should
see the default Ruby on Rails index page.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/04/01/rails-on-rackspace-cloud-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Rails on Rackspace Cloud &#8211; Part 1</title>
		<link>http://anthonylewis.com/2011/03/30/rails-on-rackspace-cloud-1/</link>
		<comments>http://anthonylewis.com/2011/03/30/rails-on-rackspace-cloud-1/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 16:17:46 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=372</guid>
		<description><![CDATA[This is my recipe for starting up a new Rackspace Cloud Server and getting it set up for hosting Ruby on Rails applications. Part one will take you from a new server to a working installation of RVM and Ruby. &#8230; <a href="http://anthonylewis.com/2011/03/30/rails-on-rackspace-cloud-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is my recipe for starting up a new
Rackspace Cloud Server and getting it 
set up for hosting Ruby on Rails applications.</p>

<p>Part one will take you from a new server to
a working installation of RVM and Ruby.  Part
two will cover installing MySQL, Apache and Phusion
Passenger.</p>

<p>If you don&#8217;t already have an account with
Rackspace, I encourage you to go over to 
<a href="http://www.rackspace.com/cloud/">http://www.rackspace.com/cloud/</a>
and check them out.</p>

<p>I prefer Ubuntu Linux, so let&#8217;s start with
that.</p>

<h2>Starting a New Server</h2>

<p>First, sign in to your Cloud Control Panel,
then click Hosting, Cloud Servers.</p>

<p><a href="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-01.png"><img src="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-01-300x211.png" class="size-auto aligncenter"></a></p>

<p>Click the Add Server button, scroll down to
Ubuntu 10.10 (Maverick Meerkat), then click
the Select button.</p>

<p><a href="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-02.png"><img src="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-02-300x211.png" class="size-auto aligncenter"></a></p>

<p>Now type in a server name and select how much
RAM you&#8217;ll need, then click the Create Server
button.</p>

<p><a href="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-03.png"><img src="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-03-300x211.png" class="size-auto aligncenter"></a></p>

<p>After a short wait, your new server should be 
ready to go.  You will receive an e-mail with
your new server&#8217;s IP address and root password.</p>

<p><a href="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-04.png"><img src="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-04-300x211.png" class="size-auto aligncenter"></a></p>

<p>Connect to the IP Address with SSH, log in
as root with the password provided, and lets
get started.</p>

<p><a href="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-05.png"><img src="http://anthonylewis.com/wp-content/uploads/2011/03/rsc-05-300x209.png" class="size-auto aligncenter"></a></p>

<h2>Initial Login</h2>

<p>The first thing you should do at this point is
change the root password.</p>

<pre><code>passwd
</code></pre>

<p>Type in your new root password twice.  Now
let&#8217;s add a new user account.</p>

<pre><code>adduser testapp
</code></pre>

<p>Type in the information for your new user.<br />
Add the new user to the sudo group so you can
execute commands as root.</p>

<pre><code>adduser testapp sudo
</code></pre>

<p>Now that we have a new account to use, we need
to deny the root user access via ssh.  This
will stop people trying to brute-force our root 
password.</p>

<pre><code>vim /etc/ssh/sshd_config
</code></pre>

<p>Find the line that contains &#8220;PermitRootLogin&#8221; and
change the value from &#8220;yes&#8221; to &#8220;no&#8221;.  Restart the
ssh server when you&#8217;re done.</p>

<pre><code>/etc/init.d/ssh restart
</code></pre>

<p>We have set up a new user account with sudo access
and denied root login via ssh, so let&#8217;s log out
for now.</p>

<pre><code>logout
</code></pre>

<h2>New User Login</h2>

<p>Now reconnect with your new user account and let&#8217;s
make sure all of our software is up to date.</p>

<pre><code>sudo apt-get update
sudo apt-get upgrade
</code></pre>

<p>This should be really fast.  Rackspace&#8217;s internet
connection puts mine to shame.</p>

<h2>Installing RVM</h2>

<p>Next, we&#8217;ll install git so we can install rvm.</p>

<pre><code>sudo apt-get install git-core
</code></pre>

<p>Now copy and paste the command below to install rvm:</p>

<pre><code>bash &lt; &lt;(curl -s https://rvm.beginrescueend.com/install/rvm)
</code></pre>

<p>Once this finishes, you will see a lot of instructions for
setting up rvm as well as a nice &#8220;Thank you&#8221; from Wayne.</p>

<p>rvm automatically creates a file called .bash_profile in
our home directory.  This causes bash to skip loading
the regular .profile.</p>

<p>The only thing in .bash_profile is rvm configuration,
so let&#8217;s just delete it&#8230;</p>

<pre><code>rm .bash_profile
</code></pre>

<p>and add the configuration to .profile instead.</p>

<pre><code>vim ~/.profile
</code></pre>

<p>Add this line at the very bottom:</p>

<pre><code>[[ -s "$HOME/.rvm/scripts/rvm" ]] &amp;&amp; . "$HOME/.rvm/scripts/rvm"
</code></pre>

<p>Finally &#8220;source&#8221; your .profile file so the change will take effect.</p>

<pre><code>source ~/.profile
</code></pre>

<h2>Installing Dependencies</h2>

<p>Now we&#8217;ll install the rest of the packages we need to
build ruby.  RVM can provide us a list of needed software.</p>

<pre><code>rvm notes
</code></pre>

<p>Look at the line starting with &#8220;For Ruby&#8221; to get a list of
dependencies.</p>

<pre><code>sudo apt-get install build-essential bison openssl libreadline6 \
                     libreadline6-dev curl git-core zlib1g zlib1g-dev \
                     libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev \
                     sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev \
                     ncurses-dev
</code></pre>

<p>Again, this shouldn&#8217;t take long.</p>

<h2>Installing Ruby</h2>

<p>We&#8217;re finally ready to install Ruby.</p>

<pre><code>rvm install 1.9.2
</code></pre>

<p>We have Ruby 1.9.2 now, let&#8217;s use it.</p>

<pre><code>rvm --default use 1.9.2
</code></pre>

<h2>Installing Rails</h2>

<p>Now that we have Ruby set up, we&#8217;re ready to install Rails.  It should be as simple as this:</p>

<pre><code>gem install rails
</code></pre>

<p>Note that when you&#8217;re using RVM, you do not have to precede this with &#8220;sudo&#8221;.</p>

<p>After another short wait, you should be all set.</p>

<p>Verify that the ruby and rails commands work as
expected.  In part 2 we&#8217;ll setup MySQL, Apache and 
Passenger.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/03/30/rails-on-rackspace-cloud-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Happy Pi Day</title>
		<link>http://anthonylewis.com/2011/03/14/happy-pi-day/</link>
		<comments>http://anthonylewis.com/2011/03/14/happy-pi-day/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 19:58:54 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=362</guid>
		<description><![CDATA[In honor of Pi Day, I thought I would write a little Ruby script to generate pi to a few decimal places. A quick trip to the Wikipedia page for pi turned up many formulas for computing pi. I took &#8230; <a href="http://anthonylewis.com/2011/03/14/happy-pi-day/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In honor of <a href="http://www.piday.org/">Pi Day</a>, I 
thought I would write a little Ruby script to 
generate pi to a few decimal places.</p>

<p>A quick trip to the <a href="http://en.wikipedia.org/wiki/Pi#Computation_in_the_computer_age">Wikipedia page for pi</a>
turned up many formulas for computing pi. I took
one of the easy ones and set about writing some
code.</p>

<p>Most of the formulas involve the factorial function.
Unfortunately, Ruby doesn&#8217;t have a built in method for computing
the factorial. After a little Googling, I found a nice, functional example
at the <a href="http://rosettacode.org/wiki/Factorial#Ruby">Rosetta Code wiki</a>.</p>

<p>Putting that all together, I ended up with this:</p>

<pre><code>def fact(n)
  (1..n).reduce(1, :*)
end

sum = 0.0

(0..8).each do |n|
  a = fact(2 * n) ** 3.0
  b = 42.0 * n + 5.0
  c = fact(n) ** 6.0
  d = 16.0 ** (3.0 * n + 1.0)

  sum += (a * b) / (c * d)

  puts 1.0 / sum
end
</code></pre>

<p>Note that I&#8217;m only iterating 8 times. On my PC, that
gives pi out to 15 decimal places which is all of the
precision available in a floating point number.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/03/14/happy-pi-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploring ODBC with Ruby DBI</title>
		<link>http://anthonylewis.com/2011/03/08/exploring-odbc-with-ruby-dbi/</link>
		<comments>http://anthonylewis.com/2011/03/08/exploring-odbc-with-ruby-dbi/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 15:15:21 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=359</guid>
		<description><![CDATA[I recently found myself in an interesting situation. I needed to extract data from a database. Unfortunately, all I had to work with was an ODBC name. If this were a MySQL database, I would have used the command-line interface &#8230; <a href="http://anthonylewis.com/2011/03/08/exploring-odbc-with-ruby-dbi/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently found myself in an interesting situation.
I needed to extract data from a database. Unfortunately, 
all I had to work with was an ODBC name.</p>

<p>If this were a MySQL database, I would have used the 
command-line interface to check out the structure and
see what was available.</p>

<p>In this case, I didn&#8217;t have a command-line interface.
What I did have was Ruby and irb.  Here&#8217;s how I got the
job done. </p>

<h2>Required Gems</h2>

<p>Three gems are required to make this work.  First, we
need dbi and a driver (or dbd).  The dbd-odbc driver also
requires the ruby-odbc gem.</p>

<pre><code>dbi
dbd-odbc
ruby-odbc
</code></pre>

<p>In order to install ruby-odbc on Windows, you will need to
compile it from source.  I used the excellent 
<a href="https://github.com/oneclick/rubyinstaller/wiki/Development-Kit">DevKit</a>
provided with the <a href="http://rubyinstaller.org/">RubyInstaller</a> and 
had no problems.</p>

<h2>Connect</h2>

<p>Let&#8217;s assume that the ODBC Data Source Name is &#8220;TestData&#8221;.
First, we require the DBI library, then connect to the data
source.</p>

<pre><code>require 'DBI'

dbh = DBI.connect("DBI:ODBC:TestData")
</code></pre>

<p>You can also pass a username and password after the connection
string if required.</p>

<h2>List and Describe Tables</h2>

<p>Now that we have a connection, let&#8217;s see what tables are
available.</p>

<pre><code>dbh.tables
</code></pre>

<p>My test database only has one table.  It&#8217;s called &#8220;Table1&#8243;.
Let&#8217;s list the columns in this table.</p>

<pre><code>dbh.columns "Table1"
</code></pre>

<p>This will tell us the type of data stored in each field in
addition to the name.</p>

<h2>Query For Data</h2>

<p>In order to see the actual data, we can prepare and execute
an SQL query.  In this case, the separate prepare and execute
is not really necessary, but it&#8217;s a good habit to get into.</p>

<pre><code>sth = dbh.prepare("SELECT * FROM Table1")
sth.execute

row = sth.fetch
</code></pre>

<p>The fetch method will return a row of data at a time until 
there is not more data.  At that point it returns nil. Calling
fetch again after it returns nil will result in an exception.</p>

<h2>A Complete Example</h2>

<p>Here&#8217;s a simple example of connecting to the ODBC database and
displaying all of the data in a tab separated format.</p>

<pre><code>require 'DBI'

dbh = DBI.connect("DBI:ODBC:TestData")

sth = dbh.prepare("SELECT * FROM Table1")
sth.execute

puts sth.column_names.join("\t")

while row = sth.fetch
  puts row.join("\t")
end

sth.finish
dbh.disconnect
</code></pre>

<p>You could also require the CSV class and use it to generate comma-separated
output. </p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/03/08/exploring-odbc-with-ruby-dbi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>To Hex and Back (With Ruby)</title>
		<link>http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/</link>
		<comments>http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 20:39:11 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://anthonylewis.com/?p=346</guid>
		<description><![CDATA[I am a big fan of plain text. It is easy to view and easy to edit. Unfortunately, it is sometimes necessary to work with binary files and data. Ruby&#8217;s inspect method does a decent job of showing the contents &#8230; <a href="http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am a big fan of plain text. It is easy to view and easy to
edit. Unfortunately, it is sometimes necessary to work with 
binary files and data.</p>

<p>Ruby&#8217;s inspect method does a decent job of showing the 
contents of a binary string, but sometimes I need something a 
little more powerful.</p>

<p>Back in the day I would use a hex editor to open binary files and
decipher their contents. I don&#8217;t have a need for a hex editor anymore,
but I would like to occasionally view binary data in the same format.</p>

<p>After some intense Googling and Ruby doc reading, I came up with a 
few methods to convert a binary string to hex, and convert a 
string of hex back to the original binary.</p>

<h2>Bin to Hex</h2>

<p>To convert a string to it hex representation, first take each byte,
convert it to hex, then join all of the hex digits back together.</p>

<pre><code>def bin_to_hex(s)
  s.each_byte.map { |b| b.to_s(16) }.join
end
</code></pre>

<p>If you like spaces between the hex digits, change join to join(&#8216; &#8216;)</p>

<h2>Hex to Bin</h2>

<p>Converting the string of hex digits back to binary is just as easy.
Take the hex digits two at a time (since each byte can range from
00 to FF), convert the digits to a character, and join them back together.</p>

<pre><code>def hex_to_bin(s)
  s.scan(/../).map { |x| x.hex.chr }.join
end
</code></pre>

<p>If you find yourself using these frequently in a project, you could
add the methods to the String class.</p>

<h2>TIMTOWTDI</h2>

<p>Of course, there is more than one way to do this.  Ruby also provides
the handy pack and unpack methods for Arrays and Strings respectively.</p>

<p>These are a little more cryptic since you need to know the meaning
of the format string to understand what&#8217;s going on.</p>

<pre><code>def bin_to_hex(s)
  s.unpack('H*').first
end

def hex_to_bin(s)
  s.scan(/../).map { |x| x.hex }.pack('c*')
end
</code></pre>

<p>Check the Ruby documentation for <a href="http://www.ruby-doc.org/core/classes/Array.html" title="Ruby Array">Array</a> and 
<a href="http://www.ruby-doc.org/core/classes/String.html" title="String">String</a> for a complete explanation of pack and unpack.</p>

<h2>Examples</h2>

<p>Here&#8217;s the output of a quick IRB session to demonstrate how this works.</p>

<pre><code>irb(main):001:0&gt; s = "Hello, World!"
=&gt; "Hello, World!"

irb(main):002:0&gt; s = s.each_byte.map { |b| b.to_s(16) }.join
=&gt; "48656c6c6f2c20576f726c6421"

irb(main):003:0&gt; s = s.scan(/../).map { |x| x.hex.chr }.join
=&gt; "Hello, World!"
</code></pre>

<p>These methods are no replacement for a hex editor, but if you need to 
check an encryption key or some other short string of binary, they can
be just the thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonylewis.com/2011/02/09/to-hex-and-back-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

