Everything in moderation — including moderation.
Securing Your API Using Rack Middleware & OAuth 1.0
Forgot to post a link to this. I wrote an article for the EdgeCase Library!
UTF-8 Good, Windows-1252 Bad
So I’m using sidekiq, with sidekiq-scheduler at work in production to handle background tasks. Well, the other night around 10pm, just as I was settling down to watch the last episode of Mad Men, all hell broke lose.
After digging through the logs to find out what happened, all I could find was this
2012-05-07T01:59:10Z 24340 TID-fmbc0 ERROR: Manager#assign died
2012-05-07T01:59:10Z 24340 TID-fmbc0 ERROR: "\xC3" on US-ASCII
2012-05-07T01:59:10Z 24340 TID-fmbc0 ERROR: /home/web_apps/dream/shared/bundle/ruby/1.9.1/gems/json-1.6.6/lib/json/common.rb:148:in `encode'
None of the stack trace bubbled up to my app, so I knew I had no chance at rescuing from an exception. That left me with one thing to do, try and figure out what the heck caused this. Fortunately, I was able to lean on a few more seasoned colleagues of mine for advice.
After some discussion, we landed on our app being sent a windows-1252 encoded character, and trying to parse it as UTF-8.
So, to combat such things, we came up with this:
# This method exists because it has been shown that we cannot
# trust data coming from facebook
#
def clean_fb_str(str)
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
if str == ic.iconv(str)
# valid UTF-8 string should match its iconv version
str
else
# UTF-8 conversion changed string, force windows-1252 into utf-8
windows_ic = Iconv.new('UTF-8//IGNORE//TRANSLIT', 'WINDOWS-1252')
windows_ic.iconv(str)
end
end
Seems to do the job, as we haven’t yet had a reoccurrence of the issue.
NOTE: After deploying this on ruby-1.9.3, I’ve noticed we are getting deprecation warnings. I suppose we should be using String#encode instead.
Damnit! I forgot to change git-pair!
I use git-pair pretty heavily. It’s really nice for assigning blame to whoever checked in crappy code :P Seriously though, it’s nice to give credit where credit is due. However, one thing I am often guilty of is forgetting to change the git-pair. Most of the time I realize what I’ve done after a few commits.
Here is an easy way to clean that up.
Warning: Only do this if your changes are still local as it IS rewriting history!
git rebase --interactive HEAD~3
Interactive
As you probably know, rebasing interactive will allow you to mark what commits you’d like to squash/edit/etc and proceed to step you through them.
HEAD~3
This just says how many commits I’d like to include in my interactive rebase. In my most recent case, I had three commits with the wrong git-pair.
Workflow
Once you save the git rebase file, and mark how you’d like to proceed, you will begin to step through the commits.
Run the following command to change the author of the commit:
git commit --amend --author="Your Name <youremail@example.com>"
followed by a:
git rebase --continue
Anyway, hope that helps someone out.
Georgia O’Keefe - Sky Above Clouds IV
Ruby Standard Library: Shellwords
It wasn’t too long ago I was working on a command line application. Luckily, ruby makes parsing the command line pretty trivial with the OptionParser. OptionParser works great if you want to specify command line arguments with flags. However, sometimes you want to accept options in a more declarative style.
The Shellwords library, also found in the standard library, may come in handy for this.
This module manipulates strings according to the word parsing rules of the UNIX Bourne shell.
require "shellwords"
Shellwords.split("todo new 'Get the milk'")
# => ["todo", "new", "Get the milk"]
Maybe I’m the only one, but I was not aware this existed. Sure, it may not be the most difficult thing to implement yourself. But, having not researched the word parsing rules of the UNIX Bourne shell, I’m willing to guess there are plenty of edgecases I would miss.
Yay Ruby Standard Library!
Emacs day 1: EOD Memory Dump
I’ve reached what I consider to be pretty fluent in vim. In other words, I wish every text box I type into (including this one) were a vim buffer :wq!
Today I switched to emacs. It was horrible. Horrible in the exact same way that switching from TextMate to vim was horrible. I spent most of my research/learning time today reading and going through contrived examples. Despite not editing much actual code, I’d like to take a moment and gather what I remember before calling it a day:
Movement
C-n ;; line below
C-p ;; line above
C-f ;; forward character
C-b ;; back character
C-a ;; beginning of line
C-e ;; end of line
C-v ;; page down
M-v ;; page up
C-l ;; center
Editing
C-d ;; delete character
C-<Space> ;; enter highlight mode
C-w ;; delete highlight, adding to kill ring
C-_ ;; undo -- eventually mapped -z button to do this
File Management
C-x C-f ;; find file
C-x C-s ;; save file
Buffer Management
C-x b ;; open buffers
C-x C-b ;; list of buffers (D to delete / press x when done)
C-x 1 ;; go to one buffer
C-x 2 ;; horizontal split
C-x 3 ;; vertical split
C-x o ;; go to "other" buffer
Modes
M-x follow-mode<Return> ;; scroll from bottom of one buffer to top of next
M-x shell ;; shell wrapper
M-term<Return> ;; Terminal emulator
Searching
C-s ;; partial search, press again to go to next, <Return> to exit
Misc Customization
- Using emacs-starter-kit
- Using emacs 24 (I hear it's the new hotness)
- Using marmalade for packages
- Using custom wombat theme (to compliment my vim theme)
And I’m sure there’s plenty more stuff if I sat down and really thought about it. But, I just wanted to do this little exercise to see what I could remember without much effort.
Copying from terminal Vim
So, I have almost completely made the switch from MacVim to terminal vim. It hasn’t been easy giving up a lot of the niceties that come for free with MacVim. But, there was one thing in particular that really bothered me — copying code from vim and pasting it somewhere else.
Well, never fear. Vim’s “*” register maps to pbcopy (at least on OSX)
So, next time you want to show that kickass piece of code to your friends, just do the following after making a selection:
“*y
You now have that awesome bit of code in your clipboard! Show it off with pride!
