Archive | Snippets RSS feed for this section

JavaScript: Base64 Encoder/Decoder

I recently had a need (or, probably more appropriately, a desire), to obfuscate some data which would be passed to a server in a URL. Of course, it’s impossible to encrypt anything using JavaScript since it executes client-side, but you can certainly obfuscate data enough to ward off most tinkering attempts. I decided base64 would [...]

Read more

Rails: clean old ActiveRecord sessions

If you use ActiveRecord for session storage, you’ll find that the sessions table fills up quickly. reset_session deletes the current session (say, when implemented in a logout action), but stale sessions can still build up when a user simply closes their browser. To combat this, here’s a simple solution using a class and a cron [...]

Read more

Generate vertically-ordered HTML table in Ruby

In a major Ruby on Rails project, I recently had to generate a table of data which had to be organized with data flowing from top-to-bottom in each column before moving on to the next column, instead of left-to-right. This seems simple, but I had trouble getting my head around it for some reason (maybe [...]

Read more

Keeping track of JavaScript timers

While working with a complex AJAX script today, I discovered a long-standing problem with an interval timer duplicating itself. I spent most of the morning trying to figure out ways to prevent more than one instance of the timer from firing, but since the timer could be stopped and restarted based on user interaction, there [...]

Read more

RMagick on Mac OS X: segfault?

Here’s a quickie for ya. I recently found that using the rmagick gem causes a nasty segmentation fault on my Rails 3.1 development machine, which happens to be a Mac running OS X 10.7 (Lion). I use ImageMagick from MacPorts with Ruby 1.9.2, and here’s what I had to do to get rmagick working: gem [...]

Read more

mysql2 Ruby Gem on Mac OS X

Getting the mysql2 Ruby gem working on Mac OS X Snow Leopard (10.6) and Lion (10.7) is a pain in the ass. Once you get the MySQL server installed and working, and the mysql2 v0.3.7 gem, you may get the following error when trying to rake db:migrate: 123456rake aborted! dlopen(/Users/mclark/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib [...]

Read more

PHP cURL and Error 417 (Expectation Failed)

I recently noticed that my KD5HIA Weather app for iPhone stopped receiving push notifications for weather advisories. One of the PHP scripts I use to send the notifications uses cURL to POST the message via HTTP to another script on one of my other servers, and it was failing with: 417 – Expectation Failed. WTF?

Read more

MySQL: Search and replace

If you ever relocate files on a web server or need to change some markup in a weblog database, you could end up with a bunch of articles with broken content.  You can use a simple SQL query to find a string in a particular column and replace it with something else: 123UPDATE `table_name` SET [...]

Read more

RoR: Get objectGUID as String from Active Directory

I recently had to write an application which requires authentication, so naturally I decided to use our existing Active Directory infrastructure. Also, the application has it’s own users table, so I had to be careful that database operations were performed for the correct user (since usernames can be changed in Active Directory).

I decided to use the

1
objectGUID

attribute, but unfortunately, it’s not a string. So, the example below shows how to convert it to a string, as well as insert the hyphens in the appropriate places.

Read more

PHP: Email Validation

This is a function I found in an article by Douglas Lovell at LinuxJournal.com which will take an email address and return true or false on whether it’s valid. I was in search of a good regex, but ultimately decided that a one-liner would just be too messy. Douglas explains (very well) why the function [...]

Read more
Page 1 of 41234