Class: Neoneo::Agent

Inherits:
WWW::Mechanize
  • Object
show all
Defined in:
lib/neoneo.rb

Overview

Wrapper around the WWW::Mechanize object to allow an easy and DRY error handling.

Ath the moment only the get, post and submit methods are subject of this error handling.

Instance Method Summary collapse

Instance Method Details

#get(url) ⇒ Object



78
79
80
81
# File 'lib/neoneo.rb', line 78

def get(url)
  page = super(url)
  handle_errors(page)
end

#handle_errors(page) ⇒ Object

This methos actually does the error handling.

When an error message is found in the response from No Kahuna it’s text determines which error is thrown. If the error message does not match any of the specific messages Neoneo tries to catch, a default Neoneo::Error is thrown.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/neoneo.rb', line 99

def handle_errors(page)
  return page if page.instance_of? WWW::Mechanize::File
  
  errors = page.search('div#flash.error p').map {|e| e.innerText}
  errors.each do |error|
    case error
    when 'Invalid login or password.'
      raise AuthenticationError
    else
      raise Error.new(e)  
    end
  end
  
  page
end

#post(url, options = {}) ⇒ Object



83
84
85
86
# File 'lib/neoneo.rb', line 83

def post(url, options = {})
  page = super(url, options)
  handle_errors(page)
end

#submit(form) ⇒ Object



88
89
90
91
# File 'lib/neoneo.rb', line 88

def submit(form)
  page = super(form)
  handle_errors(page)
end