Class: CompaniesHouseGateway::Middleware::CheckResponse

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



4
5
6
7
8
9
# File 'lib/companies_house_gateway/middleware/check_response.rb', line 4

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

#check_for_errors(env) ⇒ Object



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

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

  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



11
12
13
# File 'lib/companies_house_gateway/middleware/check_response.rb', line 11

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