Category Archives: Coding

Twitter search archiver

With Teenormous, we started giving away free tees to people that tweet a link to us. Problem is, we couldn’t find an easy solution to archiving/looking back on tweets for the month. Twitter keeps the tweets seachable for a very short time. We found a few web apps/services that did some similar things but not exactly what we wanted. We even tried Google Reader by putting the search RSS feed into it. (That worked, but trying to scroll and count to #57 is a royal pain and that method just doesn’t scale.) So, some poking around led me to the excellent Ruby library for Twitter by John Nunemaker which uses the Twitter APIs.

It’s so easy to use it’s laughable. A few hours later and I have an app/script written that stores the results of any twitter query (like the one we care about) into a static HTML file with them in order from oldest to newest and each tweet numbered for easy random picking of winners. It also automatically creates a new file each month. I used the Twitter CSS so it looks nice and clean. Slap it in a cron job and we have auto-archiving twitter searches. Plus – it’s in HTML format in case we want to put them online for some reason.

It’s nice sometimes to do a little coding project that’s not exactly what you typically do. Oh – and Ruby rocks.

How to get Rails to talk to Oracle

How to get Rails working on Windows with a pre-existing oracle DB…..

I wanted to try and hookup Rails to our existing oracle DB at work. I had a rough time figuring this out, so here it is in case anyone else wants/needs it. In my case, I did not (nor did I want to) have a full Oracle client installed on my Windows box. trying to get this to work was not easy. I found 5 or 6 different pages that told different ways to get it to work, but none worked for me.

Most of this info was compiled from Oracle’s Rails FAQ and a few other pages on the net. These instructions assume you don’t have the Oracle client installed already. If you do, some of these steps may not be necessary(1, 3, 4)

  1. if you don’t have the Oracle client installed already, install the Oracle Instant Client (I installed the Basic, Lite version) – this zip contains some DLLs and jars. Extract them somewhere and note the location. Let’s call it [ORACLE_CLIENT_DIR]
  2. install the Oracle driver for Rails (I’m assuming you already have Rails installed). Details here.
  3. create a tnsnames.ora file in [ORACLE_CLIENT_DIR]/network/admin/. Put the text below in the file. Replace host, port, SID, and name (first line – MYORACLEDB in the example below) as necessary. If the DB uses a service_name instead of SID, replace SID in the file with SERVICE_NAME.

    MYORACLEDB =
    (DESCRIPTION =
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = some.server.com)(PORT = 1521))
    )
    (CONNECT_DATA =
    (SID = mysidinfo)
    )
    )
  4. create Windows environment variables like so:
    ORACLE_HOME = [ORACLE_CLIENT_DIR]
    TNS_ADMIN = [ORACLE_CLIENT_DIR]/network/admin
    you can create these globally in Windows or just set them up before running the server each time
  5. setup the database connection in your app as shown here, but replace
    host: ORCL
    with the name you used in your tnsnames.ora file (MYORACLEDB was used in the example above)

  6. if you are connecting to an existing DB that did not follow the standard pluralization conventions Rails likes (i.e. your table names are not pluralized), you need to tell Rails not to do pluralization (which it does by default). Open [yourapp]\config\environment.rb and put this at the bottom of the file:
    ActiveRecord::Base.pluralize_table_names = false

That’s it! Now, you can generate models, controllers, etc. and they should hookup to your Oracle DB as you would expect. Hopefully I didn’t miss any steps. I wrote this after I had it working, not while I was trying to get it working. If you are in a similar situation and these steps don’t work, feel free to leave a comment and I’ll see if I can help.

New project with Ruby on Rails

I have decided to start a new programming project in my copious free time (HA!) so I am now learning Ruby On Rails. I have been through two tutorials and it really does look as easy as it seems, especially when compared to J2EE that we all know and love. This should be fun. I can’t share any details on the project yet, but stay tuned.

Overriding JComponent’s Ctrl-C Action

A little Java geekery here for my own reference and anyone else that needs it….

I needed to override the default CTrl-C (copy) behavior on a JList (though this should work on any JComponent) to copy what I wanted to the clipboard. After Googling and playing with it, I found that this works:

InputMap im = JLIST.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap am = JLIST.getActionMap();
Action copyAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
String myString = "this goes to clipboard";
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new StringSelection(myString ), null);
}
};
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
am.put("Copy", copyAction);

Anatomy of the Mailinator slashdotting

So, our Spam Map was slashdotted today and it was my first time experiencing it firsthand from the server side. I thought I’d share some interesting things that happened today. Just over 12 hours ago, (it’s now about 9:40PM) we were put on slashdot. As of right now, our hit counter for the page is at 99083 – almost 100K hits in a 12 hour period. WOW! By the time I finish this post it may very well clear 100K.

How did we handle it? Well, we are running a custom-made Java web server, written from scratch by Paul – this is running under Jetty as our servlet engine/webserver. Around 10:30AM EST, we REALLY started getting pounded. We started getting LOW ON THREADS warnings and then OUT OF THREADS warnings from org.mortbay.http.SocketListener.isLowOnResources. At that point, the server melted. We bounced the server and it ran out of threads almost immediately. POUNDED. Surprisingly, my map code and Paul’s mail code held up very well. Of course, Paul’s mail code handles a million emails a day and sometimes over 3 million a day, so his code was never in doubt.

As I write this, hits are now at 99283…

So the server ended up being down and out for a short period of time – 15 minutes total maybe? Which I thought was pretty damn good. One thing we did was make sure that the map page itself (and the data file it loads) was NOT a JSP page. We just served up static content essentially – no extra processing. A thread ran in the background to update the data file periodically. If the map page were a JSP getting the data dynamically, I’m sure it would have failed much sooner and for much longer.

99358…

The hardware is a dedicated server from ServerBeach. I don’t know the specs but can find out if anyone is interested.

Just read some emails and the hit counter is now at 99709

What else?…..
Oh – I thought this was kind of interesting…with all the traffic today – lots of people were trying out the system by sending a test mesage to mailinator. MANY of them sent an email with the word test as the subject. BUT…..that triggered our spam filters because we got hundreds of emails with the same subject so we started rejecting (bouncing) them as spam. Of course, these people then assumed that mailinator doesn’t work. So, if you are testing it, send a message with a subject other than test.

OK, that’s it for now. I’ll add on more stuff later as/if I think of it. Oh – maps don’t seem to work in Safari – gotta look into that….and the map popups in IE are centered when they should be left-justified. Minor problems I need to fix.

Counter at 99850…ok – let me wait til it crosses 100K…
10:10PM – 99922
10:12PM – 99991
10:13PM – 100021 !!!!!
9:34AM to 10:13PM – 12 hours and 39 minutes to reach 100K hits

Man, this was fun! I used to dream about creating something that was cool/geeky enough to get slashdotted and it has now happened! Cool.