Class: Bs2Api::Entities::Customer

Inherits:
Object
  • Object
show all
Defined in:
lib/bs2_api/entities/customer.rb

Constant Summary collapse

TYPES =
{
  personal: 'CPF',
  business: 'CNPJ'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Customer

Returns a new instance of Customer.



13
14
15
16
17
18
# File 'lib/bs2_api/entities/customer.rb', line 13

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

Instance Attribute Details

#business_nameObject

Returns the value of attribute business_name.



6
7
8
# File 'lib/bs2_api/entities/customer.rb', line 6

def business_name
  @business_name
end

#documentObject

Returns the value of attribute document.



6
7
8
# File 'lib/bs2_api/entities/customer.rb', line 6

def document
  @document
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/bs2_api/entities/customer.rb', line 6

def name
  @name
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/bs2_api/entities/customer.rb', line 6

def type
  @type
end

Class Method Details

.from_response(hash_payload) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/bs2_api/entities/customer.rb', line 31

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

Instance Method Details

#business?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/bs2_api/entities/customer.rb', line 46

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

#first_nameObject



50
51
52
# File 'lib/bs2_api/entities/customer.rb', line 50

def first_name
  split_name.first
end

#last_nameObject



54
55
56
57
# File 'lib/bs2_api/entities/customer.rb', line 54

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

#personal?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bs2_api/entities/customer.rb', line 42

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

#to_hashObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/bs2_api/entities/customer.rb', line 20

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