Class: Ubcbooker::Scraper::BaseScraper
- Inherits:
-
Object
- Object
- Ubcbooker::Scraper::BaseScraper
- Defined in:
- lib/ubcbooker/scrapers/base_scraper.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(username, password) ⇒ BaseScraper
constructor
A new instance of BaseScraper.
- #is_logged_in(page) ⇒ Object
-
#login_ubc_cwl(login_page) ⇒ Object
Do login for UBC CWL system.
- #populate_account_info(login_page) ⇒ Object
Constructor Details
#initialize(username, password) ⇒ BaseScraper
Returns a new instance of BaseScraper.
4 5 6 7 8 9 10 |
# File 'lib/ubcbooker/scrapers/base_scraper.rb', line 4 def initialize(username, password) @agent = Mechanize.new do |agent| agent.user_agent_alias = "Linux Mozilla" end @username = username @password = password end |
Instance Method Details
#is_logged_in(page) ⇒ Object
12 13 14 15 16 |
# File 'lib/ubcbooker/scrapers/base_scraper.rb', line 12 def is_logged_in(page) page_body = Nokogiri::HTML(page.body) login_status_text = page_body.css("p").first.text return !login_status_text.include?("Login Failed") end |
#login_ubc_cwl(login_page) ⇒ Object
Do login for UBC CWL system
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ubcbooker/scrapers/base_scraper.rb', line 28 def login_ubc_cwl(login_page) begin after_login_page = populate_account_info(login_page) if is_logged_in(after_login_page) return after_login_page else raise Ubcbooker::Error::LoginFailed end rescue Ubcbooker::Error::LoginFailed => e puts e. exit(1) end end |
#populate_account_info(login_page) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/ubcbooker/scrapers/base_scraper.rb', line 18 def populate_account_info(login_page) username_feild = login_page.form.field_with(name: "j_username") username_feild.value = @username password_field = login_page.form.field_with(name: "j_password") password_field.value = @password redirect_page = login_page.form.submit return redirect_page.form.submit end |