This week I was lucky enough to spend two days at the Merb Sprint. At the sprint I spent some time testing the install and setup process for the Merb Stack and merb_auth (the merb authentication gem). Dan Neighman (aka hassox) spent some time with me going over the merb_auth process and this is my attempt to pass the information on.
This walkthrough will take you through the steps to get a Merb app up and running with a protected resource. It assumes you are running Merb 0.9.9 or greater with the Merb Stack (merb-core, merb-more, and datamapper).
Lets start at the beginning and create an app
$ merb-gen app authentication_app
$ cd authentication_app
By default merb-gen app will create a user model for you in app/models/user.rb. The model is there, but we still need to create the table in the database and add a user to authenticate with. Lets do that now.
$ rake db:auto_migrate
To create our first user we can drop into Merb’s irb console mode with merb -i. Like Rails’ script/console, merb -i gives you access to your models and other classes.
$ merb -i
u = User.new
u.login = 'joe'
u.password = u.password_confirmation = 'password'
u.save
exit
In order to test authentication we need something to protect. Any controller would work, but lets create a resource.
$ merb-gen resource secret
We will need to add a route to the resource also
# router.rb
Merb::Router.prepare do
resources :secrets
...
end
To protect a controller with authentication we can use the before filter ensure_authenticated
# app/controllers/secrets.rb
class Secrets < Application
before :ensure_authenticated
...
end
Everything should now be setup in our app. Lets give it a run through.
First you need to start you merb server. Make sure you are in your merb app’s root directory.
$ merb
Now you can test access to your resource is denied before authentication.
http://localhost:4000/secrets
Login in and try to access the recourse again.
http://localhost:4000/login
http://localhost:4000/secrets
You should now be able to access the resource. Awesome.
Tags:
Merb, merbcamp, merb_auth10 Comments
Today I stumbled upon the first program I ever wrote. It’s an interesting story looking back twenty years later.
When I was somewhere between 8-10 years old my uncle gave my family our first computer, a Coleco Adam. Along with the computer we were given the now classic book BASIC Computer Games. No one in my family new anything about compilers, programming, or computers in general, but we were excited about the idea of creating something out of nothing so decided to give one of these programs a go.
We decided on Bunny and began to slowly peck out the source code into the text editor. We saved the file, exited the editor, and then stared blankly at the screen as we realized we didn’t know what to do next. Interpreters and compilers were obviously never discussed and after our futile attempts of running our program failed, I decided to take a different approach.
What was I trying to accomplish? What were the outcomes I needed? The answer was simple- an ASCII image of a bunny. So that’s what I created, and ASCII image of a bunny. For the rest of the afternoon I typed out the bunny by hand and by using brute force I eventually accomplished the goal.
This retrospective reminds me that when things “just aren’t working”, sometimes I need to take a step back and reevaluate my methods. I may have an Occam’s razor type revelation that could satisfy requirements without significant compromises.
Keep it old school and tell the world your first programming experience in a comment below.
Tags:
Agile, personal, retrospective3 Comments
Recently, Merb has been getting a lot of attention. With significant changes leading up to the 1.0 release, and now with it’s own conference, the Merb community needs a centralized place where relevant information can be found. Say hello to Merb Overheard.
Merb Overheard follows the planet-type aggregation sites similar to Planet Ruby on Rails and Planet PHP. It follows a list of relevant blogs and aggregates those articles into one place.
When I first decided to build Merb Overheard I was originally planning on writing it in Merb of course, but I starting to realize all the battles I was going to have ahead of me. Caching and pagination of no database objects were not something I really wanted to deal with, so I decided to try something different. Instead of trying to shove the aggregator into a framework, why not just build out the pages statically?
Merb Overhead is just a plain old ruby script that gets ran every 30 minutes from a cron task. Each time it is ran, all the pages are recreated using Erubius. This script, weighing in at a whopping 8 lines of code, implements two classes in a library I wrote called Pluto. The class for the feed aggregator is only 30 lines. The class to generate the view is only 45 lines.
The code is released on Github so go nuts creating collecting all your Furby Fan Club sites or whatever else strikes your fancy.
Tags:
launch, Merb, merb overheard, pluto5 Comments