class MoneseCsvDownloader::WebClient

Public Class Methods

new(monese_account_phone_number, monese_account_password) click to toggle source
# File lib/monese_csv_downloader/web_client.rb, line 7
def initialize(monese_account_phone_number, monese_account_password)
  @monese_account_phone_number = monese_account_phone_number
  @monese_account_password = monese_account_password
end

Public Instance Methods

browser() click to toggle source
# File lib/monese_csv_downloader/web_client.rb, line 12
def browser
  sleep(1) # Act like a human
  @browser ||= self.firefox_browser

  @screenshot_count ||= 0
  @screenshot_count += 1
  @browser.screenshot.save "/tmp/monese-csv-downloader-screenshot-#{@screenshot_count}.png"

  @browser
end
chrome_browser() click to toggle source

Unused, I wasn't able to get downloads to a specific path working with this.

# File lib/monese_csv_downloader/web_client.rb, line 32
def chrome_browser
  chrome_prefs = {
    :download => {
      :prompt_for_download => false,
      :directory_upgrade => true,
      :default_directory => "/tmp/chrome_downloads"
    },
    :savefile => {
      :default_directory => "/tmp/chrome_downloads"
    },
    :profile => {
      default_content_settings: { popups: 0 }
    }
  }

  Watir::Browser.new :chrome, options: { prefs: chrome_prefs }
end
download(account_type) click to toggle source

account_type: :personal, or :joint

# File lib/monese_csv_downloader/web_client.rb, line 60
def download(account_type)
  # Login
  self.browser.goto "https://web.monese.com/"
  self.browser.text_field(data_testid: "login-input").send_keys @monese_account_phone_number[4..-1]
  self.browser.button(data_testid: "login-submit-button").click
  self.browser.text_field(data_testid: "input-password").send_keys @monese_account_password
  self.browser.button(data_testid: "form-submit").click

  if account_type.eql?(:joint)
    self.browser.a(data_testid: /account-link-\d-MONESE_JOINT/).click
  elsif account_type.eql?(:personal)
    self.browser.a(data_testid: /account-link-\d-PRIVATE/).click
  else
    raise ArgumentError, "Invalid account_type: #{account_type}"
  end

  self.browser.button(data_testid: "download-statement-button").click
  self.browser.div(class: /DownloadModal-module__InputWrapper/, index: 1).click
  self.browser.button(data_testid: "csv").click
  self.browser.button(text: /Download/).click

  sleep 10 # Wait for file download to complete

  File.read(self.downloaded_file)
rescue StandardError
  browser.screenshot.save '/tmp/failure_screenshot.png'
  puts "Saved failure screenshot to /tmp/failure_screenshot.png"
  raise
ensure
  self.browser.close
end
downloaded_file() click to toggle source
# File lib/monese_csv_downloader/web_client.rb, line 23
def downloaded_file
  File.join(self.temporary_directory, (Dir.entries(self.temporary_directory) - [".", ".."]).first)
end
firefox_browser() click to toggle source
# File lib/monese_csv_downloader/web_client.rb, line 50
def firefox_browser
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['browser.download.folderList'] = 2 # custom location
  profile['browser.download.dir'] = self.temporary_directory
  profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv,application/pdf'

  Watir::Browser.new :firefox, options: { profile: profile }
end
temporary_directory() click to toggle source
# File lib/monese_csv_downloader/web_client.rb, line 27
def temporary_directory
  @temporary_directory ||= Dir.mktmpdir
end