class Sakura::Client

Attributes

domain[R]

Public Class Methods

current_session() click to toggle source
# File lib/sakura/client.rb, line 17
def current_session
  @current_session ||= new
end
new() click to toggle source
# File lib/sakura/client.rb, line 26
def initialize
  @domain, @passwd = credentials
end
verbose=(bool) click to toggle source
# File lib/sakura/client.rb, line 21
def verbose=(bool)
  @@verbose = !!bool
end

Public Instance Methods

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

  $stderr.puts "visit #{url}" if @@verbose
  visit url
  wait_for_loading
  unless page.text =~ expected
    raise Timeout::Error.new('Timed out')
  end

  page
end
login() click to toggle source
# File lib/sakura/client.rb, line 34
def login
  $stderr.puts 'login' if @@verbose

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

  wait_for_loading

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

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

  get url, expected
  yield page

  raise_when_error
  page
end

Private Instance Methods

credentials() click to toggle source
# File lib/sakura/client.rb, line 77
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 88
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 95
def wait_for_loading
  5.times do
    if find_all('読み込み中').empty?
      break
    else
      $stderr.puts 'still loading ...' if @@verbose
    end
  end
end