class Argosnap::Fetch
Given username and a password, fetch balance from www.tarsnap.com
Public Instance Methods
balance()
click to toggle source
Fetch
balance from tarsnap using
# File lib/argosnap/balance.rb, line 33 def balance check_configuration agent = Mechanize.new page = agent.get('https://www.tarsnap.com/account.html') form = page.form_with(action: 'https://www.tarsnap.com/manage.cgi') form.address = config.data[:email] form.password = config.data[:password] panel = agent.submit(form) check_login_errors(panel) picodollars = panel.parser.to_s.scan(/\$\d+\.\d+/)[0].scan(/\d+\.\d+/)[0].to_f.round(4) config.logger.info("Current amount of picoUSD: #{picodollars}") picodollars end
check_configuration()
click to toggle source
Elementary configuration file check
# File lib/argosnap/balance.rb, line 10 def check_configuration Install.new.ensure_compatibility Install.new.ensure_installation if config.data[:email].empty? config.log_and_abort("Please add your tarsnap email to #{config}") end if config.data[:password].empty? config.log_and_abort("Please add your tarsnap password to #{config}") end end
check_login_errors(data)
click to toggle source
check for login errors
# File lib/argosnap/balance.rb, line 22 def check_login_errors(data) wrong_email_message = 'No user exists with the provided email address; please try again.' wrong_password_message = 'Password is incorrect; please try again.' if data.body.include?(wrong_email_message) config.log_and_abort('Password is incorrect; please try again.') elsif data.body.include?(wrong_password_message) config.log_and_abort('Bad password. Please check your configuration file tarsnap password!') end end
Private Instance Methods
config()
click to toggle source
load configuration files
# File lib/argosnap/balance.rb, line 51 def config Configuration.new end