Class: MercadoPublicoCl::Resources::PublicAgency
- Inherits:
-
Data
- Object
- Data
- MercadoPublicoCl::Resources::PublicAgency
- Extended by:
- Base::ClassMethods
- Defined in:
- lib/mercado_publico_cl/resources/public_agency.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
- .all ⇒ Object
-
.directory(list: nil) ⇒ Object
Returns a ‘=> agency` Hash.
- .endpoint ⇒ Object
-
.find(code) ⇒ Object
⚠️ Each call fetches the FULL 899-agency list and does a linear scan.
- .from_payload(raw) ⇒ Object
- .list_key ⇒ Object
Instance Method Summary collapse
- #inspect ⇒ Object (also: #to_s)
Methods included from Base::ClassMethods
client, detail_payload, extract_list, fetch_list, fetch_single, list_key
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code
5 6 7 |
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 5 def code @code end |
#name ⇒ Object (readonly)
Returns the value of attribute name
5 6 7 |
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 5 def name @name end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw
5 6 7 |
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 5 def raw @raw end |
Class Method Details
.all ⇒ Object
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 |
.endpoint ⇒ Object
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.
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_key ⇒ Object
12 13 14 |
# File 'lib/mercado_publico_cl/resources/public_agency.rb', line 12 def self.list_key "listaEmpresas" end |
Instance Method Details
#inspect ⇒ Object 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 |