Class: CompaniesHouseInputGateway::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/companies_house_input_gateway/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, config) ⇒ Request

Returns a new instance of Request.



5
6
7
8
# File 'lib/companies_house_input_gateway/request.rb', line 5

def initialize(connection, config)
  @connection = connection
  @config = config
end

Instance Method Details

#perform(request_type, request_data = {}, require_submission_form) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/companies_house_input_gateway/request.rb', line 10

def perform(request_type, request_data = {}, require_submission_form)
  xml_form = XmlFormsBinder.new(request_type, @config, request_data)

  request_type = Util.camelize(request_type)
  unless Constants::SUPPORTED_REQUESTS.include?(request_type)
    msg = "Unsupported request type #{request_type}"
    raise CompaniesHouseGatewayError, msg
  end

  response = @connection.post do |request|
    request.path = @config[:api_endpoint]
    request_body = xml_form.build_request_xml(require_submission_form).to_s
    if defined?(Rails)
      Rails.logger.tagged('CompaniesHouseInputGateway', 'XmlRequest') do
        Rails.logger.info xml_data: request_body
      end
    end
    request.body = request_body
    request.options.open_timeout = @config[:open_timeout]
    request.options.timeout = @config[:timeout]
  end

  xml_response = @config[:raw] ? response : response.body
  if defined?(Rails)
    Rails.logger.tagged('CompaniesHouseInputGateway', 'XmlResponse') do
      Rails.logger.info xml_data: xml_response
    end
  end
  xml_response
rescue Faraday::ClientError => e
  if e.response.nil?
    raise CompaniesHouseGatewayError
  else
    raise CompaniesHouseGatewayError.new(e.response[:body],
                                         e.response[:status],
                                         e.response)
  end
rescue Faraday::ServerError => e
  raise CompaniesHouseGatewayError.new(e.response[:body],
                                       e.response[:status],
                                       e.response)
rescue Faraday::ParsingError => e
  raise CompaniesHouseGatewayError.new(e.response[:body],
                                       e.response[:status],
                                       e.response)
end