Class: PriceHubble::Client::Response::DataSanitization

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/price_hubble/client/response/data_sanitization.rb

Overview

We like snake cased attributes here, but the API sends camel cased hash keys on responses, so we take care of it in a generic way here.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Serve the Faraday middleware API.

Parameters:

  • env (Hash{Symbol => Mixed})

    the request



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/price_hubble/client/response/data_sanitization.rb', line 12

def call(env)
  @app.call(env).on_complete do |res|
    body = res[:body]

    # Skip string bodies, they are unparsed or contain binary data
    next if body.is_a? String

    # Skip non-hash bodies, we cannot handle them here
    next unless body.is_a? Hash

    # Perform the key transformation
    res[:body] = body.deep_underscore_keys
  end
end