module QuoraWebDriver::QuoraModule

Attributes

base_url[R]
url[R]
user[RW]

Public Class Methods

new(browser) click to toggle source
# File lib/quora-webdriver/quora.rb, line 7
def initialize(browser)
  @browser = browser
  @base_url = "https://www.quora.com/"
  @logged_in = false
end

Public Instance Methods

answers() click to toggle source
# File lib/quora-webdriver/quora.rb, line 95
def answers
  @url = "#{@base_url}#{self.stringify}/answers?share=1"
  self.goto
end
content(subcategory = :all) click to toggle source
# File lib/quora-webdriver/quora.rb, line 100
def content(subcategory = :all)
  case subcategory
  when :questions_added,
       :questions_followed,
       :answers,
       :posts
    @url = "#{@base_url}content?content_types=#{subcategory}"
  else
    @url = "#{@base_url}content"
  end
  self.goto :login
end
get_add_question_div() click to toggle source
# File lib/quora-webdriver/quora.rb, line 33
def get_add_question_div
  @browser.div({:class=>/inner/, :text=>/Add Question/})
end
get_all() click to toggle source
# File lib/quora-webdriver/quora.rb, line 119
def get_all
  last_size = 0
  while last_size != self.html.size do
    last_size = self.html.size
    @browser.scroll.to :bottom
    # sleep for 3 seconds to handle ajax request delay until page updates
    sleep 3
  end
end
get_login_email_text() click to toggle source
# File lib/quora-webdriver/quora.rb, line 21
def get_login_email_text
  @browser.text_field({:name=>"email", :class=>/header_login_text_box/})
end
get_login_password_text() click to toggle source
# File lib/quora-webdriver/quora.rb, line 25
def get_login_password_text
  @browser.text_field({:name=>"password", :class=>/header_login_text_box/})
end
get_login_submit_button() click to toggle source
# File lib/quora-webdriver/quora.rb, line 29
def get_login_submit_button
  @browser.button({:value=>"Login"})
end
goto(login = nil) click to toggle source
# File lib/quora-webdriver/quora.rb, line 83
def goto(login = nil)
 @browser.goto "#{@url}#{login.nil? ? '?share=1' : ''}"
  unless login.nil? then
    self.login
  end
end
home() click to toggle source
# File lib/quora-webdriver/quora.rb, line 90
def home
  @url = "#{@base_url}"
  self.goto :login
end
login(choice='0') click to toggle source
# File lib/quora-webdriver/quora.rb, line 37
def login(choice='0')
  return @logged_in if @logged_in
  until ['1', '2'].include?(choice)
    puts "This page requires a login.\n\t(1) Login via browser\n\t(2) Login via command line"
    choice = gets.chomp
  end
  case choice
  when '1'
    puts "Please navigate to the browser and login."
  when '2'
    email = ask("Enter email: "){ |x| x.echo = true }
    password = ask("Enter password: "){ |x| x.echo = "*" }
    email_box = get_login_email_text
    password_box = get_login_password_text
    unless email_box.exist? and password_box.exist? then
      puts "There was an error getting the login boxes. Please login manually. Report this error to someone important."
      self.login('1')
    end
    email_box.set email
    password_box.set password
    get_login_submit_button.click
  end

  self.verify_login
end
scroll_n_times(n = 1) click to toggle source
# File lib/quora-webdriver/quora.rb, line 113
def scroll_n_times(n = 1)
  n.times do
    @browser.scroll.to :bottom
  end
end
stringify() click to toggle source
# File lib/quora-webdriver/quora.rb, line 17
def stringify
  @user.to_s.gsub("_","-")
end
verify_login() click to toggle source
# File lib/quora-webdriver/quora.rb, line 63
def verify_login
  # check every 6 seconds for 1 minute
  login_count_checks = 0
  until @logged_in || (login_count_checks >= 60) do
    puts "\t Checking if logged in"
    @logged_in = get_add_question_div.exists?
    login_count_checks+=1
    sleep 1
  end

  if @logged_in then
    puts "Successfully logged in!"
  elsif @browser.text_field(:name=> "email", :class=>/header_login_text_box/).exist? then
    puts "There was an error logging in. Most likely a wrong username or password."
  else
    puts "There was an error logging in."
  end
  self.login
end