The new RubyURL API

We’ve just deployed the initial version of an API for RubyURL. It makes it really easy to create RubyURLs and is now open to the public. Should it end up being abused, we’ll consider introducing an API KEY for authenticating and tracking abuse.

In the meantime, you can now start to use the RubyURL API.

For example, the following…


$ curl  -i \ 
        -X POST \
        -H 'Content-Type: application/xml' \
        -d '<link><website_url>http://github.com/robbyrussell</website_url></link>' \
        http://rubyurl.com/api/links  

...would return the following response.

I’ll be updating the ShortURL gem in the coming days (unless someone else wants to patch it first wink) to take advantage of new API, versus how it’s currently creating RubyURLs.

You can see the code & changes for this new API on the RubyURL github site.

Update with JSON

I took a little time today to update the API and extend it to support JSON. So… you can now use the RubyURL API to generate RubyURLs via JSON. (see commits)

Enjoy! If you’re using RubyURL via the new API, I’d love to hear about it. :-)

Scripted GUI Testing with Ruby – Ruby on Rails Podcast

Ian Dees talks about his new book on testing GUI applications. His company uses Ruby to test handheld devices.

Sponsor

Scripted GUI Testing with Ruby – Ruby on Rails Podcast

Ian Dees talks about his new book on testing GUI applications. His company uses Ruby to test handheld devices.

Sponsor

Flash Message Conductor

Do you find yourself copying and pasting the same code from Rails application-to-application as new projects start? Our team has a handful of projects in development right now and we notice that some of these reusable components tend to get out of sync when we bounce between projects. So, we’re making an effort to spot these and are creating a handful of plugins so that we can keep them updated between projects. (I’m sure that a lot of you do this as well)

In an effort to share some of our patterns, we’ll try to release them into the wild for others to use and perhaps if you have better patterns to offer, we’re always interested in improving our approach.

Introducing Flash Message Conductor

Over the years, our designers and developers have approached the management of flash messages several different ways. In Rails, the default way to add something to a flash message is to do something like this in your controller.

flash[:message] = "You have successfully signed in to your account."

What we began doing a while back is to create a few controller helper methods:

add_message( "You have successfully signed in to your account." )
add_notice( "You've Got Mail!" )
add_error( "Oops! Something got fucked up!" )

Really, nothing too crazy here, just a pattern that our developers have preferred to managing our application’s flash messages.

Okay, so now for the part of the puzzle that we aimed to make consistent across our projects. Rendering flash messages would usually result in several lines of conditionals in our application layout to check if the flash had any values assigned to it. As we worked with our HTML/CSS designers to define a consistent pattern, we moved our code into a helper for rendering flash messages.

With Flash Message Conductor, we just need to pop in the following into our application layout.

<%= render_flash_messages %>

If we had called add_message, it’d render the following:

<div id="flash_messages">
  <p class="message">You have successfully done XYZ...</p>
</div>

Or, should you have called add_error, it’d render the following:

<div id="flash_messages">
  <p class="error">Oops! Something went bonkers!</p>
</div>

What we’ve done here is defined a consistent pattern for our designers and developers to follow. We’ll always have a div container that will use a p tag to display the flash messages with a CSS class value that maps to the type of flash message that we’re displaying. This makes it easier for us to reuse the same flash message styling (and tweak if necessary), but we know that it’ll produce the same HTML across our applications.

Installing Flash Message Conductor

Like most modern Rails applications, you can install with:


script/plugin install git://github.com/planetargon/flash-message-conductor.git

Then all of our helper methods will be available to your application. We’ve also included an example CSS file, which you’ll find in the plugin directory.

Sample output:

flash message area
Uploaded with plasq’s Skitch!

Anyhow, we’ve posted the plugin up on GitHub for you all to use, if you’d like to adopt a similar approach. If you have any alternative patterns that has helped your team, do share and I’m looking forward to sharing some more of ours in the near future.

For more information, visit the Flash Message Conductor plugin on GitHub.

If anything, hopefully this will inspire those of you who find yourself copying/pasting artifacts from application-to-application to extract that code into it’s own reusable plugin. 🙂

Git: Push it! (real good)

After wrestling with some git-remote-branching-merge-problems… I remembered this song…

If you’re using git, you might add this to your [alias] section in .gitconfig.

(notice the up-on-this alias)

Flex from Ford, Power by Microsoft.

20080828_FlexByFord.png
Is Ford is jumping on the Flex bandwagon? And powered by Microsoft?

20080828_poweredByMicrosoft.png

Check out the Advanced ActiveRecord Envycast

I just went through the first screencast from the Rails Envy guys’. I wasn’t sure if I would liked the fact that they used a green screen approach, but thought I would support their effort as I really like their weekly podcast. Guess what? It’s a really good screen cast, very informative, high quality and well done. Wait, there is more, they have a cool Cheat Sheet that comes with the download. Well worth the money, go check it out.

20080829_EnvyCast.jpg

Flex on Rails: the book – release date?

As you may know we started a while back with Tony on concocting some of our experiments, war stories, successes, discoveries, real life project experience on using Flex with Rails into a book titled “Flex on Rails: Building Rich Internet Applications With Adobe Flex 3.0 and Rails 2.0”. We are now “code complete”, wrote all the chapters and are in what I suspect (/hope) is the final editing stage. Yea!… well, that may end up being a longer process than I imagined and was told that it may take up to four month once the final manuscript is accepted. Nevertheless it was funny seeing our book appear on different book selling website as pre-order. Being my first (published) book I was curious and did some googling to see when it will be really ready. It’s pretty ‘funny’ to see how the dates are moving as the days go buy, but in short here is a quick overview on when the book will be released according to google…note..we are not totally done yet…so I wouldn’t really announce a date yet, but if google says…then it must be true 🙂 !!! Well, I was really hoping it hits the shelves earlier than some of these:

Capistrano 2.5.0

Capistrano 2.5.0 is now available! You can read the full release announcement on the capify.org news blog.

SQLite3-Ruby 1.2.3

SQLite3-Ruby version 1.2.3 is now available. It is a maintenance release, fixing just a few things:

  • The permissions on the database.rb and translator.rb files in 1.2.2 were incorrect, resulting in broken sqlite3-ruby installations for many *nix users. This is now fixed.
  • A few more Ruby 1.9 compatibility issues were patched.
  • Some optimizations were applied to speed up iterating over result sets.

To install or upgrade:


  gem install sqlite3-ruby

Thanks!

Flash Player 10 Mobile for iPhone?

I was viewing the video of the Day 1 Keynote by Mark Anders at 360Flex where he made a reference to a mobile Flash application build in Flex. Mark just skinned a desktop app which turned in into a very iPhone like application which just happen to have the iPhone screen dimensions and behavior. It’s 56 minutes in the presentation. Check it out and let me know what you think. I’ve included a video extract here after (without the sound):


That’s when he changed the skin:

20080827_flexiphoneskin.png

That would be cool if we could soon start coding in Flex for the iPhone.

Enjoy!
Daniel.

Cap 1.4.1? Go 1.4.2. Now.

Are you currently using Capistrano 1.4.1? If so, drop everything (I mean it, do this RIGHT NOW) and install Capistrano 1.4.2.

Why, you ask?

Capistrano 1.4.1 will work just fine, right up until you decide you want to experiment with Capistrano 2. When you do that, Cap 2.3+ will install net-ssh 2.x, which kills Capistrano 1.4.1 in all kinds of really obscure ways.

The good news is that Cap 1.4.2 is completely compatible with Cap 1.4.1. It adds no new features. The only “bug” it fixes is that if you ever install net-ssh 2.x, Cap 1.4.2 will still happily continue to work.

Ultimately, an upgrade to Cap 2 is recommended, but I understand it’s not feasible for everyone. So, if you’re one of those who can’t go cap2 yet, please please please PLEASE PLEASE FOR THE LOVE OF ALL THAT IS HOLY upgrade to Cap 1.4.2. It’ll make your life easier, and it’ll make my life easier (because I won’t have to keep troubleshooting the same issues over and over). Thanks!

#124 Beta Invitations

You know those invitation systems where a given user can invite a number of other people to join? That’s what I show you how to make in this episode.

#124 Beta Invitations

You know those invitation systems where a given user can invite a number of other people to join? That’s what I show you how to make in this episode.

What are all the Rails Date Formats?

Ever forget what all the Rails defined Date/DateTime/Time#strftime formats are? Or forget what ones you added to the project yourself?

Ala rake routes comes rake date_formats:
Sample output from a Rails 2.1 app:


Date
====
db:‘%Y-%m-%d’ 2008-08-20
long_ordinal:‘&proc’ August 20th, 2008
long:‘%B %e, %Y’ August 20, 2008
rfc822:‘%e %b %Y’ 20 Aug 2008
number:‘%Y%m%d’ 20080820
short:‘%e %b’ 20 Aug DateTime
====
db:‘%Y-%m-%d’ 2008-08-20 16:56:21
long_ordinal:‘&proc’ August 20th, 2008 16:56
long:‘%B %e, %Y’ August 20, 2008 16:56
rfc822:‘%e %b %Y’ Wed, 20 Aug 2008 16:56:21 -0600
number:‘%Y%m%d’ 20080820165621
short:‘%e %b’ 20 Aug 16:56 Time
====
db:‘%Y-%m-%d %H:%M:%S’ 2008-08-20 16:56:21
long_ordinal:‘&proc’ August 20th, 2008 16:56
long:‘%B %d, %Y %H:%M’ August 20, 2008 16:56
rfc822:‘%a, %d %b %Y %H:%M:%S %z’ Wed, 20 Aug 2008 16:56:21 -0600
short:‘%d %b %H:%M’ 20 Aug 16:56
number:‘%Y%m%d%H%M%S’ 20080820165621
time:‘%H:%M’ 16:56

RSpec: It Should Behave Like

I was going through an older project of ours and cleaning up some specs and noticed how often we were doing the same thing in several places. When we started the project, we didn’t get the benefits of shared groups. Now that we have some time to go through and update some of our older specs, I’ve been trying to take advantage of the features currently available in RSpec. One feature that I haven’t seen a lot of mention of by people is shared groups, so I thought I’d take a few minutes to write up a quick intro to using it.

To pick some low-hanging fruit, let’s take an all-too-familiar method, which you might be familiar with… login_required. Sound familiar? Have you found yourself stubbing login_required over and over throughout your specs?

describe Admin::DohickiesController, 'index' do

  before( :each ) do
    controller.stub!( :login_required )
    Dohicky.should_receive( :paginate ).and_return( Array.new )
    get :index
  end

 ...
end

If you’re requiring that a user should be logged in when interacting with most of the application (as in the case of an administration section/namespace), you might want to consolidate some of your work into one shared specification group. The basic premise behind this is that you can write a typical describe block and load it into any other spec groups that you need. For example, in our case, we’ll need to stub login_required in several places. We can set this up in one shared group and reference it wherever necessary.

For example, here is what we’ll start off with.

describe "an admin user is signed in" do
  before( :each ) do
    controller.stub!( :login_required )
  end
end

describe Admin::DohickiesController, 'index' do
  ...

However, the new describe block isn’t accessible from the block at the bottom of the example… yet. To do this, we just need to pass the option: :shared => true as you’ll see in the following example.

describe "an admin user is signed in", :shared => true do
  before( :each ) do
    controller.stub!( :login_required )
  end
end

Great, now we can reference it by referring to it with: it_should_behave_like SharedGroupName. In our example above, this would look like:

describe "an admin user is signed in" do
  before( :each ) do
    controller.stub!( :login_required )
  end
end

describe Admin::DohickiesController, 'index' do
  it_should_behave_like "an admin user is signed in"

  before( :each ) do
    Dohicky.should_receive( :paginate ).and_return( Array.new )
    get :index
  end

 ...
end

describe Admin::DohickiesController, 'new' do
  it_should_behave_like "an admin user is signed in"

  before( :each ) do
    @dohicky = mock_model( Dohicky )
    Dohicky.should_receive( :new ).and_return( @dohicky )
    get :new
  end

  ...

That’s it! Pretty simple, eh? We can now reference this shared group in any describe blocks that we want to. A benefit to this approach is that we can make change the authentication system (say, we decide to switch it entirely and/or even just change method names, set any other prerequisites necessary when an admin is signed in), we’ll have a single place to change in our specs. (tip: you can put these in your spec_helper file)

You can learn more about it_should_behave_like and other helpful features on the RSpec documentation site.

If you have any suggestions on better ways of handling things like this, please follow up and share your solutions. I’m always looking to sharpen my tools. 🙂

Update

In response, Bryan Helmkamp suggests that a better solution is to define a method in our specs like, for example: build_mock_user_and_login. then calling it in our before(:each). So, maybe the approach above isn’t the most ideal method but I did wantt o draw some attention to it_should_behave_like. I suppose that I need a better example.. another post, perhaps? 🙂

Also, Ed Spencer has posted an article titled, DRYing up your CRUD controller RSpecs, which will introduce you mor to it_should_behave_like.

Thanks for feedback people!

Related Posts

360Flex sessions video – Day one

<iframe src=“http://216.92.94.180/1733261879.html” scrolling=“auto” frameborder=“no” align=“center” height = “220px” width = "250px">

Sessions Posted:

  • TicketMaster Kiosk by Kevin Fauth
  • Flex Accessibility by Giorgio Natili
  • Reading the Flex source code by Jonathan Branam
  • Project Workflow by Axel Jensen
  • Creating Reusable Components by Ben Clinkbeard

From Ted’s blog.

Nested to_xml for awesome_nested_set

I was updating an example build using the better_nested_set to use the awesome_nested_set. One thing that I didn’t find in awesome_nested_set are some helper methods that are returning a full tree of the nested set as one XML document. With better nested set I could do

Category.result_to_attributes_xml(Category.root.ancestors)

So I have added the following full_xml method to my nested active record to recurse all the children. Note that the full_method calls the full_method on the children passing along the xml builder that is created by the to_xml method and passed as the xml variable to block, thus building a nested XML document.

class Category < ActiveRecord::Base
belongs_to :parent, :class_name => “Category”
acts_as_nested_set
def full_xml(builder=nil)
to_xml(:builder => builder, :skip_instruct => true) do |xml|
children.each { |child| child.full_xml(xml) }
end
end

end

Obviously it would be nice that the awesome_nested_set provides such a method.

So let’s assume I create the following nested structure:

Category.transaction do
root = Category.create(:name => “Main Category”)
cameras = Category.create(:name => “Cameras & Photo”)
cameras.move_to_child_of(root)
Category.create(:name => “Bags”, :qty_in_stock => 2).move_to_child_of(cameras)
Category.create(:name => “Accessories”, :qty_in_stock => 12).move_to_child_of(cameras)
Category.create(:name => “Analog Cameras”, :qty_in_stock => 0).move_to_child_of(cameras)
Category.create(:name => “Digital Cameras”, :qty_in_stock => 5).move_to_child_of(cameras)
phones = Category.create(:name => “Cell Phones”)
phones.move_to_child_of(root)
Category.create(:name => “Accessories”, :qty_in_stock => 8).move_to_child_of(phones)
Category.create(:name => “Phones”, :qty_in_stock => 20).move_to_child_of(phones)
Category.create(:name => “Prepaid Cards”, :qty_in_stock => 3).move_to_child_of(phones)
dvds = Category.create(:name => “Dvds”)
dvds.move_to_child_of(root)
Category.create(:name => “Blueray”, :qty_in_stock => 10).move_to_child_of(dvds)
Category.create(:name => “HD DVD”, :qty_in_stock => 0).move_to_child_of(dvds)
Category.create(:name => “DVD”, :qty_in_stock => 100).move_to_child_of(dvds)
end

I can now get the whole nested structure in one go:

Category.roots.first.full_xml

And get the following XML in return.

<typo:code lang=“xml>

”datetime">2008-08-18T14:46:07Z

1
1
Main Category
28
2008-08-18T14:46:07Z

2008-08-18T14:46:07Z

11
2
Dvds 1
9
2008-08-18T14:46:07Z

2008-08-18T14:46:07Z

14
3
DVD 11 100
4
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

13
5
HD DVD 11 0
6
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

12
7
Blueray 11 10
8
2008-08-18T14:46:07Z



2008-08-18T14:46:07Z

7
10
Cell Phones 1
17
2008-08-18T14:46:07Z

2008-08-18T14:46:07Z

10
11
Prepaid Cards 7 3
12
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

9
13
Phones 7 20
14
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

8
15
Accessories 7 8
16
2008-08-18T14:46:07Z



2008-08-18T14:46:07Z

2
18
Cameras & Photo 1
27
2008-08-18T14:46:07Z

2008-08-18T14:46:07Z

6
19
Digital Cameras 2 5
20
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

5
21
Analog Cameras 2 0
22
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

4
23
Accessories 2 12
24
2008-08-18T14:46:07Z


2008-08-18T14:46:07Z

3
25
Bags 2 2
26
2008-08-18T14:46:07Z




How do you deal with that situation?

Things.app syncs with the iPhone!

Awesome. Things 1.1 for the iPhone was just pushed to the Apple iTunes Store, which means… I can finally sync my Things.app with my iPhone!

I’ve been using Things for quite a while to manage my life (work and personal) and bringing this to my phone definitely makes my day.

For more information about Things, visit the following sites:

I’ll post a review in the coming days as I get a chance to play with it. Just wanted to share the news. 🙂

#123 Subdomains

Learn how to unleash the full potential of subdomains with the subdomain-fu Rails plugin in this episode.