ckundo/sandy — pull request #2 by jpettersson (2012-11-05 08:18:27+00:00)
Title: StormCentral and Areas as nested sets

Hi,

I started the migration to the official Sandy gem earlier this evening. While I was rebuilding my seed script I noticed something funny about the LIPA data and realized that they have an additional level of areas.

Internally in my app I'm using the ancestry gem to handle the the data as a nested set. Because of this I could easily support Providers with different levels, but I found that the current LIPA provider was skipping one level.

So I started thinking about ways of fixing that at first, then I realized the similarities between the ConEd and LIPA data sources. I also found that the National Grid service looks very similar: https://www1.nationalgridus.com/PowerOutageMap

They are all most likely running the same system which could be some kind of industry standard (we can probably add National Grid effortlessly).

Taking this into consideration I made a few suggested modifications to the gem:

* Refactored Sandy::Area to function as a nested set and in the process normalized the interface to only offer Report.areas (which can be traversed on the children attribute) instead of regions & neighborhoods.

* Created a common utility class for ConEd & LIPA called StormCenter (the name they both use for their map). This class contains a method called recursive_fetch_areas which traverses the XML tree and instantiates nested Sandy::Area objects. The slight differences between the ConEd and LIPA XML tree structures are handled by a lambda that is passed in as the second argument to recursive_fetch_areas. This lambda selects the scope of the next level of areas from the tree.

Here you can see how I use this functionality:
I have a recursive method that traverses the Sandy::Area structures from both providers to create areas as they become available and sample new values as they are updated.
https://github.com/jpettersson/nycpowerstatus.com/blob/sandy/app/models/provider.rb

Anyway, it's 3 am and my brain is mush, let me know what you think!

Best,
Jonathan