Class: Nfe::Resources::TaxCalculation
- Inherits:
-
AbstractResource
- Object
- AbstractResource
- Nfe::Resources::TaxCalculation
- Defined in:
- lib/nfe/resources/tax_calculation.rb,
sig/nfe/resources/tax_calculation.rbs
Overview
Tax calculation engine for the :cte family (host api.nfse.io). Runs the
tax rules engine for a tenant and returns a per-item tax breakdown.
Instance Attribute Summary
Attributes inherited from AbstractResource
Instance Method Summary collapse
- #api_family ⇒ Symbol
-
#api_version ⇒ String
The path already carries its own segments, so no version prefix is added.
-
#calculate(tenant_id, request) ⇒ Nfe::Generated::CalculoImpostosV1::CalculateResponse?
requestaccepts a plain Hash; build it from Nfe::Generated::CalculoImpostosV1::CalculateRequest#to_h for type-safety. - #items?(request) ⇒ Boolean
- #operation_type?(request) ⇒ Boolean
-
#validate_calculate!(tenant_id, request) ⇒ void
Fail fast (no HTTP) when the tenant or request shape is invalid.
Methods inherited from AbstractResource
#build_list_page, #build_multipart_body, #cursor_list_page, #delete, #dig_key, #download, #extract_invoice_id, #full_path, #get, #handle_async_response, #hydrate, #hydrate_list, #initialize, #multipart_part, #page_list_page, #parse_json, #post, #put, #unwrap, #upload_multipart
Constructor Details
This class inherits a constructor from Nfe::Resources::AbstractResource
Instance Method Details
#api_family ⇒ Symbol
22 |
# File 'lib/nfe/resources/tax_calculation.rb', line 22 def api_family = :cte |
#api_version ⇒ String
The path already carries its own segments, so no version prefix is added.
25 |
# File 'lib/nfe/resources/tax_calculation.rb', line 25 def api_version = "" |
#calculate(tenant_id, request) ⇒ Nfe::Generated::CalculoImpostosV1::CalculateResponse?
request accepts a plain Hash; build it from
Nfe::Generated::CalculoImpostosV1::CalculateRequest#to_h for type-safety.
41 42 43 44 45 46 47 |
# File 'lib/nfe/resources/tax_calculation.rb', line 41 def calculate(tenant_id, request) validate_calculate!(tenant_id, request) path = "/tax-rules/#{CGI.escape(tenant_id.to_s.strip)}/engine/calculate" response = post(path, body: JSON.generate(request), headers: { "Content-Type" => "application/json" }) hydrate(Nfe::Generated::CalculoImpostosV1::CalculateResponse, parse_json(response.body)) end |
#items?(request) ⇒ Boolean
65 66 67 68 |
# File 'lib/nfe/resources/tax_calculation.rb', line 65 def items?(request) items = request[:items] || request["items"] items.is_a?(Array) && !items.empty? end |
#operation_type?(request) ⇒ Boolean
60 61 62 63 |
# File 'lib/nfe/resources/tax_calculation.rb', line 60 def operation_type?(request) !request[:operation_type].nil? || !request["operation_type"].nil? || !request[:operationType].nil? || !request["operationType"].nil? end |
#validate_calculate!(tenant_id, request) ⇒ void
This method returns an undefined value.
Fail fast (no HTTP) when the tenant or request shape is invalid.
52 53 54 55 56 57 58 |
# File 'lib/nfe/resources/tax_calculation.rb', line 52 def validate_calculate!(tenant_id, request) Nfe::IdValidator.presence!(tenant_id, "tenant_id") return if request.is_a?(Hash) && operation_type?(request) && items?(request) raise Nfe::InvalidRequestError, "request deve conter operation_type (ou operationType) e items (Array não vazio)" end |