Sha256: 937cf4f926c5e81d34b7a1173cad875afa1b021e45e6048dd3e36fbd64e5c6cc

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Bs2Api
  module Entities
    class Customer
      attr_accessor :document, :type, :name, :business_name
  
      TYPES = {
        personal: 'CPF',
        business: 'CNPJ'
      }

      def initialize(args = {})
        @document      = args.fetch(:document, nil)
        @type          = args.fetch(:type, 'CPF')
        @name          = args.fetch(:name, nil)
        @business_name = args.fetch(:business_name, nil)
      end

      def to_hash
        ActiveSupport::HashWithIndifferentAccess.new(
          {
            "documento": @document,
            "tipoDocumento": @type,
            "nome": @name,
            "nomeFantasia": @business_name
          }
        )
      end

      def self.from_response(hash_payload)
        hash = ActiveSupport::HashWithIndifferentAccess.new(hash_payload)

        Bs2Api::Entities::Customer.new(
          document: hash["documento"],
          type: hash["tipoDocumento"],
          name: hash["nome"],
          business_name: hash["nomeFantasia"]
        )
      end

      def personal?
        @type == TYPES[:personal]
      end
      
      def business?
        @type == TYPES[:business]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bs2_api-1.0.2 lib/bs2_api/entities/customer.rb
bs2_api-1.0.1 lib/bs2_api/entities/customer.rb
bs2_api-1.0.0 lib/bs2_api/entities/customer.rb
bs2_api-0.4.0 lib/bs2_api/entities/customer.rb
bs2_api-0.3.4 lib/bs2_api/entities/customer.rb
bs2_api-0.3.3 lib/bs2_api/entities/customer.rb
bs2_api-0.3.2 lib/bs2_api/entities/customer.rb
bs2_api-0.3.1 lib/bs2_api/entities/customer.rb
bs2_api-0.3.0 lib/bs2_api/entities/customer.rb
bs2_api-0.2.1 lib/bs2_api/entities/customer.rb
bs2_api-0.2.0 lib/bs2_api/entities/customer.rb