Menu:

Post History:

Animals Matter to Me

Posts for April, 2010

Testing generated XML with Elementor

I wrote this post quite a while ago but never published it, I guess because I was planning to add more details. Development on Elementor seems to have stalled so maybe there are better ways out there now, but I still like how it works.



During a recent project to learn about the Sinatra framework, I needed a way to test the generated RSS feed. While looking into a Sinatra equivalent of has_tag from RSpec on Rails, I came across Elementor. This turned out to work well for both the view and RSS feed testing.

Instead of describing the CSS selectors in each test, with Elementor one can give them meaningful names in a before :each block and the tests can just refer to the names. Here is an example:

@page = elements(:from => :do_get, :as => :xml) do |tag|
      tag.items 'item'
      tag.guids 'guid'
      tag.links 'item/link'
    end
  end
  
  def do_get
    get "/feed", 'feeds' => ["http://feed1.test/posts.xml",
"http://feed2.test/posts.xml"]
    response.body
  end


With the descriptions of each tag out of the way, the tests can be written very clearly:

it "should repeat the items' link field in the combined feed" do
    @page.links.size.should == 3
    @page.links[0].inner_text.should == "LINK"
  end


0 comments

Moving a Sinatra app to Heroku

I haven’t written any posts here in a long time. About a year ago I had a small problem: removing duplicate posts from two similar but different RSS feeds. I wrote a small Sinatra app to solve the problem, hosted it on my Slicehost slice and added the feed to my Google Reader. Over a year later my small problem has been successfully solved by my small app, and it continues to do its job daily.

In the interest of learning more about Heroku, I thought that this simple app with no database would be a good place to start.

I first signed up for an account on Heroku but got sidetracked that day and didn’t move any further towards setting up that first app. A few days later I got a reminder email from Heroku, which from most sites would annoy me, but since I really did want to set up the app, and the email included a handy “look how simple it is” set of instructions, it was actually ok.

Since my app was already in a git repository (and on github) all I had to do was install the heroku gem and follow the simple instructions.

I created a .gems file so that Heroku knows what gems are necessary to run the application. The gem file for Amalgamator looks like this:

feedzirra --version 0.0.23
rack --version 1.1.0


Running ‘heroku create’ requests the account details and then creates the app instance with a temporary name.

jeffd@jeffd-netbook:~/programming/amalgamator$ heroku create
Enter your Heroku credentials.
Email: jeff@dallien.net
Password: 
Uploading ssh public key /home/jeffd/.ssh/id_rsa.pub
Creating cold-winter-66....... done
Created http://cold-winter-66.heroku.com/ | git@heroku.com:cold-winter-66.git
Git remote heroku added


My app was given an initial generated name of “cold-winter-66”, and although for a fake generated name it isn’t that bad, I renamed the app to amalgamator, making the URL http://amalgamator.heroku.com/.

I ran ‘git push heroku master’, which did a lot of the hard work, including installing the gems:

jeffd@jeffd-netbook:~/programming/amalgamator$ git push heroku master
The authenticity of host 'heroku.com (75.101.163.44)' can't be established.
RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'heroku.com,75.101.163.44' (RSA) to the list of known
hosts.
Counting objects: 199, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (123/123), done.
Writing objects: 100% (199/199), 138.63 KiB, done.
Total 199 (delta 37), reused 195 (delta 36)

-----> Heroku receiving push

-----> Installing gem feedzirra 0.0.23 from http://gemcutter.org, 
http://gems.rubyforge.org
       Building native extensions.  This could take a while...
       Building native extensions.  This could take a while...
       Successfully installed nokogiri-1.4.1
       Successfully installed sax-machine-0.0.15
       Successfully installed curb-0.7.1
       Successfully installed loofah-0.4.7
       Successfully installed feedzirra-0.0.23
       5 gems installed

-----> Installing gem rack 1.1.0 from http://gemcutter.org, http://gems.rubyforge.org
       Successfully installed rack-1.1.0
       1 gem installed

-----> Sinatra app detected
       Compiled slug size is 1.4MB
-----> Launching...... done
       http://amalgamator.heroku.com deployed to Heroku


Without any messing around or configuration, the app ran right away. And a Heroku fan was instantly created.

Now that the site is up and running elsewhere, I disabled the copy running on my Slicehost server so I can use those resources for something else. To keep any links or search engine results that may already exist working, I set up some Apache RewriteRules to redirect requests from the jeff.dallien.net app to the Heroku one.

PassengerHighPerformance off

RewriteEngine on
RewriteCond %{QUERY_STRING} ^feeds=(.*)&feeds=(.*)$
RewriteRule ^/amalgamator/feed http://amalgamator.heroku.com/feed?feeds[]=%1&feeds[]
=%2 [R=301,L,NE]
RewriteRule ^/amalgamator(/?) http://amalgamator.heroku.com/ [R=301,L]


Turning the PassengerHighPerformance option off was necessary to prevent the RewriteRules from being ignored. This requirement is explained in the Passenger documentation. The version of Amalgamator I’ve setup on Heroku is running on a newer version of Sinatra than I had last deployed to the old location, and that upgrade required a slight change to the format of the parameters the application accepts. The first RewriteRule takes an old style request, redirects it and adjusts the parameters simultaneously. The second rule redirects any other requests for the main page over to the Heroku app.

Now that I’ve seen how easy it is to get apps up and running on Heroku I’m thinking about the next one I’m going to create. I’ve got an idea for one using gameday_api, a ruby library for accessing MLB scores and statistics by Timothy Fisher.

0 comments