Making advanced search forms for your backend kinda sucks, right? I ran into this problem with a project and decided to come up with something similar to how Google handles search queries. No dropdowns, no checkboxes, no radios, none of that stuff. I just wanted to be able to type status:active user:rob [term] and get what I asked for. I came up with a pretty nice utility class that can handle some basic query syntaxes and give you back hashes or arrays and the left over query.
More →
I may be a devout rubyist, but I still have a soft spot for WordPress — and maybe even a little for PHP too. One of the things I think is lacking is a good way to manage WordPress updates since, as we all know, WordPress updates constantly. I wrote this quick thor script to help me update to the latest version from the command line so I can do quick version testing. As an added bonus, there is also an export task as well which will simulate doing a wordpress XML export from the dashboard.
So, for all you WordPress guys out there who hate doing manual updates, check this out. It may make life a little bit easier.
More →
If you look around this site you’ll notice that I’m hosting all my code snippets on GitHub’s awesome gist feature. While this allows me to not maintain syntax highlighter plugins and libraries, as well as lets me change the code snippet without updating the blog is nothing short of awesome. The only thing that kind of sucks is that I/we are losing the ability to have our code be search engine friendly since a gist is a 1-line javascript include. This sucks not so much from the “index me!” perspective, but more of the “if I google a line of code/error, will I find relevant code?”
To tackle this, I ended up with a small project: given a gist ID, generate the embed code along with NOSCRIPT tags so the content is indexable. This will only solve the initial problem, as updates will not propagate to the blog, but… you can always manually update.
More →
This one is a pretty specific hack. I’ve had the need to build mini reporting apps, using Sinatra, that aggregate data from multiple databases and produce a report. This is all fine since ActiveRecord is awesome, but I’ve always missed AR’s query caching ability.
After some digging I found that query caching is only enabled on AR’s Base connection and all other models must use that awkward Model.cache{} construct — which is annoying to use. I’ve hacked up a solution that works, although it relies on :send and instance_variable_set which isn’t very elegant or solid.
For what it’s worth, here is a “simple” way to get Sinatra to use ActiveRecord’s native query caching for more than 1 database:
More →
I was creating a project the other day that used anonymous pages for users and I wanted to make it a little less “guessable” but more human friendly than a SHA1 hash, so I decided to make a small home-brew URL shortener similar in format to Tiny URL.
The neat thing about ruby is that when you turn a Fixnum to a String with to_s you can specify the base, and when you turn a String back to a Fixnum you can also specify a base in to_i. This lends itself perfectly to turning (large) IDs into a shortened URL thats easier to remember.
In the first gist you can see how this would work with a model to make simple url shortening in rails — or, you can use randomized values to generate more user friendly “unique” tokens (as opposed to using SHA1 hashes for user confirmation/password reset pages).
More →
I recently had a need to parallel process some tasks for a non-critical app, but really didn’t want it to get out of hand. I wrote this as a simple way to multi-thread without the pain of worrying about synchronizing and queues and being thread safe.
It achieves ghetto thread pooling by chunking the result set into whatever size slice is needed and spins up some threads, once that set is done it will repeat until the data is all processed before moving on.
It ended up working out really well as a direct drop-in, and speeded up the processing greatly. I wouldn’t recommend this for production code, however.
More →
I’ve been spending a lot of time creating reports lately, and I generally live in a terminal, so naturally I started to think of ways to easily send emails with these reports attached. I’m stuck using Microsoft Entourage here at work, but thankfully they have a pretty robust AppleScript library. Mixing some stuff together I pulled together this Ruby script which will allow me to do some neat stuff:
- pipe data into the
email command to create a body
- use aliases as groups to send emails to common groups of people, etc
- add multiple attachments to emails straight from the command line!
- not have to move my hands from the keyboard when sending emails!
At its core, it is nothing more than a simple ruby shell script which compiles AppleScript and uses osascript to execute the AppleScript. When run it will pop open a new Entourage mail window with the To/CC/Subject filled out, as well as any attachments and moves focus to the window. If I had piped anything to STDIN that would be the body, otherwise it would blank and ready for a quick message. Then a quick CMD-ENTER and off the mail goes. Simple and elegant, yet kind of powerful
More →
I was writing a small shell script in Ruby today that needed to check STDIN for data, but I didn’t want to prompt the user to enter data when nothing was found (I only wanted piped in data). This turned out to involve a little more effort than just checking STDIN.read.
Luckily, I found a solution at Footle that worked out great. Heres to anyone who needs _only_ piped in data from STDIN without prompting the user.
More →
This is a simple, but useful little script I ended up writing to test some things. I found it easier to use this than to constantly use the mail command. It basically wraps action mailer in a small ruby script so you can call it from the command line.
./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!' --body='whatsup guy?'
or using STDIN
./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!' < body_of_message
cat|./spoofmail.rb --to=who@where.com --from=sjobs@apple.com --subject='Hey You!'
whatsup guy?
^d
It might come in handy one day when you need to mess with a co-worker or test a mail receiver... or something else?
More →
This is admittedly ghetto, but I needed a super simple small-scale solution to creating unified diffs based off large blocks of text. The easiest way to do this was by creating temp file and using the native diff command to do the comparison.
I’ve used this method for a really small snippets app for our intranet, which can be found on GitHub.
More →