class Signaly::Client

interaction with signaly.cz (through the regular web interface intended for humans)

Constants

USERMENU_XPATH

Public Class Methods

new(config) click to toggle source
# File lib/signaly/client.rb, line 8
def initialize(config)
  @config = config
  @agent = Mechanize.new

  @dbg_print_pages = config.debug_output || false # print raw html of all request results?

  @checked_page = @config.url || 'https://www.signaly.cz/'
end

Public Instance Methods

login() click to toggle source

takes user name and password; returns a page (logged-in) or throws exception

# File lib/signaly/client.rb, line 21
def login
  page = @agent.get(@checked_page)
  debug_page_print "front page", page

  login_form = page.form_with(:id => 'frm-loginForm')
  unless login_form
    raise "Login form not found on the index page!"
  end
  login_form['name'] = @config.login
  login_form['password'] = @config.password

  page = @agent.submit(login_form)
  debug_page_print "first logged in", page

  usermenu = page.search(USERMENU_XPATH)
  usermenu &&= usermenu.xpath("./div[@class='item username']")
  if usermenu.empty? then
    raise "User-menu not found. Login failed or signaly.cz UI changed again."
  end

  return page
end
user_status() click to toggle source
# File lib/signaly/client.rb, line 44
def user_status
  status = Status.new
  page = @agent.get(@checked_page)
  debug_page_print "user main page", page

  menu = page.search(USERMENU_XPATH)

  pm = menu.search(".//a[@href='/vzkazy']")
  status[:pm] = find_num(pm.text)

  notif = menu.search(".//a[@href='/ohlasky']")
  status[:notifications] = find_num(notif.text)

  inv = menu.search(".//a[@href='/vyzvy']")
  if inv then
    status[:invitations] = find_num(inv.text)
  end

  return status
end

Private Instance Methods

debug_page_print(title, page) click to toggle source
# File lib/signaly/client.rb, line 67
def debug_page_print(title, page)
  return if ! @dbg_print_pages

  STDERR.puts
  STDERR.puts ColorizedString.new("# "+title).yellow
  STDERR.puts
  STDERR.puts page.search(".//div[@class='navbar navbar-fixed-top section-header']")
  STDERR.puts
  STDERR.puts "-" * 60
  STDERR.puts
end
find_num(str, default=0) click to toggle source

finds the first integer in the string and returns it

# File lib/signaly/client.rb, line 80
def find_num(str, default=0)
  m = str.match /\d+/
  return m ? m[0].to_i : default
end