Sha256: 61277dc8910493c428de9836c24d0f937d2cd2b7a5feae265e50c739bc807da1

Contents?: true

Size: 1.46 KB

Versions: 18

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Bs2Api
  module Entities
    class Account
      attr_accessor :bank_code, :bank_name, :agency, :number, :type
  
      TYPES = {
        checking: 'ContaCorrente',
        salary: 'ContaSalario',
        saving: 'Poupanca'
      }

      def initialize(args = {})
        @bank_code  = args.fetch(:bank_code, nil)
        @agency     = args.fetch(:agency, nil)
        @number     = args.fetch(:number, nil)
        @type       = args.fetch(:type, nil)
        @bank_name  = get_bank_name
      end

      def to_hash
        ActiveSupport::HashWithIndifferentAccess.new(
          {
            "banco": @bank_code,
            "bancoNome": get_bank_name,
            "agencia": @agency,
            "numero": @number,
            "tipo": @type
          }
        )
      end

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

        Bs2Api::Entities::Account.new(
          bank_code: hash["banco"],
          bank_name: hash["bancoNome"],
          agency: hash["agencia"],
          number: hash["numero"],
          type: hash["tipo"]
        )
      end

      def checking?
        @type == TYPES[:checking]
      end

      def salary?
        @type == TYPES[:salary]
      end

      def saving?
        @type == TYPES[:saving]
      end

      private
        def get_bank_name
          Bs2Api::Util::BankService.find_by_code(@bank_code)["name"]
        end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
bs2_api-1.1.7 lib/bs2_api/entities/account.rb
bs2_api-1.1.6 lib/bs2_api/entities/account.rb
bs2_api-1.1.5 lib/bs2_api/entities/account.rb
bs2_api-1.1.4 lib/bs2_api/entities/account.rb
bs2_api-1.1.3 lib/bs2_api/entities/account.rb
bs2_api-1.1.2 lib/bs2_api/entities/account.rb
bs2_api-1.1.1 lib/bs2_api/entities/account.rb
bs2_api-1.1.0 lib/bs2_api/entities/account.rb
bs2_api-1.0.3 lib/bs2_api/entities/account.rb
bs2_api-1.0.2 lib/bs2_api/entities/account.rb
bs2_api-1.0.1 lib/bs2_api/entities/account.rb
bs2_api-1.0.0 lib/bs2_api/entities/account.rb
bs2_api-0.4.0 lib/bs2_api/entities/account.rb
bs2_api-0.3.4 lib/bs2_api/entities/account.rb
bs2_api-0.3.3 lib/bs2_api/entities/account.rb
bs2_api-0.3.2 lib/bs2_api/entities/account.rb
bs2_api-0.3.1 lib/bs2_api/entities/account.rb
bs2_api-0.3.0 lib/bs2_api/entities/account.rb