Class: HttpMonkey::Client

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

Defined Under Namespace

Classes: Environment, EnvironmentBuilder, Response

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



5
6
7
# File 'lib/http_monkey/client.rb', line 5

def initialize
  @conf = Configuration.new
end

Instance Method Details

#at(url) ⇒ Object



14
15
16
# File 'lib/http_monkey/client.rb', line 14

def at(url)
  HttpMonkey::EntryPoint.new(self, url)
end

#configure(&block) ⇒ Object



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

def configure(&block)
  @conf.instance_eval(&block) if block_given?
  self
end

#http_request(method, request) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http_monkey/client.rb', line 31

def http_request(method, request)
  client = self
  env = Client::EnvironmentBuilder.new(self, method, request).to_env

  begin
    code, headers, body = @conf.middlewares.execute(Middlewares::HttpRequest, env)
    body.close if body.respond_to?(:close)  # close when is a Rack::BodyProxy
  rescue => error
    raise unless @conf.behaviours.error_behaviour
    return @conf.behaviours.error_behaviour.call(client, request, error)
  end

  response = Client::Response.new(code, headers, body)
  behaviour = @conf.behaviours.find(response.code)
  behaviour.call(client, request, response)
end

#initialize_copy(source) ⇒ Object



9
10
11
12
# File 'lib/http_monkey/client.rb', line 9

def initialize_copy(source)
  super
  @conf = @conf.clone
end

#net_adapterObject



18
19
20
# File 'lib/http_monkey/client.rb', line 18

def net_adapter
  @conf.net_adapter
end

#storageObject



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

def storage
  @conf.storage
end