Class: Sakura::Client

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/sakura/client.rb

Constant Summary collapse

@@verbose =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



26
27
28
# File 'lib/sakura/client.rb', line 26

def initialize
  @domain, @passwd = credentials
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



13
14
15
# File 'lib/sakura/client.rb', line 13

def domain
  @domain
end

Class Method Details

.current_sessionObject



17
18
19
# File 'lib/sakura/client.rb', line 17

def current_session
  @current_session ||= new
end

.verbose=(bool) ⇒ Object



21
22
23
# File 'lib/sakura/client.rb', line 21

def verbose=(bool)
  @@verbose = !!bool
end

Instance Method Details

#get(url, expected) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sakura/client.rb', line 52

def get(url, expected)
   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

#loginObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sakura/client.rb', line 34

def 
  $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?Boolean

Returns:



30
31
32
# File 'lib/sakura/client.rb', line 30

def login?
  @logged_in
end

#process(url, expected) {|page| ... } ⇒ Object

Yields:

  • (page)


65
66
67
68
69
70
71
72
73
# File 'lib/sakura/client.rb', line 65

def process(url, expected, &block)
   unless login?

  get url, expected
  yield page

  raise_when_error
  page
end