Class: WebTsunami::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/web_tsunami/session.rb

Overview

Session is a helper class to be used inside a scenario. It's purpose is to avoid low level manipulations to handle cookies and CSRF tokens automatically.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario, root_url) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
# File 'lib/web_tsunami/session.rb', line 11

def initialize(scenario, root_url)
  @scenario = scenario
  @root_url = root_url
  @cookies = {}
  @last_response = nil
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



9
10
11
# File 'lib/web_tsunami/session.rb', line 9

def cookies
  @cookies
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



9
10
11
# File 'lib/web_tsunami/session.rb', line 9

def last_response
  @last_response
end

#root_urlObject (readonly)

Returns the value of attribute root_url.



7
8
9
# File 'lib/web_tsunami/session.rb', line 7

def root_url
  @root_url
end

#scenarioObject (readonly)

Returns the value of attribute scenario.



7
8
9
# File 'lib/web_tsunami/session.rb', line 7

def scenario
  @scenario
end

Instance Method Details

#get(path, options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/web_tsunami/session.rb', line 18

def get(path, options = {}, &block)
  url = File.join(root_url, path)
  inject_headers(default_headers, options)
  scenario.get(url, options) do |response|
    @last_response = response
    save_cookies(response)
    block&.call(response)
  end
end

#post(path, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/web_tsunami/session.rb', line 28

def post(path, options = {}, &block)
  url = File.join(root_url, path)
  inject_headers(default_post_headers, options)
  inject_csrf_token(options)
  scenario.post(url, options) do |response|
    @last_response = response
    save_cookies(response)
    block&.call(response)
  end
end