class Biro::Midia100::Response

Public Class Methods

new(responses) click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 6
def initialize(responses)
  @body = { registration: responses[:registration].body }

  [:rf, :equity, :job, :default].each { |r| @body[r] = responses[r].body if responses[r] }
end

Public Instance Methods

addresses() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 96
def addresses
  Array.wrap(registration&.fetch('Enderecos', nil)).map do |e|
    {
      street: e['Logradouro']&.strip,
      number: e['Numero']&.strip,
      complement: e['Complemento']&.strip,
      city: e['Cidade']&.strip,
      state: e['Uf']&.strip,
      zipcode: e['Cep']&.strip
    }
  end
end
birthdate() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 16
def birthdate
  text('DtNasc')
end
cpf() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 12
def cpf
  text('Cpf')&.gsub(/\D/, '')
end
defaults() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 69
def defaults
  Array.wrap(default&.fetch('spc', nil)).map do |d|
    {
      bureau: :spc,
      company: d['nomeassociado']&.strip,
      created_at: d['datainclusao'],
      due_at: d['datavencimento'],
      value: d['valor'],
      city: d['nome'],
      state: d['siglauf'],
      phone: { ddd: d['numeroddd'], number: d['numero'] },
      contract: d['contrato']
    }
  end
end
education() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 40
def education
  text('Escolaridade')
end
emails() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 109
def emails
  Array.wrap(registration&.fetch('Emails', nil)).map do |e|
    e['EMail']&.strip
  end
end
error() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 89
def error
  return nil unless registration.keys.first =~ /Erro/

  error = registration.flatten
  "#{error[0]} - #{error[1]['Mensagem']}"
end
gender() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 48
def gender
  text('Sexo')
end
income() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 32
def income
  text('RendaPresumida')
end
jobs() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 60
def jobs
  Array.wrap(job&.fetch('Funcionarios', nil)).map do |j|
    {
      cnpj: j['Cnpj']&.gsub(/[\s\.\-\/]/, ''),
      company: j['Empresa']&.strip,
    }
  end
end
landline_phones() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 119
def landline_phones
  Array.wrap(registration&.fetch('Telefones', nil)).map do |t|
    {
      ddd: t['Ddd'],
      number: t['Fone'],
      kind: :landline
    }
  end
end
marital_status() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 20
def marital_status
  text('EstadoCivil')
end
mobile_phones() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 129
def mobile_phones
  Array.wrap(registration&.fetch('Celulares', nil)).map do |c|
    {
      ddd: c['Ddd'],
      number: c['Fone'],
      kind: :mobile
    }
  end
end
mothers_name() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 28
def mothers_name
  text('NomeMae')
end
name() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 44
def name
  text('Nome')
end
phones() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 115
def phones
  landline_phones.to_a + mobile_phones.to_a
end
pis() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 24
def pis
  text('Pis')
end
profession() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 36
def profession
  text('IdProfissao')
end
receita_federal() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 52
def receita_federal
  return nil unless rf

  data = rf['Receita_x0020_Federal_x0020_Pf']

  { name: data.dig('Nome'), status: data.dig('Situacao'), date: data.dig('DataPesquisa'), death: data.dig('Obito') }
end
success?() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 85
def success?
  error.blank?
end

Private Instance Methods

default() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 149
def default
  result(@body[:default])
end
job() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 153
def job
  result(@body[:job])
end
registration() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 157
def registration
  result(@body[:registration])
end
result(data) click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 161
def result(data)
  Nori.new(convert_attributes_to: ->(key, value) { [] }).parse(data[:consultar_response][:consultar_result])['NewDataSet'] unless data.nil?
end
rf() click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 145
def rf
  result(@body[:rf])
end
text(field) click to toggle source
# File lib/biro/gateways/midia100/response.rb, line 141
def text(field)
  registration.dig('Pf', field)&.strip
end