Class: Sakura::Client

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



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

def initialize
  @domain, @passwd = credentials
end

Class Attribute Details

.verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



15
16
17
# File 'lib/sakura/client.rb', line 15

def domain
  @domain
end

Class Method Details

.current_sessionObject



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

def current_session
  @current_session ||= new
end

Instance Method Details

#get(url, expected) ⇒ Object

Raises:

  • (Timeout::Error)


59
60
61
62
63
64
65
66
67
68
# File 'lib/sakura/client.rb', line 59

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

#loginObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sakura/client.rb', line 35

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

Returns:

  • (Boolean)


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

def login?
  @logged_in
end

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

Yields:

  • (page)


70
71
72
73
74
75
76
77
78
79
# File 'lib/sakura/client.rb', line 70

def process(url, expected)
   unless login?

  get url, expected
  yield page

  raise_when_error
  wait_for_loading
  page
end