Class: MoneseCsvDownloader::WebClient
- Inherits:
-
Object
- Object
- MoneseCsvDownloader::WebClient
- Defined in:
- lib/monese_csv_downloader/web_client.rb
Instance Method Summary collapse
- #browser ⇒ Object
-
#chrome_browser ⇒ Object
Unused, I wasn't able to get downloads to a specific path working with this.
-
#download(account_type) ⇒ Object
account_type: :personal, or :joint.
- #downloaded_file ⇒ Object
- #firefox_browser ⇒ Object
-
#initialize(monese_account_phone_number, monese_account_password) ⇒ WebClient
constructor
A new instance of WebClient.
- #temporary_directory ⇒ Object
Constructor Details
#initialize(monese_account_phone_number, monese_account_password) ⇒ WebClient
Returns a new instance of WebClient.
7 8 9 10 |
# 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 |
Instance Method Details
#browser ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# 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 ⇒ Object
Unused, I wasn't able to get downloads to a specific path working with this.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
account_type: :personal, or :joint
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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.(data_testid: "login-submit-button").click self.browser.text_field(data_testid: "input-password").send_keys @monese_account_password self.browser.(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.(data_testid: "download-statement-button").click self.browser.div(class: /DownloadModal-module__InputWrapper/, index: 1).click self.browser.(data_testid: "csv").click self.browser.(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 ⇒ Object
23 24 25 |
# 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 ⇒ Object
50 51 52 53 54 55 56 57 |
# 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 ⇒ Object
27 28 29 |
# File 'lib/monese_csv_downloader/web_client.rb', line 27 def temporary_directory @temporary_directory ||= Dir.mktmpdir end |