Class: Dinero::Bank::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dinero/banks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dinero/banks.rb', line 9

def initialize options
  opts = default_options.merge options

  @username = opts[:username]
  @password = opts[:password]
  @login_url = opts[:login_url]
  @security_questions = opts[:security_questions] || []
  @timeout = opts[:timeout] || DEFAULT_TIMEOUT
  @authenticated = false
  validate!
end

Instance Attribute Details

#login_urlObject (readonly)

Returns the value of attribute login_url.



7
8
9
# File 'lib/dinero/banks.rb', line 7

def 
  @login_url
end

#passwordObject (readonly)

Returns the value of attribute password.



6
7
8
# File 'lib/dinero/banks.rb', line 6

def password
  @password
end

#security_questionsObject (readonly)

Returns the value of attribute security_questions.



6
7
8
# File 'lib/dinero/banks.rb', line 6

def security_questions
  @security_questions
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/dinero/banks.rb', line 7

def timeout
  @timeout
end

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/dinero/banks.rb', line 6

def username
  @username
end

Instance Method Details

#accounts_summary_documentObject



62
63
64
65
66
67
# File 'lib/dinero/banks.rb', line 62

def accounts_summary_document
  return @accounts_summary_document if @accounts_summary_document

  goto_accounts_summary_page
  @accounts_summary_document = Nokogiri::HTML connection.page_source
end

#after_successful_loginObject



69
70
71
# File 'lib/dinero/banks.rb', line 69

def 
  # NOP
end

#authenticated?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dinero/banks.rb', line 54

def authenticated?
  !!@authenticated
end

#class_nameObject



73
74
75
# File 'lib/dinero/banks.rb', line 73

def class_name
  self.class.to_s.downcase.gsub("dinero::bank::", '')
end

#connectionObject



50
51
52
# File 'lib/dinero/banks.rb', line 50

def connection
  @connection ||= establish_connection
end

#default_optionsObject



27
28
29
# File 'lib/dinero/banks.rb', line 27

def default_options
  {}
end

#establish_connectionObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/dinero/banks.rb', line 39

def establish_connection
  capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs(
    'phantomjs.page.settings.userAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:50.0) Gecko/20100101 Firefox/50.0',
    'service_args' => ['--ignore-ssl-errors=true', '--ssl-protocol=any']
  )

  driver = Selenium::WebDriver.for :phantomjs, :desired_capabilities => capabilities
  driver.manage.window.size = Selenium::WebDriver::Dimension.new(1640, 768)
  driver
end

#find_answer(question) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/dinero/banks.rb', line 31

def find_answer question
  if q = security_questions.detect{ |qa| qa["question"] == question }
    return q["answer"]
  else
    raise "Unknown security question: #{question.inspect}"
  end
end

#goto_login_pageObject



96
97
98
99
# File 'lib/dinero/banks.rb', line 96

def 
  connection.navigate.to 
  snap "#{class_name}_login_page.png"
end

#login!Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/dinero/banks.rb', line 101

def login!
  return if authenticated?
  screenshot_on_error do
    
    post_credentials!
    wait.until { on_accounts_summary_page? }
    
    @authenticated = true
  end
end

#screenshot_on_error(name = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dinero/banks.rb', line 82

def screenshot_on_error name = nil
  begin
    yield
  rescue
    unless name
      class_name, method_name = caller.first.match(/(\w+)\.rb\:\d+\:in\s\`([^\']+)/).captures
      name = "#{class_name}_#{method_name.gsub(/\W/, '')}"
    end
    snap "#{name}_error" unless @captured_error
    @captured_error = true
    raise
  end
end

#snap(filename) ⇒ Object



77
78
79
80
# File 'lib/dinero/banks.rb', line 77

def snap filename
  filename = filename + '.png' unless filename =~ /\.png$/
  connection.save_screenshot "log/#{filename}"
end

#validate!Object



21
22
23
24
25
# File 'lib/dinero/banks.rb', line 21

def validate!
  raise "Must supply :username" if @username.to_s.empty?
  raise "Must supply :password" if @password.to_s.empty?
  raise "Must have a :login_url" if @login_url.to_s.empty?
end

#waitObject



58
59
60
# File 'lib/dinero/banks.rb', line 58

def wait
  @wait ||= Selenium::WebDriver::Wait.new(:timeout => timeout)
end