Sha256: 491b431f98b47eff31b027a9012e8fdd034d4e0775d9e790c739a3a7b8a00f97

Contents?: true

Size: 1.43 KB

Versions: 15

Compression:

Stored size: 1.43 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

      def first_name
        split_name.first
      end
      
      def last_name
        return '' if split_name.size <= 1
        split_name.last
      end

      private
        def split_name
          @split_name ||= @name.split(' ')
        end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
bs2_api-1.9.0 lib/bs2_api/entities/customer.rb
bs2_api-1.8.0 lib/bs2_api/entities/customer.rb
bs2_api-1.7.0 lib/bs2_api/entities/customer.rb
bs2_api-1.6.2 lib/bs2_api/entities/customer.rb
bs2_api-1.6.1 lib/bs2_api/entities/customer.rb
bs2_api-1.6.0 lib/bs2_api/entities/customer.rb
bs2_api-1.5.1 lib/bs2_api/entities/customer.rb
bs2_api-1.5.0 lib/bs2_api/entities/customer.rb
bs2_api-1.4.3 lib/bs2_api/entities/customer.rb
bs2_api-1.4.2 lib/bs2_api/entities/customer.rb
bs2_api-1.4.1 lib/bs2_api/entities/customer.rb
bs2_api-1.4.0 lib/bs2_api/entities/customer.rb
bs2_api-1.2.2 lib/bs2_api/entities/customer.rb
bs2_api-1.2.1 lib/bs2_api/entities/customer.rb
bs2_api-1.2.0 lib/bs2_api/entities/customer.rb