Sha256: fd5a77df663b02f36fdd34b93a70d99e495cca66e871b9d9dd5d65499c976981

Contents?: true

Size: 960 Bytes

Versions: 7

Compression:

Stored size: 960 Bytes

Contents

# frozen_string_literal: true

module PriceHubble
  module Client
    module Response
      # Convert every response body to an +RecursiveOpenStruct+ for an easy
      # access.
      class RecursiveOpenStruct < Faraday::Middleware
        # Serve the Faraday middleware API.
        #
        # @param env [Hash{Symbol => Mixed}] the request
        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
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pricehubble-1.3.0 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.5 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.4 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.3 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.2 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.1 lib/price_hubble/client/response/recursive_open_struct.rb
pricehubble-1.2.0 lib/price_hubble/client/response/recursive_open_struct.rb