Class: Nfe::Resources::NaturalPeople

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

Overview

Natural-people (pessoas fĂ­sicas) resource, scoped by company under /companies/{id}/naturalpeople on the :main host family. The API wraps responses in a {"naturalPeople" => ...} envelope, unwrapped here before hydrating NaturalPerson.

Constant Summary collapse

ENVELOPE =

Returns:

  • (String)
"naturalPeople"

Instance Attribute Summary

Attributes inherited from AbstractResource

#client

Instance Method Summary collapse

Methods inherited from AbstractResource

#api_version, #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)


18
19
20
# File 'lib/nfe/resources/natural_people.rb', line 18

def api_family
  :main
end

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

Create a natural person.

Parameters:

  • company_id (String)
  • data (Hash)

Returns:



41
42
43
44
45
46
# File 'lib/nfe/resources/natural_people.rb', line 41

def create(company_id, data)
  id = Nfe::IdValidator.company_id(company_id)
  response = post("/companies/#{id}/naturalpeople",
                  body: json_body(data), headers: json_headers)
  hydrate(Nfe::NaturalPerson, unwrap(parse_json(response.body), ENVELOPE))
end

#create_batch(company_id, list) ⇒ Array<Nfe::NaturalPerson>

Create several natural people. Unlike the Node SDK (+Promise.all+), this runs the create calls sequentially and returns them in order.

Parameters:

  • company_id (String)
  • list (Array<Hash>)

Returns:



92
93
94
# File 'lib/nfe/resources/natural_people.rb', line 92

def create_batch(company_id, list)
  list.map { |data| create(company_id, data) }
end

#delete(company_id, natural_person_id) ⇒ nil

Delete a natural person.

Parameters:

  • company_id (String)
  • natural_person_id (String)

Returns:

  • (nil)


79
80
81
82
83
84
# File 'lib/nfe/resources/natural_people.rb', line 79

def delete(company_id, natural_person_id)
  id = Nfe::IdValidator.company_id(company_id)
  npid = Nfe::IdValidator.presence!(natural_person_id, "natural_person_id")
  super("/companies/#{id}/naturalpeople/#{npid}")
  nil
end

#find_by_tax_number(company_id, federal_tax_number) ⇒ Nfe::NaturalPerson?

Find a natural person by federal tax number (CPF). Normalises the input to 11 digits before filtering. Convenience helper built on #list.

Parameters:

  • company_id (String)
  • federal_tax_number (String)

Returns:



102
103
104
105
106
107
# File 'lib/nfe/resources/natural_people.rb', line 102

def find_by_tax_number(company_id, federal_tax_number)
  target = federal_tax_number.to_s.gsub(/\D/, "")
  list(company_id).data.find do |person|
    person.federal_tax_number.to_s.gsub(/\D/, "") == target
  end
end

#json_body(data) ⇒ String

Parameters:

  • data (Object)

Returns:

  • (String)


115
116
117
# File 'lib/nfe/resources/natural_people.rb', line 115

def json_body(data)
  JSON.generate(data)
end

#json_headersHash[String, String]

Returns:

  • (Hash[String, String])


111
112
113
# File 'lib/nfe/resources/natural_people.rb', line 111

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

#list(company_id) ⇒ Nfe::ListResponse

List every natural person for a company (no pagination, parity with Node).

Parameters:

  • company_id (String)

Returns:



28
29
30
31
32
33
34
# File 'lib/nfe/resources/natural_people.rb', line 28

def list(company_id)
  id = Nfe::IdValidator.company_id(company_id)
  response = get("/companies/#{id}/naturalpeople")
  items = (unwrap(parse_json(response.body), ENVELOPE) || [])
          .map { |item| hydrate(Nfe::NaturalPerson, item) }
  Nfe::ListResponse.new(data: items)
end

#retrieve(company_id, natural_person_id) ⇒ Nfe::NaturalPerson

Retrieve a natural person by id.

Parameters:

  • company_id (String)
  • natural_person_id (String)

Returns:



53
54
55
56
57
58
# File 'lib/nfe/resources/natural_people.rb', line 53

def retrieve(company_id, natural_person_id)
  id = Nfe::IdValidator.company_id(company_id)
  npid = Nfe::IdValidator.presence!(natural_person_id, "natural_person_id")
  response = get("/companies/#{id}/naturalpeople/#{npid}")
  hydrate(Nfe::NaturalPerson, unwrap(parse_json(response.body), ENVELOPE))
end

#update(company_id, natural_person_id, data) ⇒ Nfe::NaturalPerson

Update a natural person.

Parameters:

  • company_id (String)
  • natural_person_id (String)
  • data (Hash)

Returns:



66
67
68
69
70
71
72
# File 'lib/nfe/resources/natural_people.rb', line 66

def update(company_id, natural_person_id, data)
  id = Nfe::IdValidator.company_id(company_id)
  npid = Nfe::IdValidator.presence!(natural_person_id, "natural_person_id")
  response = put("/companies/#{id}/naturalpeople/#{npid}",
                 body: json_body(data), headers: json_headers)
  hydrate(Nfe::NaturalPerson, unwrap(parse_json(response.body), ENVELOPE))
end