Parsing HTML with Nokogiri

Its insane what xpath can do for you. I don’t think there has been anything that Nokogiri haven’t done for me. For instance parsing this:

results = ‘<tbody>
<tr>
<td><a class=”view” href=”?id=1&amp;o=r”>All Managed Clients</a></td>
<td>1</td>
</tr>
<tr>
<td><a class=”view” href=”?id=2&amp;o=r”>All Managed Servers</a></td>
<td>0</td>
</tr>
<tr>
<td><a class=”view” href=”?id=10&amp;o=r”>AutomationUIGroup</a></td>
<td>0</td>
</tr>
<tr>
<td><a class=”view” href=”?id=4&amp;o=r”>Test JAMF Eau Claire</a></td>
<td>0</td>
</tr>
</tbody>’

This snippet of HTML will change so you can’t depend on your value to be in the same place. One good way to find the numerical value associated with the textual value is to do this:

numerical_value = Nokogiri::HTML(results).xpath(“//tr[td/a = 'AutomationUIGroup']/td/text()”).to_s

Breaking it down…

- Pass the HTML to Nokogiri; it subsequently returns a Nokogiri document

Nokogiri::HTML(results)

- We call the document’s xpath

xpath

- We pass in the path that we want to find. We want to look at any <tr> element that has a nested <td> element and a <a> element. The text nested in the <a> element should equal ‘AutomationUIGroup’. If that is true, we want to get the next <td> element’s text value.

“//tr[td/a = 'AutomationUIGroup']/td/text()”

Changing password in SourceTree with Git-SVN

So I recently changed my AD password at work and using SourceTree for Git-SVN. It works well for me. I noticed that I could not push my commits because of password failure. So I googled. Found out that Git-SVN uses your existing SVN setup. On a Mac, you can change your password if you had it stored in plain text: ~/.subversion/auth/svn.simple. There should be a few files there. You can simply use a commandline text editor to change your password. If you didn’t store you password in plain text, open up Keychain. Select the “login” keychain and “password” under category. Find your password and double click it, check the “Show password” checkbox, authenticate and change it! Done.

Helpful Mac Terminal Keyboard Commands

Wish I would have found this earlier:
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor

From: http://osxdaily.com/2006/12/19/command-line-keyboard-shortcuts-for-mac-os-x/

Rename native Mac OSX Ruby binaries

When I install Ruby on my Mac, I usually install the 1.9.3-p327 from source. I don’t delete the Ruby version that was installed by Apple but rename and add 187 to binaries to reduce confusion. Run this in the terminal with sudo:

mv /usr/bin/ruby /usr/bin/ruby187
mv /usr/bin/gem /usr/bin/gem187
mv /usr/bin/irb /usr/bin/irb187
mv /usr/bin/erb /usr/bin/erb187
mv /usr/bin/rdoc /usr/bin/rdoc187
mv /usr/bin/ri /usr/bin/ri187
mv /usr/bin/testrb /usr/bin/testrb187
mv /usr/bin/rake /usr/bin/rake187