Tips

Converting an XML file to a CSV file

Sometime I have the task, of converting a file from one format to the next. Here is my template for converting from XML to CSV using ruby.

require 'rexml/document'

xml_file = File.open("input.xml", "r")
csv_file = File.new("output.txt", "w")

# Header
csv_file.puts "field1, field2, field3\n"

xml = REXML::Document.new(xml_file)

xml.elements.each("array") do |e|
e.elements.each("node") do |f|

field1 = f.elements['field1'].text ? f.elements['field1'].text : ""
field2 = f.elements['field2'].text ? f.elements['field2'].text : ""
field3 = f.elements['field3'].text ? f.elements['field3'].text : ""

csv_file.puts "#{field1}, #{field2}, #{field3}\n"

end
end

Personal Encryption

Safe Place color.jpg 298×299 pixels-2.pngHow do you decide on your passwords? Here are a few ways I have seen others done:

* Memorable one word e.g. *”Niiiiiiice”
* Memorable one word including pseudo randomness e.g. “7pumkin33″
* Merabable phrase e.g. “TwoCatsInTheGarden”

For many, it can be a challenge to dream up a “good” password. Thinking since it shouldn’t be written down, that it must be memorable.
What I have seen happen and have been guilty of this myself is that, we didn’t always know about what makes a good password and the inevitable need for more then one. When we signed up for that AOL account back in the 90s, the password we used then still comes to mind.

The first example I gave of “Niiiiiiice”, that was a root password of noted Security researcher Dan Kaminsky. He was recently shamed by a group of BlackHat hackers which owned one of his server.

When you are responsible for more then a few machines, password management becomes critical. I’ve seen spreadsheets and text files used for this. When an admin needs access to a machine, he will reference his document.

Rather then the memorable types noted above, Systems Admins typically use strong passwords. There was nothing memorable about the passwords I’ve seen from them. Documents that contain these passwords are only as safe as the location the user keeps them. If the admins system is compromised, so are all them system he manages.

One possible solution is to also password protect and encrypt the document right? Not really. Most likely that password is short and memorable. So what is the solution?

I would suggest that personal encryption might be the key. The name is something I have made up, however the method is not. Here is the concept. Let’s say you have this passwords “1T8Ft8\P-X#9le”. This password is written down on your document. Before using it on a system, add your memorable unwritten key “peerless” someplace in the middle of the password. In practice the password would be used like this “1T8Ftpeerless8\P-X#9le”. Peerless is your personal encryption key. No one should know this, and the password should never be written down with the key included.

Personal encryption in practice does not give us an excuse to write passwords on the walls. We should still take every precaution as usual.

Can IRB look good?

  • Tips
  • June 30, 2009

MagMyPic.com - Step 1_ Select a Magazine Cover

Professional Rails developers know that ’script/console’ is an essential tool for testing and debugging applications. However, it can be tricky to read the output of large Objects such as a query result set. Here is an example of what we typically see in an IRB result set:

Default-1

To get around this, for the most part we learn little tricks like this:

print MODEL.all.to_xml

Doing the above will give you a more eye friendly xml formatted version of the contents of your Object. This works decently granted you are not overriding the “to_xml” method and serving just a few of the Object attributes. There are other time consuming ways(but sometime necessary) methods of print out debug information. One way is to write a for loop, using “puts” statements, or perhaps write a method in IRB. There is a better way.

Hirb was develop to help soften this pain. It will pretty print ActiveRecord results sets in such a way as you my see in the Mysql console.

Installation
sudo gem install cldwalker-hirb --source http://gems.github.com

Quick Start with Rails

Its easy to try hirb out with rails by doing the following:

Setup Irb
bash> script/console
Loading local environment (Rails 2.3.2)
irb>> require 'hirb'
=> []
irb>> Hirb.enable
=> nil

Usage
MODEL.all

root@clickhere14_~ — ruby — 147×41-1

Enjoy!

A tip for Passenger(mod_rails) users

  • Tips
  • January 21, 2009

Passenger users, may I have your attention please. I have a tip for you.

Commit the tmp/restart.txt to your VCS.

Think about it… think of all the time I have just saved you.

your friend,

NC

script/console pretty print for xml

Alright Rails programmers, its that time again!
Its time for “Almost useless tip of the week!”

A tip so small, its barely worth the blog entry.

Today I wanted to see the “to_xml” output of an Object. And if your
like me, you spends more then your share of time in the deep dark ./script/console
of you rails app playing with your Objects(yum objects with jelly).

So here I was with this:

>> a.to_xml
=> "<?xml version="1.0" encoding="UTF-8"?>\n<asset>\n <assetable-type>IMG</assetable-type>\n  <content-type>image/jpeg</content-type>\n <created-at type="datetime">2008-06-10T10:07:23-05:00</created-at>\n <filename>ninja.jpg</filename>\n  <height type="integer">366</height>\n  <id type="integer">19</id>\n
<parent-id type="integer" nil="true"></parent-id>\n  <right-id type="integer" nil="true"></right-id>\n  <size type="integer">40643</size>\n
<thumbnail nil="true"></thumbnail>\n  <updated-at type="datetime">2008-06-10T10:07:23-05:00</updated-at>\n  <width type="integer">425</width>\n</asset>\n"

Beautiful eh?
Well how bout adding the “print” command first like this:

>> print a.to_xml
<?xml version="1.0" encoding="UTF-8"?>
<asset>
  <assetable-type>IMG</assetable-type>
  <content-type>image/jpeg</content-type>
  <created-at type="datetime">2008-06-10T10:07:23-05:00</created-at>
  <filename>ninja.jpg</filename>
  <height type="integer">366</height>
  <id type="integer">19</id>
<parent-id type="integer" nil="true"></parent-id>
  <right-id type="integer" nil="true"></right-id>
  <size type="integer">40643</size>
<thumbnail nil="true"></thumbnail>
  <updated-at type="datetime">2008-06-10T10:07:23-05:00</updated-at>
  <width type="integer">425</width>
</asset>

Now thats better.
Get back to work!

Bad news never looked so good

Recently I came across a product from Fiveruns called TuneUp. I wanted to share with you my initial impression.

Installation is a gem

Installation was a snap. Its a 3 step process

Install the gem

sudo gem install fiveruns_tuneup

Then install the plugin

fiveruns_tuneup /path/to/rails/app

Finally, you need to start your rails application. Browser over to it and you will see something new at the top of your application. It’s so clean looking, seems the say “I’m so clean, get busy and make your site as pretty as me.”
So the pretty bar is the TuneUp bar for which you will need to login.

So the MVC bar helps show you were most of the work is being done.

Then there is a breakout of how long each Model took to load. One thing I like about this is that performance is in your face everytime you run your app.

Those three haunting little colors will keep you up at night, thinking “Controller blue, is bigger the Model green! OMG I must fix this.”.

Then you will be noticing the milliseconds, “Why are my BloatedTables, taking 5.82 milliseconds to load, perhaps its the BloatedTables.find_by_sql calling some ungody query”.

Yes, I see this as a good thing.