Sha256: 3984bb8fbb91fb1e98be08f5513a8c3df8d972193bf59a8a93b820a1e8227eb4

Contents?: true

Size: 779 Bytes

Versions: 5

Compression:

Stored size: 779 Bytes

Contents

# frozen_string_literal: true

class Valvat
  class Lookup
    class Response
      def initialize(raw)
        @raw = raw
      end

      def [](key)
        to_hash[key]
      end

      def to_hash
        @to_hash ||= self.class.cleanup(@raw.to_hash)
      end

      def self.cleanup(hash)
        (
          hash[:check_vat_approx_response] || hash[:check_vat_response] || {}
        ).each_with_object({}) do |(key, value), result|
          result[cleanup_key(key)] = cleanup_value(value) unless key == :"@xmlns"
        end
      end

      TRADER_PREFIX = /\Atrader_/.freeze

      def self.cleanup_key(key)
        key.to_s.sub(TRADER_PREFIX, '').to_sym
      end

      def self.cleanup_value(value)
        value == '---' ? nil : value
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
valvat-1.1.4 lib/valvat/lookup/response.rb
valvat-1.1.3 lib/valvat/lookup/response.rb
valvat-1.1.2 lib/valvat/lookup/response.rb
valvat-1.1.1 lib/valvat/lookup/response.rb
valvat-1.1.0 lib/valvat/lookup/response.rb