class Sakura::Client

Attributes

verbose[RW]
domain[R]

Public Class Methods

current_session() click to toggle source
# File lib/sakura/client.rb, line 22
def current_session
  @current_session ||= new
end
new() click to toggle source
# File lib/sakura/client.rb, line 27
def initialize
  @domain, @passwd = credentials
end

Public Instance Methods

get(url, expected) click to toggle source
# File lib/sakura/client.rb, line 59
def get(url, expected)
  login unless login?

  warn "visit #{url}" if self.class.verbose
  visit url
  wait_for_loading
  raise Timeout::Error, 'Timed out' unless page.text =~ expected

  page
end
login() click to toggle source
# File lib/sakura/client.rb, line 35
def login
  warn 'login' if self.class.verbose

  visit BASE_URL
  fill_in 'username', with: @domain
  fill_in 'password', with: @passwd
  find('form button[type=submit]').click

  if has_text?('認証コード')
    puts '認証コード:'
    otp = $stdin.gets

    fill_in 'login-otp', with: otp
    find('form button[type=submit]').click
  end

  wait_for_loading

  @logged_in = true if page.text =~ /サーバーコントロールパネル ホーム/

  raise_when_error
  login?
end
login?() click to toggle source
# File lib/sakura/client.rb, line 31
def login?
  @logged_in
end
process(url, expected) { |page| ... } click to toggle source
# File lib/sakura/client.rb, line 70
def process(url, expected)
  login unless login?

  get url, expected
  yield page

  raise_when_error
  wait_for_loading
  page
end

Private Instance Methods

credentials() click to toggle source
# File lib/sakura/client.rb, line 83
def credentials
  warn 'SAKURA_DOMAIN is not set' unless ENV['SAKURA_DOMAIN']
  warn 'SAKURA_PASSWD is not set' unless ENV['SAKURA_PASSWD']

  if ENV['SAKURA_DOMAIN'] && ENV['SAKURA_PASSWD']
    [ENV['SAKURA_DOMAIN'], ENV['SAKURA_PASSWD']]
  else
    exit 1
  end
end
raise_when_error() click to toggle source
# File lib/sakura/client.rb, line 94
def raise_when_error
  %w[.error .input-error].each do |cls|
    error = page.all(cls)
    raise error.first.text unless error.empty?
  end
end
wait_for_loading() click to toggle source
# File lib/sakura/client.rb, line 101
def wait_for_loading
  5.times do
    break if all('読み込み中').empty?

    warn 'still loading ...' if self.class.verbose
  end
end