require 'rubygems'
require 'hpricot'
require 'mysql'
require 'time'
doc = Hpricot::XML(File.read("Desktop/slips.bex"))
(doc/'plist').each do |sx|
(sx/'dict array dict').each do |sm|
(sm/'array').each do |st|
((st.parent/'key')[1].inner_html == 'comment') ? @billing_comment = (st.parent/'string')[0].inner_html : @billing_comment = ''
((st.parent/'key')[2].inner_html == 'comment') ? @billing_comment = (st.parent/'string')[1].inner_html : @billing_comment = ''
((st.parent/'key')[1].inner_html == 'category.name') ? @billing_category = (st.parent/'string')[0].inner_html : @billing_category = ''
if ((st.parent/'string')[2].inner_html == 'Billings')
@billing_description = ((st.parent/'string')[2].inner_html == 'Billings' ? (st.parent/'string')[3].inner_html : (st.parent/'string')[2].inner_html)
@billing_worker = ((st.parent/'string')[2].inner_html == 'Billings' ? (st.parent/'string')[4].inner_html : (st.parent/'string')[3].inner_html)
else
@billing_description = ((st.parent/'string')[4].inner_html == 'Billings' ? (st.parent/'string')[5].inner_html : (st.parent/'string')[4].inner_html)
@billing_worker = ((st.parent/'string')[4].inner_html == 'Billings' ? (st.parent/'string')[6].inner_html : (st.parent/'string')[5].inner_html)
end
(st/'dict').each do |sz|
@time_uuid = (sz/'string')[1].inner_html
@time_start = (sz/'date')[2].inner_html
@time_end = (sz/'date')[1].inner_html
@time_duration = (Time.parse(@time_end) - Time.parse(@time_start)) / 3600
begin
db = Mysql.real_connect("localhost", "username", "password", "dbname")
sth = db.prepare(
"INSERT INTO timeslips
(uuid, begin, end, title, duration, worker, description, category)
VALUES
(?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
count=count + 1, begin=?, end=?, title=?, duration=?, worker=?, description=?, category=?"
)
sth.execute(@time_uuid, @time_start, @time_end, @billing_description, @time_duration, @billing_worker, @billing_comment, @billing_category, @time_start, @time_end, @billing_description, @time_duration, @billing_worker, @billing_comment, @billing_category)
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
db.close if db
end
end end end end
Customated Code and Development