Author Archives: Lee

[BlogEntry] Returning and Executing JavaScript from a Rails URL

AuctionRelay.com has been running in Tomcat for a few years. A simple "include" script tag is all it takes to make AuctionRelay add HTML to the middle of an existing web page. The trick is in using the document.write command to make executable JavaScript write visible HTML at the location in the document where it executes.

I'm now in the process of rewriting AuctionRelay to run on Ruby on Rails (shouldn't that really be Rails on Ruby?). This morning, it was time to tackle the issue of setting the HTTP Content-Type header in the response, so that the browser would recognize that the response was executable JavaScript, and not displayable HTML. (Of course, the executable JavaScript is responsible for squirting in the displayable HTML, but that's the kind of convoluted world we've created for ourselves.)

I can browse to http://localhost:3000/js/test, which includes the line

<script src="http://localhost:3000/js/free?id=option_insight"/>

and see something like this:
in the midst of the original HTML page. (What you see here is still the output of the Java version.)

js_controller.rb:

class JsController < ApplicationController

  def out
    @sellerId = @request.params['id'].to_s
    $mylog.debug("free request for " + @sellerId)
    
    if $freeSellers[@sellerId].nil?
      $mylog.debug("Not found - instantiating...")
      $freeSellers[@sellerId] = Ebay.new
      $freeSellers[@sellerId].retrieve(@sellerId) 
    else
      $mylog.debug("Already exists.")
    end
    
    @headers["Content-Type"] = "text/javascript"
  end

  def test
  end
end

test.rhtml:

Before
<script src="http://localhost:3000/js/out?id=option_insight"/>
After

out.rhtml:

document.write('<table border=1 align=center><caption><br/>My eBay feedback rating is\
<a href="http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback&userid=<%= @sellerId %>" target="_blank">\
<%= $freeSellers[@sellerId].feedback %>\
</a><br/></caption>\
<tr><th>Item<th>Price<th>Bids<th>Ends<tr>\
<td colspan=4 align=center>Display your live auctions on any web site for free at\
<a href="http://www.auctionrelay.com" target="_blank">AuctionRelay.com</a></table>');

One of the things I learned the hard way is that the string you pass to document.write must either be on a single line or use backslashes for continuations. (I was trying to make out.rhtml readable. Duh. Hindsight is 20/15 here.) Fortunately, the Firefox JavaScript Console gave me an error message that made me realize what was going on. Unfortunately, it took me 15 minutes of head-scratching to even think to look at it and recognize that the problem was with the actual JavaScript syntax and not something server-side.

[BlogEntry] I'll be with you in 1frdtydangy

Microsoft Office Outlook 2003. A product so advanced that it understands time coordinates from other galaxii.

When this ingenious product pops up a reminder for you, did you know that you can snooze it, not only with the drop-down list of options from minutes to weeks, but that you can also type in an arbitrary floating point amount of time to snooze? You can type in 1m, 5.3 d, 2w, etc. (One problem is that I'm not sure if 1m means I'll be reminded again in a minute or a month. But that's my fault for not RTFM.) But I discovered today that you can also type in 1frdtydangy.

Interestingly, it gave me an error when I tried frdtydangy. Naturally. How can you expect it to delay based on a lone unit of measure, if you don't tell it how many of that unit of measure you mean? I mean, it's not like Outlook 2003 can read your mind or something, right? No, it wasn't until I added a leading 1 that Outlook 2003 understood that 1frdtydangy meant I wanted this particular reminder to snooze for the amount of time it takes the moon of wklcphhhftzsh-po to complete one full revolution around it's neutron star.

I hope it's less than a frdtydangy before I can install OS X on my laptop.

[Comment] Re: Iterating over a Hash in Ruby

Yep, cool. Thanks, Tony!

irb(main):043:0> h1.each{|k,v| puts "#{k} ---> #{v}"}
a ---> 1
b ---> 2
c ---> 3
=> {"a"=>1, "b"=>2, "c"=>3}

This reminds me of something else that has been bugging me since I saw it. What does this syntax mean?

def list
  @category_pages, @categories = paginate :category, :per_page => 10, :order_by => 'category'
end

How can Ruby return two values from the paginate method?!

[Comment] Re: Bill Maher takes a beating

That's what I was going to write on my blog! Jesus H. Christ does that guy get on your last nerve, or what? Not that some of the talking heads on the right aren't just as annoying, but, at least they're on my side – they trust the president – they know the definition of the word enemy – and they love this country. Great Blog!

[Comment] Re: Bill Maher takes a beating

That's what I was going to write on my blog! Jesus H. Christ does that guy get on your last nerve, or what? Not that some of the talking heads on the right aren't just as annoying, but, at least they're on my side – they trust the president – they know the definition of the word enemy – and they love this country. Great Blog!

[BlogEntry] Iterating over a Hash in Ruby

Let's say you define a simple Hash.

irb(main):001:0> h1 = {"a"=>1,"b"=>2,"c"=>3}
=> {"a"=>1, "b"=>2, "c"=>3}

What do you think you'll get when you send an each message to it? I figured either a, b, c or 1, 2, 3. Surprise:

irb(main):003:0> h1.each{|a| puts a}
a
1
b
2
c
3
=> {"a"=>1, "b"=>2, "c"=>3}

It all comes out, keys and values interleaved.

Now, the trick becomes figuring out what to Google for to learn the syntax that gets you just the part of the hash you're after. Let me save you a little time. What you're after is each_value.

irb(main):008:0> h1.each_value {|a| puts a}
1
2
3
=> {"a"=>1, "b"=>2, "c"=>3}

You can also use each_key. Fairly intuitive, once you know it. Ah, there's the rub. 🙂

irb(main):009:0> h1.each_key {|a| puts a}
a
b
c
=> {"a"=>1, "b"=>2, "c"=>3}

[Comment] Re: Java the Hut

Anyone remember Lotusnotes (later renamed domino)? That was a platform alright. A giant one.

Some people, frustrated with Domino, started complaining how it looks more and more like 'a giant solution in search of a problem'.

Today, some of us are starting to feel that J2EE is another one of those 'giant solutions in search of a problem.' Yes, you may get paid big bucks for doing some J2EE work today, but back in the mid-late '90s, you could've done the same with Domino. Today? I honestly doubt it.

J2EE is slowly turning into the legacy platform. It is going the way om mainframes. That is to say, the way of dynosaurs.

The handwriting is clearly on the wall. Get out while there is still an opportunity.