Skip to content

Instantly share code, notes, and snippets.

@mfilej
Created October 24, 2008 23:48
Show Gist options
  • Save mfilej/19627 to your computer and use it in GitHub Desktop.
Save mfilej/19627 to your computer and use it in GitHub Desktop.
simobil billing specification parser
require 'rubygems'
require 'hpricot'
require 'pp'
source = open('spec.xml')
doc = Hpricot(source)
ppl = Hash.new { |h,k| h[k] = { :call => 0.0, :sms => 0.0, :other => 0.0, :sum => 0.0 } }
doc.search(:zapis).each do |entry|
phone = entry.at(:stevilka).inner_html
price = entry.at(:eur).inner_html.to_f
type = case entry.at(:opis).inner_html
when /sms/i
:sms
when /znotraj/i, /klic/i
:call
else
:other
end
next if phone == "INTERNET.SIMOBIL.SI"
ppl[phone][type] += price
ppl[phone][:sum] += price
end
ppl.sort_by { |pair| -pair.last[:call] }.each_with_index do |data, i|
puts "#{"%2d" % (i+1)}: #{"%11s" % data.first} (#{data.last[:sum]}) #{data.last.inspect}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment