module AtCoderFriends::Scraping::Authentication

fetch pages and authenticates user at login page if needed

Constants

XPATH_USERNAME

Public Instance Methods

fetch_raw(url) click to toggle source
# File lib/at_coder_friends/scraping/authentication.rb, line 21
def fetch_raw(url)
  begin
    return agent.get(url)
  rescue Mechanize::ResponseCodeError => e
    raise e if username_link(e.page)
  end

  agent.get(common_url('login') + '?continue=' + CGI.escape(url))
end
fetch_with_auth(url) click to toggle source
# File lib/at_coder_friends/scraping/authentication.rb, line 13
def fetch_with_auth(url)
  page = fetch_raw(url)
  page.uri.path == '/login' && page = post_login(page)
  page.uri.path == '/login' && (raise AppError, 'Authentication failed.')
  show_username(page)
  page
end
post_login(page) click to toggle source
# File lib/at_coder_friends/scraping/authentication.rb, line 31
def post_login(page)
  user, pass = read_auth
  form = page.forms[1]
  form.field_with(name: 'username').value = user
  form.field_with(name: 'password').value = pass
  form.submit
end
read_auth() click to toggle source
# File lib/at_coder_friends/scraping/authentication.rb, line 39
def read_auth
  user = ctx.config['user'].to_s
  if user.empty?
    print('enter username:')
    user = STDIN.gets.chomp
  end

  pass = ctx.config['password'].to_s
  if pass.empty?
    print("enter password for #{user}:")
    pass = STDIN.noecho(&:gets).chomp
    puts
  end
  [user, pass]
end
show_username(page) click to toggle source
# File lib/at_coder_friends/scraping/authentication.rb, line 55
def show_username(page)
  username_bak = @username
  link = username_link(page)
  @username = (link ? link.text.strip : '-')
  return if @username == username_bak || @username == '-'

  puts "logged in as #{@username}"
end