Class: MercadoPublicoCl::Resources::Supplier

Inherits:
Data
  • Object
show all
Extended by:
Base::ClassMethods
Defined in:
lib/mercado_publico_cl/resources/supplier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassMethods

client, detail_payload, extract_list, fetch_list, fetch_single, list_key

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



5
6
7
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 5

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 5

def name
  @name
end

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of raw



5
6
7
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 5

def raw
  @raw
end

#rutObject (readonly)

Returns the value of attribute rut

Returns:

  • (Object)

    the current value of rut



5
6
7
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 5

def rut
  @rut
end

Class Method Details

.bare_rut_regexObject



20
21
22
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 20

def self.bare_rut_regex
  /\A(\d{1,9})-([\dkK])\z/
end

.endpointObject



8
9
10
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 8

def self.endpoint
  "/servicios/v1/Publico/Empresas/BuscarProveedor"
end

.find_by_rut(rut) ⇒ Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 36

def self.find_by_rut(rut)
  raise InvalidQueryError, "rut is required" if rut.nil? || rut.to_s.strip.empty?

  normalized = normalize_rut(rut)
  raise InvalidQueryError, "invalid RUT format: #{rut.inspect}" if normalized.nil?

  body = client.get(endpoint, { rutempresaproveedor: normalized })
  item = Array(body[list_key]).first
  return nil if item.nil?

  from_payload(item.merge("__rut" => normalized))
end

.from_payload(raw) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 49

def self.from_payload(raw)
  new(
    code: Base.to_int(raw["CodigoEmpresa"] || raw["CodigoProveedor"]),
    name: raw["NombreEmpresa"] || raw["Nombre"] || raw["NombreProveedor"],
    rut: raw["RutSucursal"] || raw["RutEmpresa"] || raw["Rut"] || raw["__rut"],
    raw: raw.reject { |k, _| k == "__rut" }
  )
end

.list_keyObject



12
13
14
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 12

def self.list_key
  "listaEmpresas"
end

.normalize_rut(rut) ⇒ Object

Acepta RUT con o sin puntos; lo canonicaliza con puntos para la API.



25
26
27
28
29
30
31
32
33
34
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 25

def self.normalize_rut(rut)
  stripped = rut.to_s.strip
  return stripped if rut_regex.match?(stripped)

  m = bare_rut_regex.match(stripped.delete("."))
  return nil if m.nil?

  dotted = m[1].reverse.scan(/\d{1,3}/).join(".").reverse
  "#{dotted}-#{m[2]}"
end

.rut_regexObject



16
17
18
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 16

def self.rut_regex
  /\A\d{1,3}(?:\.\d{3}){0,2}-[\dkK]\z/
end

Instance Method Details

#inspectObject Also known as: to_s



58
59
60
# File 'lib/mercado_publico_cl/resources/supplier.rb', line 58

def inspect
  "#<MercadoPublicoCl::Supplier code=#{code.inspect} name=#{name.inspect} rut=#{rut.inspect}>"
end