Skip to content

Instantly share code, notes, and snippets.

@everdaniel
Forked from foca/itau.rb
Created April 12, 2013 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdaniel/5371628 to your computer and use it in GitHub Desktop.
Save everdaniel/5371628 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
require 'mechanize'
require 'money'
require 'ostruct'
module Itau
class Session < Struct.new(:ci, :pass)
def login
login_page = agent.get(itaurl)
login_form = login_page.form_with(:name => "form1")
login_form.set_fields(:nro_documento => ci, :password => pass)
Dashboard.new(login_form.submit)
end
def logout
agent.get(itaurl)
end
def agent
@agent ||= Mechanize.new do |agent|
agent.user_agent_alias = "Windows IE 7"
end
end
def itaurl
"https://www.itaulink.com.uy/appl/index.jsp"
end
end
class Dashboard < Struct.new(:page)
CURRENCIES = {
"Pesos" => "UYU",
"Dólares" => "USD",
"Euros" => "EUR"
}
def checking_accounts
account_links("Cuentas Corrientes") do |link|
Account.new(:checking, account_currency(link), link.text, link.click)
end
end
def savings_accounts
account_links("Cajas de Ahorro") do |link|
Account.new(:savings, account_currency(link), link.text, link.click)
end
end
private
def account_currency(link)
curr = link.node.search("../../following-sibling::*[./font[contains(.,'Pesos') or contains(.,'lares') or contains(.,'Euros')]]")
CURRENCIES.fetch(curr.text.strip)
end
def account_links(text, &block)
page.links.select do |link|
link.node.xpath("../../../..//font[contains(.,'#{text}')]").any?
end.map(&block)
end
end
class Account < Struct.new(:type, :currency, :number, :page)
def transactions
page.search(".//tr[./td[contains(.,'Concepto')]]/following-sibling::*").map do |tr|
cells = tr.search("td").map(&:text)
cells[2..-1].map! { |s| "#{s} #{currency}".to_money if s =~ /\d+/ }
Transaction.new(*cells)
end
end
end
class Transaction < Struct.new(:date, :description, :expense, :income, :balance)
@@date = lambda do |str|
months = %W(ENE FEB MAR ABR MAY JUN JUL AGO SET OCT NOV DIC)
matches = str.match /(?<day>\d{2})(?<month>\w{3})(?<year>\d{4})/
matches && Date.new(matches[:year].to_i, months.index(matches[:month]) + 1, matches[:day].to_i)
end
def initialize(date, *args)
super @@date[date], *args
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment