Hi, I'm Sam.

I like to create software, make music, and write about technology.

Find out more about me.

#ruby Posts

I Released an iPhone Push Notification Gem

Posted in development, gem, iphone, push, rails, and ruby

I forked a Rails plugin for sending push notifications with Rails awhile back for a client project. I've had a few people fork and add cool little fixes.

I've been wanting to play with GemCutter for awhile now. How hard could it be to release a Rails plugin as a gem, right? I sat down and rewatched a Railscast on how to build a gems with Jeweler and push it to GemCutter. It turns out, that was the easy part. GemCutter and Jeweler made it so easy to publish my plugin as gem.

So I know this is very backwards, but after it was on GemCutter I tested it my new gem. (Yes, many smacks on the hand for not writing test. I'm still learning all of that.) It turns out, that it was completely broken. I started to try and fix things, but ended up doing a complete rewrite.

Before, it would add the model in the plugin to your search path and then you would have to run a custom rake task in the plugin to add the table and migrate the database for the model. This always felt a little dirty, so I abstracted it all out. Now you can do:

class Device < ActiveRecord::Base
    acts_as_pushable
end

Pretty cool, right? Now sending is as easy as

d.send_notification :alert => "Hello world!"

(d being an instance of Device of course). You can read the full readme on GitHub. (Yes, another smack on the hand for no RDoc.)

Anyway, my code is probably very bad, as I'm pretty new to Ruby and Rails. I was very proud of myself for figuring it out. Check out the gem and let me know if you find it useful or stupid.

New Server Script

Posted in deployment, development, gem, nginx, postgres, rackspace, ruby, and shell-script

I've found myself setting up lots of servers over at RackSpace Cloud lately. It seemed look a good idea to automate everything instead of leaving it up to myself to remember everything you have to do each time.

I wrote this little shell script to make life easier. It installs everything I need to run a Ruby on Rails app in a matter of minutes. It's designed to work with Cent OS 5.4+. Here's the script's main tasks:

  • Install Git 1.7.1.1
  • Install Ruby 1.8.7p299
  • Install RubyGems 1.3.7
  • Install Passenger 2.2.15
  • Install Nginx 0.8.45 (with Passenger and SSL modules)
  • Install Postgres 8.4.4
  • Initialize Postgres
  • Install ImageMagick 6.6.3-0
  • Install Bundler (latest)
  • Open port 80 in iptables
  • Open port 443 in iptables
  • Start Nginx
  • Start Postgres

For me, that's enough to get the bare bones of an app running. The rest, I let Bundler handle for me. I use this on all of my servers that I manage.

Let's get started already

All you have to do to setup your new server is run the following one line:

$ wget http://gist.github.com/raw/314865/new_server.sh;chmod +x new_server.sh;./new_server.sh

You'll have to press y twice at the beginning when yum asks you to install some stuff. After that, you can let it do its thing.

Configuration

It's super easy to add a new Nginx virtual host. All you have to do is add a file with the .conf extension to /usr/local/nginx/conf/virtual_hosts/. Here's a good example virtual host. Of course, you can configure any of the installed stuff like you normally would. My nginx.conf just makes setting up virtual hosts easy. After you edit any of the Nginx configuration files, you'll want to run service nginx reload to apply your changes.

One more thing

There's also another little script that I run after the main one that is more specific to my needs. This little guy creates /var/www/, setups the proper permissions, and installs some database related gems. You can run it with the following one line:

$ wget http://gist.github.com/raw/314865/z_after.sh;chmod +x z_after.sh;./z_after.sh

Updated 07/13/10: Bumped versions of packages and fixed ImageMagick

How To Learn Rails

Posted in development, rails, ruby, and tutorial

A lot of people ask me how to learn Ruby on Rails. I usually forward this email I wrote awhile back to a friend. I figured it would probably be better to post it on my blog so more people can benefit from it.

Here's the email:

Hey man,

Rails is awesome. If you're not familiar with Ruby, I'd recommend getting acquainted with it first. I spend 2 hours reading a PDF online and then jumped in. The more you understand Ruby, the less magical Rails seems. The Ruby Programming Language is a great book. Matz (the creator of Ruby) is one of the authors. A lot of it is like "this is they way it is because when I designed it I though this". I liked it a lot.

The Poignant Guide is another really popular way to learn Ruby. There's a bit of history to it, but nevermind that for now.

Once you feel semi comfortable with Ruby (i.e. you can make a class that has methods, inheritance, control structures, etc), watch this 15 minute video. It will blow your mind. My next step was going back through it and trying to build it myself. This was really hard at first cause I had no idea what I was doing and didn't understand the syntax yet. Don't worry.

Try to write something simple in Rails. Maybe a blog or to do manager, etc. I learn by doing. This was the best for me. I've rewritten my blog at least 20 times. (By the way, it's open source on GitHub.)

The best resource I've found is Railscasts.com. They are really well done. Pretty much anything you could want to do, he has already covered. After I got several under my belt, I sorta "got it" and started really understanding what I was writing. #carlhuda on freenode (IRC channel) was also super helpful. @wycats (he's on the core team of Rails and jQuery) is in there and he's really great about helping new comers and teaching you stuff.

Ruby5 is also really great. It's a biweekly 5 minute podcast about news in the Ruby and Rails community. I've found lots of great stuff there.

Let me know if you need any help or have questions. I'd be happy to help.

Sam

More Stuff

I have several open source projects on my GitHub profile. My blog and Markdownr.com are two fairly straight forward Rails 3 projects.

I highly recommend learning Git. The Git documentation page will help a lot if you're trying to learn.