Class: Nfe::Resources::StateTaxes

Inherits:
AbstractResource show all
Defined in:
lib/nfe/resources/state_taxes.rb,
sig/nfe/resources/state_taxes.rbs

Overview

Company state-tax (Inscrição Estadual) registrations for the :cte host family (+https://api.nfse.io/v2/companies/companyId/statetaxes+). Exposes the full CRUD: list (cursor-style), create, retrieve, update and delete.

The API wraps single state-tax objects in a {"stateTax" => <object>} envelope (and the list in {"stateTaxes" => [...]}), transparently unwrapped here before hydrating NfeStateTax.

Examples:

client.state_taxes.create(company_id, code: "SP", taxNumber: "1234567890")
client.state_taxes.list(company_id, limit: 50)

Constant Summary collapse

ENVELOPE =

Envelope key for a single state-tax object.

Returns:

  • (String)
"stateTax"
LIST_ENVELOPE =

Wrapper key for the list envelope.

Returns:

  • (String)
"stateTaxes"

Instance Attribute Summary

Attributes inherited from AbstractResource

#client

Instance Method Summary collapse

Methods inherited from AbstractResource

#build_list_page, #build_multipart_body, #cursor_list_page, #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_familySymbol

Returns:

  • (Symbol)


28
29
30
# File 'lib/nfe/resources/state_taxes.rb', line 28

def api_family
  :cte
end

#api_versionString

The :cte host (+api.nfse.io+) does not bake in a version; the /v2 segment is carried in the base path instead.

Returns:

  • (String)


34
35
36
# File 'lib/nfe/resources/state_taxes.rb', line 34

def api_version
  ""
end

#base_path(company_id) ⇒ String

Parameters:

  • company_id (String)

Returns:

  • (String)


109
110
111
# File 'lib/nfe/resources/state_taxes.rb', line 109

def base_path(company_id)
  "/v2/companies/#{company_id}/statetaxes"
end

#create(company_id, data) ⇒ Nfe::NfeStateTax

Create a state-tax registration.

Parameters:

  • company_id (String)
  • data (Hash)

    state-tax attributes (camelCase keys per the API).

Returns:



62
63
64
65
66
67
# File 'lib/nfe/resources/state_taxes.rb', line 62

def create(company_id, data)
  cid = Nfe::IdValidator.company_id(company_id)
  response = post(base_path(cid), body: JSON.generate({ ENVELOPE => data }),
                                  headers: json_headers)
  hydrate(Nfe::NfeStateTax, unwrap(parse_json(response.body), ENVELOPE))
end

#delete(company_id, state_tax_id) ⇒ nil

Delete a state-tax registration.

Parameters:

  • company_id (String)
  • state_tax_id (String)

Returns:

  • (nil)


100
101
102
103
104
105
# File 'lib/nfe/resources/state_taxes.rb', line 100

def delete(company_id, state_tax_id)
  cid = Nfe::IdValidator.company_id(company_id)
  sid = Nfe::IdValidator.state_tax_id(state_tax_id)
  super("#{base_path(cid)}/#{sid}")
  nil
end

#json_headersHash[String, String]

Returns:

  • (Hash[String, String])


113
114
115
# File 'lib/nfe/resources/state_taxes.rb', line 113

def json_headers
  { "Content-Type" => "application/json" }
end

#list(company_id, starting_after: nil, ending_before: nil, limit: nil) ⇒ Nfe::ListResponse

List a company's state-tax registrations (cursor-style pagination).

Parameters:

  • company_id (String)
  • starting_after (String, nil) (defaults to: nil)

    cursor — items after this id.

  • ending_before (String, nil) (defaults to: nil)

    cursor — items before this id.

  • limit (Integer, nil) (defaults to: nil)

    page size.

  • starting_after: (Object) (defaults to: nil)
  • ending_before: (Object) (defaults to: nil)
  • limit: (Object) (defaults to: nil)

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/nfe/resources/state_taxes.rb', line 47

def list(company_id, starting_after: nil, ending_before: nil, limit: nil)
  cid = Nfe::IdValidator.company_id(company_id)
  query = {} #: Hash[String, untyped]
  query["startingAfter"] = starting_after unless starting_after.nil?
  query["endingBefore"] = ending_before unless ending_before.nil?
  query["limit"] = limit unless limit.nil?
  response = get(base_path(cid), query: query)
  hydrate_list(Nfe::NfeStateTax, parse_json(response.body), wrapper_key: LIST_ENVELOPE)
end

#retrieve(company_id, state_tax_id) ⇒ Nfe::NfeStateTax

Retrieve a single state-tax registration.

Parameters:

  • company_id (String)
  • state_tax_id (String)

Returns:



74
75
76
77
78
79
# File 'lib/nfe/resources/state_taxes.rb', line 74

def retrieve(company_id, state_tax_id)
  cid = Nfe::IdValidator.company_id(company_id)
  sid = Nfe::IdValidator.state_tax_id(state_tax_id)
  response = get("#{base_path(cid)}/#{sid}")
  hydrate(Nfe::NfeStateTax, unwrap(parse_json(response.body), ENVELOPE))
end

#update(company_id, state_tax_id, data) ⇒ Nfe::NfeStateTax

Update a state-tax registration.

Parameters:

  • company_id (String)
  • state_tax_id (String)
  • data (Hash)

    state-tax attributes (camelCase keys per the API).

Returns:



87
88
89
90
91
92
93
# File 'lib/nfe/resources/state_taxes.rb', line 87

def update(company_id, state_tax_id, data)
  cid = Nfe::IdValidator.company_id(company_id)
  sid = Nfe::IdValidator.state_tax_id(state_tax_id)
  response = put("#{base_path(cid)}/#{sid}", body: JSON.generate({ ENVELOPE => data }),
                                             headers: json_headers)
  hydrate(Nfe::NfeStateTax, unwrap(parse_json(response.body), ENVELOPE))
end