Power-Set Method For Ruby Hash

# The Array power set is stolen from http://snippets.dzone.com/posts/show/3524
class Array
  # Returns the "power set" for this Array. This means that an array with all
  # subsets of the array's elements will be returned.
  def power_set
    # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
    inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
  end
end

class Hash
  def power_set
    # Returns the "power set" for this Hash. This means that a array with hashes of all
    # subsets of the hash's (key => value) pairs will be returned.
    # Example:
    # >> {:feedback_type=>"", :language_code=>"", :comment=>""}.power_set
    #
    # [{}, {:comment=>""}, {:language_code=>""}, {:language_code=>"", :comment=>""}, {:feedback_type=>""}, {:feedback_type=>"", :comment=>""}, {:feedback_type=>"", :language_code=>""}, {:feedback_type=>"", :language_code=>"", :comment=>""}]

    hash_to_array = self.to_a
    array_power_set = hash_to_array.power_set
    hash_power_set = array_power_set.collect { |pairs| pairs.inject({}) { |hash,pair| hash[pair[0]] = pair[1]; hash } }
    hash_power_set
  end
end

How to empty a Gmail label with FireWatir

When you have a huge label with thousands of messages, Gmail can’t handle deleting all of them at a time (it says it can, but for me it’s never worked). This solves the problem.

I wrote it more as a practice to get familiar with FireWatir, so it’s not very pretty or anything and it’s a bit slow. I think WWW::Mechanize would be faster at it (although you wouldn’t have the eye-candy of seeing the live action on the browser ;-). I’ll port it to Mechanize, once I have some time, as a bootstrap into learning that too. I’ll have to subscribe to a few more lists in order to have testing ammo for all that. ;-)

If you have doubts on how to use or suggestions on how to improve it please feel free to contact me. It assumes, by the way, that Gmail is in German. Replace that string with the one that comes up for your language.

P.S.: there’s also a syntax highlighted version. Does anyone know of a way to highlight the code here in WordPress?

def empty_label(label)
  # you need a Firefox instance already running with JSSh installed and listening
  ff = Firefox.new
  # Goes to Gmail in HTML
  ff.goto('http://mail.google.com/mail/h/x4odcwnm5f5v/?s=l&l=#{label}')
  a = []
  # doing 'c.set' didn't work for me here so I had to do this hack of getting the value
  # and then acquiring the element through ff.checkbox(:value,value)
  ff.checkboxes.each {|c| a << c.value}

  while a.length > 0 do
    c = []
    a.each {|v| c << ff.checkbox(:value,v)}
    # checks all checkboxes
    c.each {|e| e.set}
    # clicks the drop-down entry for deleting
    ff.select_list(:name,'tact').select('In den Papierkorb verschieben')
    ff.button(:name,'nvp_tbu_go').click
    a = []
    ff.checkboxes.each {|c| a << c.value}
  end
end
Posted in Uncategorized. Tags: , , , , , . Leave a Comment »

One feed per tag in WordPress

WordPress lets you choose to see only the entries with a specified tag from a blog, like, say, http://obvio171.wordpress.com/tag/soc. Thinking it would also have one feed per tag in the same way, I added that address to Planet-Soc as my soc feed.

To my great surprised, it crawled all of my posts and dumped it into the planet (sorry about that guys). I don’t know if they actually have a way to get one feed per tag, but I couldn’t find it, so I wrote the following:

#! /usr/bin/ruby
require 'rss'
rss = RSS::Parser.parse(open('http://obvio171.wordpress.com/feed/'))
channel = RSS::Rss::Channel.new
rss.channel.items.select {|i| !(i.categories.select {|c| c.content == "soc"}).empty?}.each {|i| channel.items << i}
channel.link = rss.channel.link
channel.title = rss.channel.title
channel.description = rss.channel.description
channel.generator = rss.channel.generator
rss.channel = channel
begin
File.delete('rss_soc.xml')
rescue
end
File.new('rss_soc.xml','w') << rss.to_xml

Now I only have to find a way to remove the old feed from planet-soc and add the new one (which btw is hosted at http://www.students.ic.unicamp.br/~ra033245/rss_soc.xml if anyone’s interested).

Posted in Uncategorized. Tags: , , , , , . 2 Comments »