Class: WsdlMapper::Runtime::Middlewares::SimpleDispatcher

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb

Direct Known Subclasses

AsyncDispatcher

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SimpleDispatcher) initialize(connection = Faraday.new, auto_retry = false, logger = nil)

Returns a new instance of SimpleDispatcher

Parameters:

  • connection (Faraday::Connection) (defaults to: Faraday.new)

    A faraday connection to use.



15
16
17
18
19
# File 'lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb', line 15

def initialize(connection = Faraday.new, auto_retry = false, logger = nil)
  @cnx = connection
  @auto_retry = auto_retry
  @logger = logger || Logger.new(File::NULL).tap { |l| l.level = Logger::FATAL }
end

Instance Attribute Details

- (Object) cnx (readonly)

Returns the value of attribute cnx



11
12
13
# File 'lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb', line 11

def cnx
  @cnx
end

- (Object) logger

Returns the value of attribute logger



12
13
14
# File 'lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb', line 12

def logger
  @logger
end

Instance Method Details

- (Array<WsdlMapper::Runtime::Operation, Faraday::Response>) call(operation, request)

Dispatches the request via the configured #cnx and returns the HTTP response.

Parameters:

Returns:

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb', line 26

def call(operation, request)
  retries = 0
  begin
    http_response = execute_request(request)

    [operation, http_response]
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Faraday::Error => e
    if @auto_retry && retries <= @auto_retry
      retries += 1
      @logger.debug { "Error occured: #{e}, attempting retry #{retries}."}
      retry
    end
    raise TransportError.new(e.message, e, request)
  rescue HTTPError => e
    if @auto_retry && retries <= @auto_retry
      retries += 1
      @logger.debug { "Error occured: #{e}, attempting retry #{retries}."}
      retry
    end
    raise e
  end
end

- (Object) execute_request(request)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wsdl_mapper/runtime/middlewares/simple_dispatcher.rb', line 49

def execute_request(request)
  http_response = cnx.post do |c|
    c.url request.url
    c.body = request.xml

    request.http_headers.each do |key, val|
      c[key] = val
    end
  end

  if http_response.status.to_s !~ /^2/
    raise HTTPError.new(http_response.status, http_response.body, nil, request)
  end

  http_response
end