Class: MercadoPublicoCl::Resources::PublicAgency

Inherits:
Data
  • Object
show all
Extended by:
Base::ClassMethods
Defined in:
lib/mercado_publico_cl/resources/public_agency.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/public_agency.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/public_agency.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/public_agency.rb', line 5

def raw
  @raw
end

Class Method Details

.allObject



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

def self.all
  body = client.get(endpoint, {})
  Array(body[list_key]).map { |raw| from_payload(raw) }
end

.directory(list: nil) ⇒ Object

Returns a ‘=> agency` Hash. Useful when you need to look up several agencies — one network call instead of one per lookup.

directory = MercadoPublicoCl::PublicAgency.directory
directory[7086]      # => PublicAgency
directory[1224636]   # => PublicAgency


27
28
29
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 27

def self.directory(list: nil)
  (list || all).each_with_object({}) { |agency, hash| hash[agency.code] = agency }
end

.endpointObject



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

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

.find(code) ⇒ Object

⚠️ Each call fetches the FULL 899-agency list and does a linear scan. For multiple lookups use ‘.directory` instead.

Raises:



33
34
35
36
37
38
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 33

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

  target = code.to_i
  all.find { |agency| agency.code == target }
end

.from_payload(raw) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 40

def self.from_payload(raw)
  new(
    code: Base.to_int(raw["CodigoEmpresa"] || raw["CodigoOrganismo"]),
    name: raw["NombreEmpresa"] || raw["NombreOrganismo"] || raw["Nombre"],
    raw: raw
  )
end

.list_keyObject



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

def self.list_key
  "listaEmpresas"
end

Instance Method Details

#inspectObject Also known as: to_s



48
49
50
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 48

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