Class: Lavin::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lavin/client.rb

Defined Under Namespace

Classes: Error, NoCurrentAsyncTaskError, ServerError

Constant Summary collapse

DEFAULT_HEADERS =
{
  'User-Agent'=> 'LavinLoadTest',
  'Accept' => '*/*',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
# File 'lib/lavin/client.rb', line 29

def initialize(base_url = nil)
  raise NoCurrentAsyncTaskError unless Async::Task.current?

  @internet = Async::HTTP::Internet.new
  @base_url = base_url
  @request_count = 0
  @cookie = nil
  @report_statistics = true
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#internetObject (readonly)

Returns the value of attribute internet.



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

def internet
  @internet
end

#report_statisticsObject

Returns the value of attribute report_statistics.



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

def report_statistics
  @report_statistics
end

#request_countObject

Returns the value of attribute request_count.



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

def request_count
  @request_count
end

Instance Method Details

#closeObject



39
40
41
# File 'lib/lavin/client.rb', line 39

def close
  internet.close
end

#request(method, url:, headers:, body: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lavin/client.rb', line 43

def request(method, url:, headers:, body: nil)
  url, headers, body = rewrite_request(url:, headers:, body:)

  start_time = Time.now
  response = internet.send(method, url, headers, body)
  duration = Time.now - start_time

  status, headers, body = process(response)

  Statistics.register_request(method:, url:, status:, duration:) if report_statistics

  {status:, headers:, body: body}
end