Sha256: c55641feefc8eda9497d40fa08befdbd04d09905bd5554a2e4febfad428da92f

Contents?: true

Size: 962 Bytes

Versions: 5

Compression:

Stored size: 962 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

5 entries across 5 versions & 1 rubygems

Version Path
pricehubble-1.1.0 lib/pricehubble/client/response/recursive_open_struct.rb
pricehubble-1.0.0 lib/pricehubble/client/response/recursive_open_struct.rb
pricehubble-0.4.2 lib/pricehubble/client/response/recursive_open_struct.rb
pricehubble-0.4.1 lib/pricehubble/client/response/recursive_open_struct.rb
pricehubble-0.4.0 lib/pricehubble/client/response/recursive_open_struct.rb