Regex Golf

I was going to name this post Vim Golf, but it turned out to be more of a regex experiment than vim keystrokes.

So I found this cool ifttt recipe that logs all of your tweets to a dropbox file. It puts them all in a single file with a format of:

At a drive in. Wat?  @ Valle Drive-In http://t.co/Z9lz7O5F
Jun 30, 2012
http://twitter.com/darrinholst/status/218878382123909120
- - - - -

I see blurry apps
Jun 30, 2012
http://twitter.com/darrinholst/status/219074474073538560
- - - - -

Since the twitter api only allows you to get to 3200 of your tweets I though it would be a good idea to get the rest of the tweets that are accessible to me in there as well. The quickest way I know to get my latest tweets is at allmytweets.net. Allmytweets will pull down all your tweets and then show them on their page. The html ends up looking like:

8"><img src="css/extlink.png"></a></li><li>Straw in the wro
ng hole.  @ High Life Lounge <a href="http://t.co/kdMfTvPk">
http://t.co/kdMfTvPk</a> <span class="created_at">Jun 12, 20
12</span> <a href="https://twitter.com/#!/darrinholst/status
/212644435689881600"><img src="css/extlink.png"></a></li><li
>I can see @dwolla from here! <span class="created_at">Jun 1
2, 2012</span> <a href="https://twitter.com/#!/darrinholst/s
tatus/212639918541910016"><img src="css/extlink.png"></a></l
i><li>Simplify Design With  Zero, One, Many rules <a href="h
ttp://t.co/0bvdVzzo">http://t.co/0bvdVzzo</a> <span class="c

All the data is there, but not very useful for a txt file. Here’s the commands I threw at it to format it up:

Split li tags up to separate lines

%s/<li>/\r<li>/g

Preserve new lines in the tweets with a token

%s/\n/##NL##/g

Change new line tokens in between li tags back to new lines

%s/<\/li>##NL##<li>/<\/li>\r<li>/g

Reverse the order of the tweets, I want them in chronological order (The only non-replace command)

g/^/m0

Remove li start tags

%s/<li>//g

Replace li end tags with a separator

%s/<\/li>/\r- - - - -\r/g

Turn new line tags back into new lines

%s/##NL##/\r/g

Get rid of the start span tag for the date

%s/<span class="created_at">/\r/g

Get rid of the end span tag and tweet link start tag. Also change from https to http

%s/<\/span> <a href="https/\rhttp/g

Get rid of those stupid #!s

%s/\/#!\//\//g

Get rid of the image from allmytweets.net

%s/"><img src="css\/extlink.png"><\/a>//g

HTML decode

%s/&amp;/\&/g
%s/&lt;/</g
%s/&gt;/>/g
%s/&nbsp;/ /g

Get rid of remaining html

%s/<a href="//g
%s/">.*<\/a>//g

Clean up trailing spaces

%s/ *$//g

EDITING TEXT IS FUHHHH UHN! I wish I would have though of this 3800 tweets ago though.

CoffeeScript Function Binding

In my last talk that I gave on CoffeeScript I quickly went over function binding and CoffeeScript’s => syntax. My point was that when you need to use .bind(this) then you could just use => for shorter syntax. The inevitable question that came up was “Why wouldn’t I just use that everywhere?”. It’s a valid question, especially coming from people that aren’t in the JavaScript world a lot and/or haven’t figured out the this craziness. I didn’t have a good answer at the time, but I gave the example of jQuery setting this to the event element and you might want that this pointer instead of the current one.

class ClickLogger
   clicked: ->
     console.log(@.href)

   clickedBound: (event) =>
     console.log(event.target.href)

$("a").on("click", new ClickLogger().clicked)
$("a").on("click", new ClickLogger().clickedBound)

In the example above we want the element that was clicked and we don’t need anything from the instance of ClickLogger so there is no reason to bind it. It also shows what you would have to do if you did bind it.

Fast forward to present day when Les Hill posted some slides of a Backbone and Rails presentation he did. In there was…

…which got me thinking about it again. So I asked him and his response was…

Intention revealing. So by not using => everywhere means that I know what it does and when I should use it and that I really intended to override the default behavior.

Just because you have shorter syntax to do cumbersome things doesn’t mean you should use it everywhere, especially if you wouldn’t have naturally done the cumbersome thing if you didn’t have the shorter syntax.

In case you were wondering if performance was a reason for not using it everywhere…it’s not.

Why I Switched to Sublime Text 2

  • Download Spacie from $0.99 sale
  • See that rvm is taking up 2GB
  • Delete ruby 1.9.2
  • Type mvim .
  • Realize that MacVim doesn’t start anymore because it was compiled with 1.9.2
  • Go find the MacVim source that has the grey sidebar background
  • Realize that doesn’t exist anymore
  • See references to Sublime Text 2 in the comments
  • Download Sublime Text 2
  • Enable vim mode
  • go back to work

Rails Mass Assignment

A quiet Sunday morning turned into Dramaville in the Rails world over this commit to Rails master. A github user managed to get his public key into the list that are allowed access via the mass assignment loophole. If you use Rails and don’t know about the problem with the default behavior of mass assignment go read this now.

There are a lot of issues with this whole situation, the first being that it’s absolutely the developer’s responsibility for the security and integrity of the software that we write. Blaming a framework is not an option here. That said, if you have a Rails app I highly recommend adding the following line to config/application.rb

config.active_record.whitelist_attributes = true

What this will do is turn white listing on for every model in your application. That means that you will have to explicitly define each attribute in your model that you expect to update via a http request with attr_accessible. That certainly is a pain in the ass, especially for large applications, but it’s better than the alternative of what happened to github today.

Another issue was the way that this was exposed. I’m torn on this specific incident because the guy was ignored, but on the other hand this has been such a well known problem in the Rails world that I probably would have ignored it too if he didn’t provide any code to fix the problem. Generally though I would encourage security vulnerabilities to be reported via the appropriate channels and give the owners time to fix it.

The last issue I want to comment on is the default behavior of Rails. Although I believe you shouldn’t rely on or blame a framework for security stuff, I do think that the framework should do everything possible to lead you down the right path. Rails doesn’t do the right thing in this situation. The white list (or some other solution) should be on by default. Since this would break a lot of apps I can see why they haven’t done anything yet, but I do expect something to change for Rails 4 (and hopefully the solution moves out of the model and into the controller where it belongs).

###Edit

This gist from @dhh looks like a good technique if you want to move this concern into the controller now.

blogging

Blogging is a weird thing when you’re not internet famous. Why do you write stuff when ones of tens of people will probably see it? For me it’s the act of writing that helps with thoughts. I write here, I write on twitter, I write in a journal.

The benefits of writing is not what this is about. It’s about more important stuff like what tool do you use to write?

I’ve been using tumblr since around 2009 when I switched off of a wordpress install that ran on some vps that I had somewhere. Somewhere is the key because that stuff is gone now. My fault, but still all of it is gone.

Tumblr is a really easy tool to use and it has a ton of themes, but one thing that I found difficult is syntax highlighting of code. There are embedded gists (which you have to go through hoops to get working on tumblr) and there is prettify (more hoops). I ended up writing a syntax highlighting app that gives you html with inline styles. It did the job, but not as easy as it could be.

Recently I started playing around with jekyll. It’s really nice to have all your posts stored in markdown in a git repository, but you’re pretty much on your own for design. I suck at design.

Then I found octopress which combines the best of both worlds. It uses jekyll, but comes with a nice looking theme and plenty of plugins. It’s really easy to host your site on github pages or even a heroku app. The thing I like best about it is it’s syntax highlighting capabilities. It uses the same “triple tick” code fencing that you’re used to with github flavored markdown.

If you’re deploying to github pages I’m not sure you can host the source files and the generated files in the same repo. I tried doing it with different branches, but couldn’t get it to work. So I have 2 repos, but the one with the source files is the only one I update. rake deploy takes care of updating the other one with the static content.