about img
blogger img

buchos posts img

Corys posts image

UnderPaidLoveMonkis posts img

scotts posts image

Ruby's Archive

Brobinius

Scott Rippee @ 1:52 pm Sunday, April 20th, 2008

I was going to put in the time and get a forked ruby out to fill in some feature gaps, but the maker of tender love beat me to it.

Brobinus adds some much needed features like Object#tase! and brobinius –screencast my_code.rb for youtube integration. link

new clarinet +1, RubyConf 0

Scott Rippee @ 6:20 pm Thursday, November 15th, 2007

Ruby is not some bachelor’s party with a foxy lady in a sherlock holmes hat. Enjoy the clarinet _why. link

Test 93 overwriting neuron 23441952 axon 3

Scott Rippee @ 11:42 pm Sunday, October 28th, 2007

and my hand slaps my face proving that the hand can still successfully slap the face. If only there were not so many commits kicking off the slap suite.

Dstamat at ELC provides a whirl wind of the many options that have come to existence in Ruby automated test land.

As I started to see my test suite slow down, Mocha to the rescue (FlexMock would have done the job too). My controller tests became focused, and my models were simply mocked and stubbed away from them completely. What if I wanted to test my views ?
Enter Selenium and Watir. Now I could even test to make sure my blind_downs were working when a user clicked on my links from browser X, Y, Z. Half way into setting up Selenium, RSpec hit the scene.

Test::Unit, Mocha, Rspec, RCov, Heckle. Throw ci_reporter in the mix for testing in continous integration (CI) setups such as pulse. Rock and roll for what rails has pulled off in the testing arena and more rock and roll considering the benefits gained by all ruby development.

There are so many amazing tools are available that incubate quality and live in complete automation. The learning curve is there, time to implement is there, but the pay off is huge and shouldn’t be ignored.

The vague and partially relevant point of this post is to illustrate the agile software development (r)evolution that passionate Rubyists have manifested in such a short amount of time. The creation, porting, adaption and adoption of these testing tools / frameworks alone should clearly exemplify this.

It’s an exciting time to be writing software

dstamat, I must agree. I’m completely enthralled.

Pick Your RoR HTML Parsing Poison

Scott Rippee @ 4:07 pm Thursday, July 5th, 2007

Ruby HTML parsing has been keeping me quite entertained frustrated lately, so I thought I'd share some thoughts. There are a couple of instance in your rails app when you'll want to parse HTML

  1. Automated functional/controller testing
  2. Screen scraping

Functional Testing

The standard method of verifying aspects of resulting HTML in your functional test is HTML::Selector. It's simple, powerful, and baked in. Agile Rails 2nd does a great job of explaining how it's used in functional tests.

RUBY:
  1. def test_add_no_name
  2.   post :add, :color => { :name => '', :hex => '#123456' }
  3.   assert_template 'add'
  4.   assert_select "div[id=errorExplanation]" do
  5.     assert_select "ul" do
  6.       assert_select "li", 'Name is not present'
  7.     end
  8.   end
  9. end

 

Scraping

Several options are available, but oh so popular is why's Hpricot. It's fast and enjoyable (although I experienced no joy while learning how to use it =) It also happens to be used in some of the other scraping/navigating libraries (WWW::Mechanize [rdoc] and scRUBYt!).

 

Some Thoughts...

So if your just concerned with testing use HTML::Selector and the built in asserts. If you have to do very basic screen scraping I would also suggest going with HTML::Selector (as long as speed is not an issue and the scraping is basic) with open-uri or curb for fetching the pages.

For more serious screen scraping bust out Hpricot and if you need to navigate pages via automation use WWW::Mechanize (Mechanize also uses Hpricot so all of that Hpricot knowledge you've absorbed is directly applicable. Mechanize is Hpricot with the ability to click). Don't worry about scRUBYt!. It's more of a pain to figure out than it's worth (but maybe I'm wrong about it. Any good examples/write-ups?).

Hpricot with CSS selector

RUBY:
  1. divs = (doc/"div[@style*='font-weight:'][text()*='$'").inner_html
  2. divs.each do |div|
  3.   if div =~ /\$[0-9]?[0-9]\.[0-9][0-9]/
  4.     self.price = div.to_s.sub('$', '')
  5.   end
  6. end

Hpricot search with XPath

RUBY:
  1. require 'hpricot'
  2. require 'open-uri'
  3. doc = Hpricot(URI.parse("http://google.com/").read)
  4.  
  5. doc.search("/html/body//p")
  6. doc.search("//p")
  7. doc.search("//p/a")
  8. doc.search("//a[@src]")
  9. doc.search("//a[@src='google.com']")

Using Mechanize to do a search on google

RUBY:
  1. require 'rubygems'
  2. require 'mechanize'
  3.  
  4. agent = WWW::Mechanize.new
  5. agent.user_agent_alias = 'Mac Safari'
  6. page = agent.get("http://www.google.com/")
  7. search_form = page.forms.with.name("f").first
  8. search_form.q = "Hello"
  9. search_results = agent.submit(search_form)
  10. puts search_results.body

Note that Hpricot lets you use a CSS method of selecting and an XPATH method. Use XPATH if you already have experience otherwise the CSS method is more intuitive.

If you go with XPATH grab the XPather firefox plugin and use it with the DOM Inspector. Also, it works with the firebug firefox plugin. I'm still in awe that it worked when I tried. :) To do this, use firebug to "inspect", choose an element, right click on the page and select "Show in XPather". XPather will open with the selected element locked and loaded.

Finally, if your a Hpricot wiz forget about HTML::Selector and put Hpricot to work for view validation in your functional tests. See this great write up, Testing your Rails views with Hpricot, which demonstrates this elegant solution.

RUBY:
  1. assert_equal "My Funky Website", tag('title')
  2. assert_equal 20, tags('div.boxout').size
  3. assert_equal 'visible', element('div#site_container').attributes['class']

 

Rails app in a single executalbe??

Scott Rippee @ 7:45 pm Monday, May 28th, 2007

moab Petroglyph src wikipedia
This is quite impressive. It documents how to wrap up a whole rails app into one executalbe.

Driving this magic is RubyScript2Exe, which packages ruby scripts into executables for linux, mac, or windows. I'm sure I'll have a use for this one.

 

ruby-debug - =]

Scott Rippee @ 5:49 pm Monday, May 28th, 2007

cc by Designerdruby-debug = cool + very useful. Many have said it before and I have blissfully ignored until in a sticky situation that helped me realize I need to be able to poke and prod a little more than spitting out to the log in rails. After all poke and prod is my favorite way to learn.

It's has a simple feel, yet is powerful letting you drop into a debug shell where you can set breakpoint, move through the stack, step around, and examine variables. I was pretty hesitant about learning ruby-debug as I had thoughts of gdb in my head. I have a dislike/fright of gdb and continue to gimp around with it despite a lot of use. ruby-degub, however, is no foe.

No need to cover installing or usage here, as these fine people have created useful docs and even a screencast...

Install and use with RAILS

Common usage

Watch

Data Noise - Developers blog

Haskell? Who dat??

UnderpaidLoveMonki @ 8:47 pm Tuesday, March 13th, 2007

Haskell is not a who, but a what. Read here for more info.

Behavior Driver Development Example

Scott Rippee @ 1:08 pm Sunday, March 11th, 2007

Over at Giant Robots... Tammer Saleh writes about using rSpec with Spec::Rails. This post shows how natural language is used to write the spec in the code and how running it produces meaningful information along with the verification of the statements.

Running your own OpenID server

UnderpaidLoveMonki @ 6:43 am Friday, March 9th, 2007

From OpenID Enabled, you can download OpenID server in an implementation of your favorite language. It's open source. Port it to your language of choice if you don't see it on the list.

Hmmm....I don't see an implementation in Erlang. Imagine the flexibilty of OpenID server's decentralization implemented in Erlang, a derivative of Prolog, that is highly scalable, fault tolerant.

By the way, Programming Erlang will be out soon. There's also a Rails plugin, open_id_authentication. I got too many ideas, too little time. *SIGH* Good times anyways! :)

JRuby Explorations

Scott Rippee @ 9:14 pm Monday, March 5th, 2007

I read something about JRuby today, I think it was that they have a new release that is many many times faster. Anyways, this inspired me to play around with it some. I pulled the newest code from their subversion repo, set up my environment and wrote out some code for a bit

Conclusion... Its really cool and its hard to believe that it actually works. It feels strange creating Java objects in ruby, using their Java methods, and then using them like ruby objects. Very strange, but that strange feeling is cool. Thinking about the huge set of Java libraries instantly available is pretty amazing. No wonder Sun wanted these guys. :)

/usr/bin/ruby: no such file to load — auto_gem (LoadError)

Scott Rippee @ 8:46 pm Sunday, December 17th, 2006

Gentoo decided to throw me a hurdle when trying to `emerge rubygems` tonight:

CODE:
  1. >>> Install rubygems-0.8.11-r5 into /var/tmp/portage/rubygems-0.8.11-r5/image/ category dev-ruby
  2. /usr/bin/ruby: no such file to load -- auto_gem (LoadError)
  3. /usr/bin/ruby: no such file to load -- auto_gem (LoadError)
  4.  
  5. !!! ERROR: dev-ruby/rubygems-0.8.11-r5 failed.
  6. !!! Function ruby_einstall, Line 125, Exitcode 1
  7. !!! setup.rb config failed
  8. !!! If you need support, post the topmost build error, NOT this status message.

The solution is to clear the RUBYOPT variable:

CODE:
  1. RUBYOPT="" emerge rubygems