Sha256: ac6f199c33c0ca43b8aa30a2f16c5011f52cb13b66dd6896329128d5ef797738

Contents?: true

Size: 633 Bytes

Versions: 7

Compression:

Stored size: 633 Bytes

Contents

module MailerLite
  module Middleware
    # This class will underscore all response keys from CamelCase.
    class UnderscoreKeys < Faraday::Response::Middleware
      private

      def on_complete(response)
        response[:body] = updated_response(response[:body])
      end

      def updated_response(response)
        if response.is_a?(Hash)
          underscore_hash_keys(response)
        else
          response
        end
      end

      def underscore_hash_keys(hash)
        hash.each_with_object({}) do |(k, v), new_hash|
          new_hash[k.underscore] = updated_response(v)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mailerlite-1.1.0 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-1.0.2 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-1.0.1 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-1.0.0 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-0.3.0 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-0.2.0 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-0.1.0 lib/mailerlite/middleware/underscore_keys.rb