class Biro::Spc::Response

Public Class Methods

new(response) click to toggle source
# File lib/biro/gateways/spc/response.rb, line 6
def initialize(response)
  @body = response.body
end

Public Instance Methods

addresses() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 56
def addresses
  [single_address] + all_addresses
end
birthdate() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 14
def birthdate
  fetch_date(person.dig(:@data_nascimento))
end
cpf() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 10
def cpf
  person.dig(:cpf, :@numero)
end
defaults() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 64
def defaults
  Array.wrap(spc).map do |d|
    {
      bureau: :spc,
      company: d.dig(:@nome_associado)&.strip,
      created_at: fetch_date(d.dig(:@data_inclusao)),
      due_at: fetch_date(d.dig(:@data_vencimento)),
      value: d.dig(:@valor)&.to_f,
      city: d.dig(:cidade_associado, :@nome),
      state: d.dig(:cidade_associado, :estado, :@sigla_uf),
      phone: { ddd: d.dig(:telefone_associado, :@numero_ddd), number: d.dig(:telefone_associado, :@numero) },
      contract: d.dig(:@contrato)
    }
  end
end
emails() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 99
def emails
  Array.wrap(person&.dig(:@email))
end
error() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 92
def error
  return nil unless registration.keys.first =~ /Erro/

  error = registration.flatten
  "#{error[0]} - #{error[1]['Mensagem']}"
end
fathers_name() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 26
def fathers_name
  person.dig(:@nome_pai)
end
gender() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 42
def gender
  person.dig(:@sexo)
end
income() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 46
def income
  result.dig(:renda_presumida_spc, :resumo, :@valor_total)&.to_f&.round(2)
end
marital_status() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 18
def marital_status
  person.dig(:@estado_civil)
end
mothers_name() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 22
def mothers_name
  person.dig(:@nome_mae)
end
name() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 34
def name
  person.dig(:@nome)
end
phones() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 60
def phones
  [single_phone] + all_phones
end
receita_federal() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 50
def receita_federal
  return nil unless rf

  { status: rf.dig(:@descricao_situacao), date: fetch_date(rf.dig(:@data_situacao)) }
end
rg() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 30
def rg
  person.dig(:@numero_rg)
end
scores() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 80
def scores
  [:spc_score_3_meses, :spc_score_12_meses].map do |score|
    next unless result.include? score

    fetch_score(result.dig(score))
  end.compact
end
success?() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 88
def success?
  error.blank?
end
voter_id() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 38
def voter_id
  person.dig(:@numero_titulo_eleitor)
end

Private Instance Methods

address() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 169
def address
  result.dig(:ultimo_endereco_informado)
end
all_addresses() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 135
def all_addresses
  Array.wrap(address&.fetch(:detalhe_ultimo_endereco_informado, nil)).map { |e| fetch_address(e) }
end
all_phones() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 111
def all_phones
  phones = result.dig(:telefone_vinculado_consumidor, :detalhe_telefone_vinculado_consumidor)

  Array.wrap(phones).map do |p|
    phone = p.dig(:assinante, :telefone)
    fetch_phone(phone)
  end
end
fetch_address(e) click to toggle source
# File lib/biro/gateways/spc/response.rb, line 144
def fetch_address(e)
  address = e.dig(:endereco)
  city = address&.dig(:cidade, :@nome)
  state = address&.dig(:cidade, :estado, :@sigla_uf)
  street = address&.dig(:@logradouro)
  number = address&.dig(:@numero)
  complement = address&.dig(:@complemento)
  zipcode = address&.dig(:@cep)

  {
    street: street,
    number: number,
    complement: complement,
    city: city,
    state: state,
    zipcode: zipcode
  }
end
fetch_date(date) click to toggle source
# File lib/biro/gateways/spc/response.rb, line 105
def fetch_date(date)
  Time.parse(date)
rescue
  nil
end
fetch_phone(phone) click to toggle source
# File lib/biro/gateways/spc/response.rb, line 127
def fetch_phone(phone)
  ddd = phone.dig(:@numero_ddd)
  number = phone.dig(:@numero)
  kind = (number.to_s[0] == '9' ? :mobile : :landline) if number

  { ddd: ddd, number: number, kind: kind }
end
fetch_score(score) click to toggle source
# File lib/biro/gateways/spc/response.rb, line 163
def fetch_score(score)
  data = score&.values&.first

  { score_class: data.dig(:@classe), score: data.dig(:@score)&.to_i, period: data.dig(:@horizonte)&.to_i }
end
person() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 177
def person
  result.dig(:consumidor, :consumidor_pessoa_fisica)
end
result() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 185
def result
  @body.dig(:resultado)
end
rf() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 173
def rf
  person.dig(:situacao_cpf)
end
single_address() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 140
def single_address
  fetch_address(person)
end
single_phone() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 120
def single_phone
  phone = person.dig(:telefone_residencial)
  return nil unless phone

  fetch_phone(phone)
end
spc() click to toggle source
# File lib/biro/gateways/spc/response.rb, line 181
def spc
  result.dig(:spc, :detalhe_spc)
end