Archive for the ‘Technology’ Category

Recursive JSON to URL function

Posted on by Alex in Randomness, Software

While working on a snazzy new toy for Velocity, I needed a script to transform JSON to URL parameters. I found this clever method for doing it, but sadly, it wasn’t recursive, so it would only generate URL parameters for the first level of JSON elements.

I rewrote the function to recurse over all elements in the object tree and spit them out in a URL-safe string.

  var JSON = {
    params : function(a1){
      var u=[];
      for(x in a1){
        if(a1[x] instanceof Array)
          u.push(x+"="+encodeURI(a1[x].join(",")));
        else if(a1[x] instanceof Object)
          u.push(JSON.params(a1[x]));
        else
          u.push(x+"="+encodeURI(a1[x]));
      }
      return u.join("&");
    }
  };
var doc = {
  "env":{
    "platform":"linux",
    "browser":"Firefox%2F3.5.8"
  },
  "query":{
    "project":"user-tracking",
    "fetch_time":"0"
  },
  "user":{
    "locale":"en-US",
    "cluster":""
  },
  "last_modified":"1270872812"
};

Reloading SQLite data in Rails

Posted on by Alex in Randomness, Software

While developing a new system built on rails, I encountered a peculiar problem. I wanted to reset my database, but keep the data that was currently living it it. This turned out to be a unique problem for the following reasons:

  • Rails has the functionality to ‘seed’ the database using the seeds.rb file and running: “rake db:seed” This is great if you already have the data written out in pretty rails code. If your data lives in the database, resetting it will wipe out the data.
  • You can only seed data if it has a model associated with it. I’m working with data that uses ‘has_and_belongs_to_many’, which requires a non-modeled table in the db to hold the many-to-many association. You can’t import these associations in Rails.
  • Looking online, I found a nifty rake task to dump the SQL data from the database to a file. You can then load it into the current environment’s db after you’ve done your magic. I found that over at Agile Web Operations. The only problem is, it doesn’t work for sqlite. :(

So I did what any self-respecting developer would do… I rewrote it to do what I needed.

My new rake task implements the following changes:

  1. Works with sqlite3
  2. Only saves the ‘INSERT’ statements from the dump. No need for the schema info from sqlite since it’s already in rails.
  3. Fixes a bug where data loaded was always being loaded into the test db. I like flexibility to load into any environment.

Save the following code into /lib/tasks/seed.rake:

namespace :db do
  namespace :seed do
    require 'db/seed_tables'

    desc "dump the tables holding seed data to db/RAILS_ENV_seed.sql. SEED_TABLES need to be defined in config/environment.rb!!!"
    task :dump => :environment do
      config = ActiveRecord::Base.configurations[RAILS_ENV]
      dump_cmd = "echo '.dump #{SEED_TABLES.join(" ")}' | sqlite3 #{config['database']} | fgrep INSERT > db/#{RAILS_ENV}_seed.sql"
      system(dump_cmd)
    end

    desc "load the dumped seed data from db/RAILS_ENV_seed.sql into the test database"
    task :load => :environment do
      config = ActiveRecord::Base.configurations[RAILS_ENV]
      load_cmd = "sqlite3 #{config['database']} < db/#{RAILS_ENV}_seed.sql"
      system(load_cmd)
     end
  end
end

And, like the previous incarnation, load up the names of the tables you want to dump into a file at /db/seed_tables.rb:

SEED_TABLES = {
  "accounts",
  "products",
  "users"
]

Here’s an example of how I used it:

rake db:seed:dump
rake db:reset
rake db:seed:load

Multi-domain WordPressMU Made Easy

Posted on by Alex in Randomness, Software

So I was fighting with WordPress MU for about 6 hours today attempting to get it to properly serve blogs for multiple domains and paths. This seemed like it wasn’t going to work since WPMU is configured to use either subdomains or subdirectories and all multi-domain solutions seem to only work when using subdomains.

Somehow, through the magic of mod_rewrite I was able to get multiple domains working while using the ‘subdirectory’ configuration in WPMU. The only problem I ran into was that I was unable to login to wp-admin in the non-default domains. I realized this is because of the ‘domain’ defined in the cookie. To remedy that, I changed a single line in my wp-config.php to the following:

define('DOMAIN_CURRENT_SITE', $_SERVER[ 'HTTP_HOST' ] );

That tells WPMU that the current domain is the one PHP sees. Surprisingly this fixed the cookie issue, and hasn’t triggered any other errors. We’ll see how it holds up.

Thunderbird 2.x Movemail Account on Fedora Core 8

Posted on by Alex in Interesting Links, Software

Sadly, Fedora Core 8 doesn’t ship a packaged version of Thunderbird (2.0.0.9 as of this writing) that includes movemail capabilities. Movemail is the account type (in Thunderbird) that allows you to read mail from your local system (in /var/spool/mail/[username]). If you run cron jobs, you should be getting emails about the status of those jobs in your local mail directory. Instead of having to read them through the command line, I wanted to read them through Thunderbird. Here’s a step-by-step on how to get it working:

  1. Download movemail.rdf from the Mozilla repository. This includes the account type ‘Movemail’.
  2. Save a copy of movemail.rdf to /usr/lib[64]/thunderbird-2.0.0.9/isp
  3. Run the following: chmod 777 /var/spool/mail
  4. Restart Thunderbird
  5. In Thunderbird when adding a new account, select Movemail as the type, and follow the directions as usual.

All done! Hopefully, future versions of the Thunderbird package in Fedora’s repo. will be updated to include movemail, but until then, this works.

Comments Off

Save the Bell Labs Holmdel Facility!

Posted on by Alex in Interesting Links, Internet, Technology

While browsing one of my favorite tech-news sites, Digg.com, I found out that PREI has plans to demolish the old Bell Labs Holmdel Research Facility. While I am not old enough to have been a direct part of the historic research which was conducted at the facility, I am directly indebted to the men and women who did groundbreaking work there. Without them, I probably would not be pursuing a degree in Computer Science.

I first learned of the Holmdel Facility in my Networks class sophomore year. We learned all about cellphone technologies, network protocols, topologies and interfaces, as well as encryption algorithms. All these things were pioneered in the Holmdel Facility. Without that facility and the people inside, technology would not be what it is today. Hell, I might not be writing this entry on my blog if it weren’t for the folks in the Holmdel facility.

I think it is important that PREI looks past the financial gains which may be had at the expense of the destruction of one of the birthplaces of computing. Surely there is a fate more suited to such a historic site. It has been suggested that the site be converted into a technology and computing museum, or used to house offices for new technology companies. Sadly, this is probably a fate the site will never see.

Link: http://www.engadget.com/20…ace-of-the/

Comments Off

AIM Client using AJAX?

Posted on by Alex in Interesting Links, Software

Do you think it is possible to create an AIM client using Perl and AJAX? On the x10hosting forums someone started a thread about creating an online AOL Instant Messenger client. He cited WebAIM.net and AIMonPSP.com.

I then realized that Perl has functionality for connecting to AOL’s servers. I once made an AIM bot using perl (I really wish I still had that code :/ ). Then I thought… well, it the server is doing all the work, how do we get the information to the client? AJAX? Might work. Could be kinda slow. Would definitely have to work out the kinks. WebAIM is based on Java. While that is all well and good, it still requires a client download. AIMonPSP uses a server based approach, but it has to use META refreshes to get its information. Not so fast. I was thinkin’ well, why not AJAX. From what I know of AJAX, this isn’t the best use of it, but it could be a nice proof of concept.

Oh well, as of right now, I’m up to my eyeballs in Night Light, my soon-to-be-released distribution of Linux. I’m having a lot of fun and I’m learning a whole lot about how Linux works. Now, if I could only learn this much about cars….
Read the rest of this entry »

Comments Off