diff --git a/bin/autility b/bin/autility index d9742c1..11baaa0 100755 --- a/bin/autility +++ b/bin/autility @@ -83,7 +83,9 @@ command :lacaixa do |c| raise "Both --user and --password are required options." unless options.user && options.password result = Autility::LaCaixa.scrape(options.user, options.password, options.month, options.output_folder) - puts "Path: #{result}" + result.each do |document_path| + puts "Path: #{document_path}" + end end end diff --git a/lib/autility/lacaixa.rb b/lib/autility/lacaixa.rb index c1a6554..0a548be 100644 --- a/lib/autility/lacaixa.rb +++ b/lib/autility/lacaixa.rb @@ -39,17 +39,21 @@ def initialize(user, password, month, folder) @year = Time.now.year end - # Public: Scrapes the vodafone website and gets the invoice for the current + # Public: Scrapes the lacaixa website and gets the invoice for the current # month, saving it to @folder. # # Returns the String path of the saved document. - def scrape + def scrape(index=nil) setup_capybara log_in FileUtils.mkdir_p(@folder) - filename = "#{@folder}/lacaixa_#{month}_#{@year}.pdf" - document.save(filename) + if index + filename = "#{@folder}/lacaixa_#{month}_#{@year}__#{index}.pdf" + document(index).save(filename) + else + return document + end filename end @@ -69,7 +73,7 @@ def log_in # fetched yet). # # Returns the Document to be fetched. - def document + def document(index=nil) @document ||= begin params = {} url = "https://loc6.lacaixa.es" @@ -86,15 +90,24 @@ def document within_frame(all('frame')[1][:name]) do within_frame('Cos') do + sleep 3 find("#lbl_Varios a").click - p "CLICKED!" - binding.pry + sleep 3 wait_until { find('#enlaceDescr') } rows = all('.table_generica tr').select do |tr| tr.find("td").text =~ /#{month}\/#{@year}/ end - rows.first.find("a").click + if index + rows[index].find("a").click + else + docs = [] + rows.each_with_index do |row, idx| + scraper = LaCaixa.new(@user, @password, @month, @folder) + docs << scraper.scrape(idx) + end + return docs + end end end diff --git a/test/autility/lacaixa_test.rb b/test/autility/lacaixa_test.rb deleted file mode 100644 index ed66079..0000000 --- a/test/autility/lacaixa_test.rb +++ /dev/null @@ -1,32 +0,0 @@ -# encoding: utf-8 -require_relative '../test_helper' - -module Autility - describe LaCaixa do - subject { LaCaixa.new("foo", "bar", 11, "/tmp/utilities") } - - describe ".scrape" do - it 'delegates to an instance' do - LaCaixa.stubs(:new).with("foo", "bar", 11, "/tmp/utilities").returns subject - subject.expects(:scrape) - - LaCaixa.scrape("foo", "bar", 11, "/tmp/utilities") - end - end - - describe '#scrape' do - it 'gets the document in' do - subject.expects(:setup_capybara) - subject.expects(:log_in) - document = Document.new(url = stub, cookie = stub, {}) - subject.expects(:document).returns(document) - document.expects(:save) - - month = 11 - year = Time.now.year - - subject.scrape.must_equal "/tmp/utilities/lacaixa_#{month}_#{year}.pdf" - end - end - end -end