Class: LeanplumApi::ResponseValidation

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/leanplum_api/faraday_middleware/response_validation.rb

Instance Method Summary collapse

Instance Method Details

#call(environment) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/leanplum_api/faraday_middleware/response_validation.rb', line 8

def call(environment)
  operations = nil

  if environment.body
    operations = environment.body[:data] if environment.body[:data] && environment.body[:data].is_a?(Array)
    environment.body = environment.body.to_json
  end

  @app.call(environment).on_complete do |response|
    fail ResourceNotFoundError, response.inspect if response.status == 404
    fail BadResponseError, response.inspect unless response.status == 200 && response.body['response']
    fail BadResponseError, "No :success key in #{response.inspect}!" unless response.body['response'].is_a?(Array) && response.body['response'].first.has_key?('success')
    fail BadResponseError, "Not a success! Response: #{response.inspect}" unless response.body['response'].first['success'] == true
    validate_operation_success(operations, response) if operations
  end
end