Class: PriceHubble::Client::Response::RecursiveOpenStruct

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

Overview

Convert every response body to an RecursiveOpenStruct for an easy access.

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
26
27
# File 'lib/price_hubble/client/response/recursive_open_struct.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

    # By definition empty responses (HTTP status 204)
    # or actual empty bodies should be an empty hash
    body = {} if res[:status] == 204 || res[:body].blank?

    # Looks like we have some actual data we can wrap
    res[:body] =
      ::RecursiveOpenStruct.new(body, recurse_over_arrays: true)
  end
end