Sha256: f31dcbe514382088a113c423d667472362def0015941e46209f8e1bb0f308ed1

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require "multi_xml"
require "rexml"

module Experian
  class Report
    attr_reader :response, :url, :raw_xml, :xml

    def initialize(response)
      @response = response
      @url = response.env.url.to_s
      @raw_xml = response.body
      MultiXml.parser = :rexml
      @xml = MultiXml.parse(@raw_xml)

      raise Experian::AuthenticationError if authentication_error?
    end

    def id
      if (id_xml = data["DatosIdentificativos"])
        OpenStruct.new(
          cif: id_xml["Cif"],
          name: id_xml["Nombre"],
          infotel_code: id_xml["CodigoInfotel"],
          incorporation_date: Date.parse(id_xml["FechaFundacion"]),
          social_form: id_xml["FormaSocial"]["__content__"],
        )
      end
    end

    def address
      if (address_xml = data["DomicilioComercial"])
        OpenStruct.new(
          line: address_xml["Domicilio"],
          city: address_xml["Poblacion"],
          province: address_xml["Provincia"],
          postal_code: address_xml["CodigoPostal"],
          municipality: address_xml["Municipio"],
        )
      end
    end

    # Number of employees in the last recorded excercise
    def number_of_employees
      data.dig("ListaAnualEmpleados", "Empleado")&.first&.dig("EmpleadoFijo")&.to_i
    end

    def rating
      if (rating_xml = data["Rating"])
        return unless rating_xml["RatingAxesorDef"]

        OpenStruct.new(
          score: rating_xml["RatingAxesorDef"]&.strip&.to_i,
          default_probability: rating_xml["ProbImpago"]&.to_f,
          risk: rating_xml["GrupoRiesgo"],
          size: rating_xml["TamaƱo"],
        )
      end
    end

    def cnae
      data.dig("ActividadComercial", "Cnae")&.first&.dig("Codigo")&.to_i
    end

    private

    def data
      xml.dig("ServicioWebAxesor", "ListaPaquetesNegocio")
    end

    def authentication_error?
      xml.dig("DatosError", "CodError") == "1"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-experian-0.0.7 lib/experian/report.rb