Class: Argosnap::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/argosnap/balance.rb

Overview

Given username and a password, fetch balance from www.tarsnap.com

Instance Method Summary collapse

Instance Method Details

#balanceObject

Fetch balance from tarsnap using



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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)
  (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_configurationObject

Elementary configuration file check



10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object

check for login errors



22
23
24
25
26
27
28
29
30
# File 'lib/argosnap/balance.rb', line 22

def (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