Class: Omie::Company
- Inherits:
-
BaseResource
- Object
- BaseResource
- Omie::Company
- Defined in:
- lib/omie/company.rb
Overview
This class abstracts the Company resource from Omie (Ref: Cliente) which is used for all kinds of companies in Omie, mainly Clients and Suppliers. It aims at providing abstractions to the endpoints described in /.
The class methods of Omie::Company usually perform requests to Omie API and manipulate Company objects that contain the returned values. Attributes’ names are equal to the Portuguese names described in the API documentation.
Constant Summary collapse
- CALLS =
{ list: 'ListarClientes', create: 'IncluirCliente', update: 'AlterarCliente', find: 'ConsultarCliente', delete: 'ExcluirCliente', upsert: 'UpsertCliente', associate: 'AssociarCodIntCliente' }.freeze
- URI =
'/v1/geral/clientes/'
Instance Attribute Summary collapse
-
#bairro ⇒ Object
Returns the value of attribute bairro.
-
#cep ⇒ Object
Returns the value of attribute cep.
-
#cidade ⇒ Object
Returns the value of attribute cidade.
-
#cnpj_cpf ⇒ Object
Returns the value of attribute cnpj_cpf.
-
#codigo_cliente_integracao ⇒ Object
Returns the value of attribute codigo_cliente_integracao.
-
#codigo_cliente_omie ⇒ Object
Returns the value of attribute codigo_cliente_omie.
-
#codigo_pais ⇒ Object
Returns the value of attribute codigo_pais.
-
#complemento ⇒ Object
Returns the value of attribute complemento.
-
#contato ⇒ Object
Returns the value of attribute contato.
-
#email ⇒ Object
Returns the value of attribute email.
-
#endereco ⇒ Object
Returns the value of attribute endereco.
-
#endereco_numero ⇒ Object
Returns the value of attribute endereco_numero.
-
#estado ⇒ Object
Returns the value of attribute estado.
-
#inativo ⇒ Object
Returns the value of attribute inativo.
-
#inscricao_estadual ⇒ Object
Returns the value of attribute inscricao_estadual.
-
#nome_fantasia ⇒ Object
Returns the value of attribute nome_fantasia.
-
#razao_social ⇒ Object
Returns the value of attribute razao_social.
Class Method Summary collapse
-
.associate(codigo_cliente_omie, codigo_cliente_integracao) ⇒ Object
Associate the local entry with an existing entry at Omie AssociarCodIntCliente.
-
.create(params = {}) ⇒ Omie::Company
Record a new company using the IncluirCliente call and returns an instance of Omie::Company with the data from the created company.
-
.find(params) ⇒ Omie::Company?
Search for a company using the ConsultarCliente call and returns an instance of the found company or nil otherwise.
-
.list(options = {}) ⇒ Array<Omie::Company>
Get a paginated list of companies recorded in Omie by using the ListarClientes.
-
.update(params = {}) ⇒ Omie::Company
Updates an existing company using the AlterarCliente call and returns an instance of the updated company.
Instance Method Summary collapse
-
#add_tag(tag = nil) ⇒ Omie::Company
Add a new tag method to formatted into Omie`s structure.
-
#associate_entry ⇒ Boolean
Updates the omie entry with the local id for integration purposes.
-
#save ⇒ Omie::Company
Save the company.
-
#saved? ⇒ Boolean
Check whether the object has a related record on Omie based on the #codigo_cliente_omie attribute.
-
#tag_values ⇒ Array<String>
Get method for tag_values attribute.
-
#tags ⇒ Array<Hash>
Get method for tags attribute.
-
#tags=(value) ⇒ Object
Set method for tags attribute to be used for mass assignment of attribuites returned from Omie.
Methods inherited from BaseResource
#initialize, request, request_and_initialize, #update_attributes
Constructor Details
This class inherits a constructor from Omie::BaseResource
Instance Attribute Details
#bairro ⇒ Object
Returns the value of attribute bairro.
31 32 33 |
# File 'lib/omie/company.rb', line 31 def bairro @bairro end |
#cep ⇒ Object
Returns the value of attribute cep.
30 31 32 |
# File 'lib/omie/company.rb', line 30 def cep @cep end |
#cidade ⇒ Object
Returns the value of attribute cidade.
30 31 32 |
# File 'lib/omie/company.rb', line 30 def cidade @cidade end |
#cnpj_cpf ⇒ Object
Returns the value of attribute cnpj_cpf.
28 29 30 |
# File 'lib/omie/company.rb', line 28 def cnpj_cpf @cnpj_cpf end |
#codigo_cliente_integracao ⇒ Object
Returns the value of attribute codigo_cliente_integracao.
29 30 31 |
# File 'lib/omie/company.rb', line 29 def codigo_cliente_integracao @codigo_cliente_integracao end |
#codigo_cliente_omie ⇒ Object
Returns the value of attribute codigo_cliente_omie.
29 30 31 |
# File 'lib/omie/company.rb', line 29 def codigo_cliente_omie @codigo_cliente_omie end |
#codigo_pais ⇒ Object
Returns the value of attribute codigo_pais.
31 32 33 |
# File 'lib/omie/company.rb', line 31 def codigo_pais @codigo_pais end |
#complemento ⇒ Object
Returns the value of attribute complemento.
30 31 32 |
# File 'lib/omie/company.rb', line 30 def complemento @complemento end |
#contato ⇒ Object
Returns the value of attribute contato.
28 29 30 |
# File 'lib/omie/company.rb', line 28 def contato @contato end |
#email ⇒ Object
Returns the value of attribute email.
28 29 30 |
# File 'lib/omie/company.rb', line 28 def email @email end |
#endereco ⇒ Object
Returns the value of attribute endereco.
29 30 31 |
# File 'lib/omie/company.rb', line 29 def endereco @endereco end |
#endereco_numero ⇒ Object
Returns the value of attribute endereco_numero.
30 31 32 |
# File 'lib/omie/company.rb', line 30 def endereco_numero @endereco_numero end |
#estado ⇒ Object
Returns the value of attribute estado.
30 31 32 |
# File 'lib/omie/company.rb', line 30 def estado @estado end |
#inativo ⇒ Object
Returns the value of attribute inativo.
31 32 33 |
# File 'lib/omie/company.rb', line 31 def inativo @inativo end |
#inscricao_estadual ⇒ Object
Returns the value of attribute inscricao_estadual.
31 32 33 |
# File 'lib/omie/company.rb', line 31 def inscricao_estadual @inscricao_estadual end |
#nome_fantasia ⇒ Object
Returns the value of attribute nome_fantasia.
28 29 30 |
# File 'lib/omie/company.rb', line 28 def nome_fantasia @nome_fantasia end |
#razao_social ⇒ Object
Returns the value of attribute razao_social.
28 29 30 |
# File 'lib/omie/company.rb', line 28 def @razao_social end |
Class Method Details
.associate(codigo_cliente_omie, codigo_cliente_integracao) ⇒ Object
Associate the local entry with an existing entry at Omie AssociarCodIntCliente. Omie will find the existing entry through the #codigo_cliente_omie and updates its #codigo_cliente_integracao
128 129 130 131 132 133 134 135 |
# File 'lib/omie/company.rb', line 128 def self.associate(codigo_cliente_omie, codigo_cliente_integracao) params = { codigo_cliente_integracao: codigo_cliente_integracao, codigo_cliente_omie: codigo_cliente_omie } request(URI, CALLS[:associate], params) end |
.create(params = {}) ⇒ Omie::Company
Record a new company using the IncluirCliente call and returns an instance of Omie::Company with the data from the created company.
46 47 48 |
# File 'lib/omie/company.rb', line 46 def self.create(params = {}) request_and_initialize(URI, CALLS[:create], params) end |
.find(params) ⇒ Omie::Company?
Search for a company using the ConsultarCliente call and returns an instance of the found company or nil otherwise. One may use either the #codigo_cliente_omie or #codigo_cliente_integracao to search for company
84 85 86 87 88 |
# File 'lib/omie/company.rb', line 84 def self.find(params) request_and_initialize(URI, CALLS[:find], params) rescue Omie::RequestError nil end |
.list(options = {}) ⇒ Array<Omie::Company>
Get a paginated list of companies recorded in Omie by using the ListarClientes. You may change the params to get other pages of records.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/omie/company.rb', line 101 def self.list( = {}) default = { pagina: 1, registros_por_pagina: 50, apenas_importado_api: 'N' } default.each do |k, v| [k] = v unless .key?(k) end response = request(URI, CALLS[:list], ) response['clientes_cadastro'].map { |client| Omie::Company.new(client) } rescue Omie::RequestError [] end |
.update(params = {}) ⇒ Omie::Company
Updates an existing company using the AlterarCliente call and returns an instance of the updated company. Omie will use either the #codigo_cliente_integracao or the #codigo_cliente_omie to identify the entry to be changed. It will change only the informed attributes in params.
66 67 68 |
# File 'lib/omie/company.rb', line 66 def self.update(params = {}) request_and_initialize(URI, CALLS[:update], params) end |
Instance Method Details
#add_tag(tag = nil) ⇒ Omie::Company
Add a new tag method to formatted into Omie`s structure. It does not duplicate entries.
168 169 170 171 172 173 174 175 176 |
# File 'lib/omie/company.rb', line 168 def add_tag(tag = nil) if tag && !tag_values.include?(tag) << { tag: tag } tag_values << tag end self end |
#associate_entry ⇒ Boolean
Updates the omie entry with the local id for integration purposes.
207 208 209 210 |
# File 'lib/omie/company.rb', line 207 def associate_entry Omie::Company.associate(codigo_cliente_omie, codigo_cliente_integracao) true end |
#save ⇒ Omie::Company
Save the company.
If the company is new a record is created on Omie, otherwise the existing record gets updated.
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/omie/company.rb', line 185 def save company = if saved? Omie::Company.update(as_json.except('tag_values')) else Omie::Company.create(as_json.except('tag_values')) end self.codigo_cliente_omie = company.codigo_cliente_omie if company company end |
#saved? ⇒ Boolean
Check whether the object has a related record on Omie based on the #codigo_cliente_omie attribute
200 201 202 |
# File 'lib/omie/company.rb', line 200 def saved? !codigo_cliente_omie.blank? end |
#tag_values ⇒ Array<String>
Get method for tag_values attribute.
158 159 160 161 |
# File 'lib/omie/company.rb', line 158 def tag_values @tag_values ||= [] @tag_values end |
#tags ⇒ Array<Hash>
Get method for tags attribute.
142 143 144 145 |
# File 'lib/omie/company.rb', line 142 def @tags ||= [] @tags end |
#tags=(value) ⇒ Object
Set method for tags attribute to be used for mass assignment of attribuites returned from Omie. It also sets #tag_values.
149 150 151 152 |
# File 'lib/omie/company.rb', line 149 def (value) @tags = value @tag_values = @tags.map { |t| t[:tag] } end |