Class: CompaniesHouseInputGateway::Middleware::CheckResponse

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/companies_house_input_gateway/middleware/check_response.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
# File 'lib/companies_house_input_gateway/middleware/check_response.rb', line 6

def call(env)
  @app.call(env).on_complete do |item|
    check_for_errors(item)
    response_values(item)
  end
end

#check_for_errors(env) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/companies_house_input_gateway/middleware/check_response.rb', line 17

def check_for_errors(env)
  body = env[:body]['GovTalkMessage']
  raise InvalidResponseError.new(env[:body], env[:status], env) unless body && body['GovTalkDetails']

  if body['GovTalkDetails']['GovTalkErrors']
    error = body['GovTalkDetails']['GovTalkErrors'].values.flatten.first
    raise APIError.new(error['Text'], error['Number'], env[:status], env)
  end
end

#response_values(env) ⇒ Object



13
14
15
# File 'lib/companies_house_input_gateway/middleware/check_response.rb', line 13

def response_values(env)
  { status: env[:status], headers: env[:headers], body: env[:body] }
end