Class: CompaniesHouseGateway::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(connection, config) ⇒ Request

Returns a new instance of Request.



3
4
5
6
# File 'lib/companies_house_gateway/request.rb', line 3

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

Instance Method Details

#build_request_xml(request_type, request_data = {}) ⇒ Object

Compile the complete XML request to send to Companies House



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/companies_house_gateway/request.rb', line 34

def build_request_xml(request_type, request_data={})
  transaction_id = (Time.now.to_f * 100).to_i

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.GovTalkMessage(header_namespace) do
      xml.EnvelopeVersion "2.0"
      xml.Header do
        message_details(xml, request_type, transaction_id)
        authentication(xml, transaction_id)
      end
      xml.GovTalkDetails
      xml.Body do
        request_body(xml, request_type, request_data)
      end
    end
  end
  builder.doc
end

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

Perform a check



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/companies_house_gateway/request.rb', line 9

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

  response = @connection.post do |request|
    request.path = @config[:api_endpoint]
    request.body = build_request_xml(request_type, request_data).to_s
    request.options.open_timeout = @config[:open_timeout]
    request.options.timeout = @config[:timeout]
  end
  @config[:raw] ? response : response.body
rescue Faraday::Error::ClientError => e
  if e.response.nil?
    raise CompaniesHouseGatewayError.new
  else
    raise CompaniesHouseGatewayError.new(e.response[:body],
                                        e.response[:status],
                                        e.response)
  end
end