Sha256: 61a8d13bb3801c104ec2fa8b64aa6c0e3e3e14374f039156760dbb9a05709bde

Contents?: true

Size: 708 Bytes

Versions: 3

Compression:

Stored size: 708 Bytes

Contents

# frozen_string_literal: true

module MailerLite
  module Middleware
    # This class will underscore all response keys from CamelCase.
    class UnderscoreKeys < Faraday::Response::Middleware
      def on_complete(response)
        response[:body] = updated_response(response[:body])
      end

      private

      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|
          key = MailerLite::Utils.underscore(k.to_s)
          new_hash[key] = updated_response(v)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mailerlite-1.14.0 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-1.13.1 lib/mailerlite/middleware/underscore_keys.rb
mailerlite-1.13.0 lib/mailerlite/middleware/underscore_keys.rb