Class: Biro::CredDefense::Response

Inherits:
BaseResponse show all
Defined in:
lib/biro/gateways/cred_defense/response.rb

Instance Attribute Summary

Attributes inherited from BaseResponse

#body

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



6
7
8
# File 'lib/biro/gateways/cred_defense/response.rb', line 6

def initialize(response)
  @body = response.body
end

Instance Method Details

#addressesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/biro/gateways/cred_defense/response.rb', line 69

def addresses
  data.fetch('dmlistaenderecos', nil).map do |a|
    parts =  a['nologradouro'].split('-')
    street = parts[0]&.split[0..-2].join(' ')
    number = parts[0]&.split&.pop
    city_parts = a['cdibgemunicipio']&.split('/')
    city = city_parts && city_parts[0]
    state = city_parts && city_parts[1]


    {
      street: street,
      number: number,
      complement: parts[1]&.strip,
      city: city,
      state: state,
      zipcode: a['nrcep']
    }
  end
end

#birthdateObject



14
15
16
# File 'lib/biro/gateways/cred_defense/response.rb', line 14

def birthdate
  text('dtnascimento')
end

#cpfObject



10
11
12
# File 'lib/biro/gateways/cred_defense/response.rb', line 10

def cpf
  text('nrcpf').gsub(/\D/, '')
end

#emailsObject



34
35
36
37
# File 'lib/biro/gateways/cred_defense/response.rb', line 34

def emails
  email = text('noemail')
  Array.wrap(email)
end

#equitiesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/biro/gateways/cred_defense/response.rb', line 47

def equities
  data.dig('dmlistaparticipacaosocietaria')&.map do |e|
    cpf = e.dig('nrdocumentosocio')&.gsub(/\D/, '')
    cnpj = e.dig('nrcnpj')&.gsub(/\D/, '')
    name = e.dig('nosocio')&.strip
    company = e.dig('norazaosocial')&.strip
    shares = e.dig('qtpercentualcapitalsocial')
    position = e.dig('noqualificacaosocio')&.strip
    since = e.dig('dtentradasociedade')&.strip

    {
      cpf: cpf,
      cnpj: cnpj,
      name: name,
      company: company,
      shares: shares,
      position: position,
      since: since,
    }
  end
end

#errorObject



110
111
112
113
114
# File 'lib/biro/gateways/cred_defense/response.rb', line 110

def error
  if json['result'] == 'error'
    json['messages']&.first
  end
end

#incomeObject



26
27
28
# File 'lib/biro/gateways/cred_defense/response.rb', line 26

def income
  text('nrrenda')
end

#mothers_nameObject



18
19
20
# File 'lib/biro/gateways/cred_defense/response.rb', line 18

def mothers_name
  text('nomae')
end

#nameObject



22
23
24
# File 'lib/biro/gateways/cred_defense/response.rb', line 22

def name
  text('nopessoa')
end

#phonesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/biro/gateways/cred_defense/response.rb', line 90

def phones
  data.fetch('dmlistatelefones', nil).map do |p|
    parts = p['nrtelefone']&.split(' ')
    ddd = parts&.fetch(0, nil)&.gsub(/\D/, '')
    number = parts&.fetch(1)
    kind = (number.to_s[0] == '9' ? :mobile : :landline) if number

    {
      ddd: ddd,
      number: number,
      kind: kind
    }
  end

end

#professionObject



30
31
32
# File 'lib/biro/gateways/cred_defense/response.rb', line 30

def profession
  text('nrcbo')
end

#receita_federalObject



39
40
41
42
43
44
45
# File 'lib/biro/gateways/cred_defense/response.rb', line 39

def receita_federal
  rf = data.dig('dmcomprovantereceita')&.first

  return nil unless rf

  { name: rf.dig('nopessoa'), status: rf.dig('nosituacao'), date: rf.dig('dhprocessamento'), death: text('boobito') }
end

#success?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/biro/gateways/cred_defense/response.rb', line 106

def success?
  error.blank?
end