Class: Omie::Product
- Inherits:
-
BaseResource
- Object
- BaseResource
- Omie::Product
- Defined in:
- lib/omie/product.rb
Overview
This class abstracts the product resource from Omie (Ref: Produto) It aims at providing abstractions to the endpoints described in /.
The class methods of Omie::Product usually performs requests to Omie API and manipulate product objects that contain the returned values. Attributes’ names are equal to the Portuguese names described in the API documentation.
Constant Summary collapse
- CALLS =
{ list: 'ListarProdutos', create: 'IncluirProduto', update: 'AlterarProduto', find: 'ConsultarProduto', delete: 'ExcluirProduto', simple: 'UpsertProduto', associate: 'AssociarCodIntProduto' }.freeze
- INTERNAL_MODELS =
{ recomendacoes_fiscais: Omie::TaxRecommendation }.freeze
- URI =
'/v1/geral/produtos/'
Instance Attribute Summary collapse
-
#codigo ⇒ Object
They do not keep the same name style =(.
-
#codigo_produto ⇒ Object
Returns the value of attribute codigo_produto.
-
#codigo_produto_integracao ⇒ Object
Returns the value of attribute codigo_produto_integracao.
-
#descricao ⇒ Object
Returns the value of attribute descricao.
-
#descricao_status ⇒ Object
Returns the value of attribute descricao_status.
-
#ncm ⇒ Object
Returns the value of attribute ncm.
- #recomendacoes_fiscais ⇒ Object
-
#tipoItem ⇒ Object
They do not keep the same name style =(.
-
#unidade ⇒ Object
Returns the value of attribute unidade.
-
#valor_unitario ⇒ Object
Returns the value of attribute valor_unitario.
Class Method Summary collapse
-
.associate(codigo_produto, codigo_produto_integracao) ⇒ Object
Associate the local entry with an existing entry at Omie AssociarCodIntProduto.
-
.create(params = {}) ⇒ Omie::Product
Record a new product using the IncluirProduto call and returns an instance of Omie::Product with the data from the created product.
-
.find(params) ⇒ Omie::Product?
Search for a product using the ConsultarProduto call and returns an instance of the found product or nil otherwise.
-
.list(options = {}) ⇒ Array<Omie::Product>
Get a paginated list of companies recorded in Omie by using the Listarprodutos.
-
.update(params = {}) ⇒ Omie::Product
Update an existing product using the AlterarProduto call and returns an instance of the updated product.
Instance Method Summary collapse
-
#associate_entry ⇒ Boolean
Updates the omie entry with the local id for integration purposes.
-
#save ⇒ Omie::Product
Save the product.
-
#saved? ⇒ Boolean
Check whether the object has a related record on Omie based on the #codigo_produto attribute.
Methods inherited from BaseResource
#initialize, request, request_and_initialize, #update_attributes
Constructor Details
This class inherits a constructor from Omie::BaseResource
Instance Attribute Details
#codigo ⇒ Object
They do not keep the same name style =(
31 32 33 |
# File 'lib/omie/product.rb', line 31 def codigo @codigo end |
#codigo_produto ⇒ Object
Returns the value of attribute codigo_produto.
30 31 32 |
# File 'lib/omie/product.rb', line 30 def codigo_produto @codigo_produto end |
#codigo_produto_integracao ⇒ Object
Returns the value of attribute codigo_produto_integracao.
30 31 32 |
# File 'lib/omie/product.rb', line 30 def codigo_produto_integracao @codigo_produto_integracao end |
#descricao ⇒ Object
Returns the value of attribute descricao.
30 31 32 |
# File 'lib/omie/product.rb', line 30 def descricao @descricao end |
#descricao_status ⇒ Object
Returns the value of attribute descricao_status.
29 30 31 |
# File 'lib/omie/product.rb', line 29 def descricao_status @descricao_status end |
#ncm ⇒ Object
Returns the value of attribute ncm.
29 30 31 |
# File 'lib/omie/product.rb', line 29 def ncm @ncm end |
#recomendacoes_fiscais ⇒ Object
32 33 34 |
# File 'lib/omie/product.rb', line 32 def recomendacoes_fiscais @recomendacoes_fiscais end |
#tipoItem ⇒ Object
They do not keep the same name style =(
31 32 33 |
# File 'lib/omie/product.rb', line 31 def tipoItem @tipoItem end |
#unidade ⇒ Object
Returns the value of attribute unidade.
29 30 31 |
# File 'lib/omie/product.rb', line 29 def unidade @unidade end |
#valor_unitario ⇒ Object
Returns the value of attribute valor_unitario.
29 30 31 |
# File 'lib/omie/product.rb', line 29 def valor_unitario @valor_unitario end |
Class Method Details
.associate(codigo_produto, codigo_produto_integracao) ⇒ Object
Associate the local entry with an existing entry at Omie AssociarCodIntProduto. Omie will find the existing entry through the #codigo_produto and updates its #codigo_produto_integracao
131 132 133 134 135 136 137 138 |
# File 'lib/omie/product.rb', line 131 def self.associate(codigo_produto, codigo_produto_integracao) params = { codigo_produto: codigo_produto, codigo_produto_integracao: codigo_produto_integracao } request(URI, CALLS[:associate], params) end |
.create(params = {}) ⇒ Omie::Product
Record a new product using the IncluirProduto call and returns an instance of Omie::Product with the data from the created product.
47 48 49 |
# File 'lib/omie/product.rb', line 47 def self.create(params = {}) request_and_initialize(URI, CALLS[:create], params) end |
.find(params) ⇒ Omie::Product?
Search for a product using the ConsultarProduto call and returns an instance of the found product or nil otherwise. One may use either the #codigo_cliente_omie or #codigo_cliente_integracao to search for product
85 86 87 88 89 |
# File 'lib/omie/product.rb', line 85 def self.find(params) request_and_initialize(URI, CALLS[:find], params) rescue Omie::RequestError nil end |
.list(options = {}) ⇒ Array<Omie::Product>
Get a paginated list of companies recorded in Omie by using the Listarprodutos. You may change the params to get other pages of records.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/omie/product.rb', line 102 def self.list( = {}) default = { pagina: 1, registros_por_pagina: 50, apenas_importado_api: 'N', filtrar_apenas_omiepdv: 'N' } default.each do |k, v| [k] = v unless .key?(k) end response = request(URI, CALLS[:list], ) response['produto_servico_cadastro'].map do |product| Omie::Product.new(product) end rescue Omie::RequestError [] end |
.update(params = {}) ⇒ Omie::Product
Update an existing product using the AlterarProduto call and returns an instance of the updated product. Omie will use either the #codigo_produto_integracao or the #codigo_produto to identify the entry to be changed. It will change only the informed attributes in params.
67 68 69 |
# File 'lib/omie/product.rb', line 67 def self.update(params = {}) request_and_initialize(URI, CALLS[:update], params) end |
Instance Method Details
#associate_entry ⇒ Boolean
Updates the omie entry with the local id for integration purposes.
169 170 171 172 |
# File 'lib/omie/product.rb', line 169 def associate_entry Omie::Product.associate(codigo_produto, codigo_produto_integracao) true end |
#save ⇒ Omie::Product
Save the product.
If the product is new a record is created on Omie, otherwise the existing record gets updated.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/omie/product.rb', line 147 def save product = if saved? Omie::Product.update(as_json) else Omie::Product.create(as_json) end self.codigo_produto = product.codigo_produto if product product end |
#saved? ⇒ Boolean
Check whether the object has a related record on Omie based on the #codigo_produto attribute
162 163 164 |
# File 'lib/omie/product.rb', line 162 def saved? !codigo_produto.blank? end |