Sha256: 3b6fc762b57be7664ecb51ff45cefae1aa38f77ac91a33d48e29652012d1b069

Contents?: true

Size: 967 Bytes

Versions: 4

Compression:

Stored size: 967 Bytes

Contents

# frozen_string_literal: true

require 'faraday'

module Ivapi
  # Faraday response middleware
  module Response
    class RenameKeys < Faraday::Response::Middleware
      UNUSED_KEYS = %w[ac_ us_ bo_ or_ se_ ta_ in_].freeze

      private

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

      def rename_keys(input)
        if input.is_a?(Hash)
          rename_keys_from_hash(input)
        elsif input.is_a?(Array)
          input.map { |value| rename_keys(value) }
        else
          input
        end
      end

      def rename_keys_from_hash(input)
        new_hash = {}

        input.map do |key, value|
          if value.is_a?(Hash)
            value = rename_keys(value)
          elsif value.is_a?(Array)
            value = value.map { |v| rename_keys(v) }
          end

          new_hash[key.gsub(Regexp.union(UNUSED_KEYS), '')] = value
        end

        new_hash
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ivapi-1.8.0 lib/ivapi/response/rename_keys.rb
ivapi-1.7.0 lib/ivapi/response/rename_keys.rb
ivapi-1.6.1 lib/ivapi/response/rename_keys.rb
ivapi-1.6.0 lib/ivapi/response/rename_keys.rb