struct CNPJ
- CNPJ
- CadastroID
- Struct
- Value
- Object
Overview
Represents a CNPJ (Cadastro Nacional de Pessoas JurÃdica) identifier.
A CNPJ object always contains a valid identifier, so you can safely assume
that it never holds an invalid identifier.
cnpj = CNPJ.new("24.485.147/0001-87")
cnpj.value # => "24.485.147/0001-87"
cnpj.formatted # => "24.485.147/0001-87"
cnpj.unformatted # => "24485147000187"
cnpj.root # => "24485147"
CNPJ.new("11111111111111") # => raises `ArgumentError`
CNPJ.parse?("11111111111111") # => nil
CNPJ.parse?("24.485.147/0001-87") # => #<CNPJ: ...>
Defined in:
cpf_cnpj/cnpj/cnpj.crcpf_cnpj/cnpj/errors.cr
cpf_cnpj/cnpj/validator.cr
cpf_cnpj/json.cr
cpf_cnpj/yaml.cr
Constructors
-
.from_json_object_key?(key : String) : self
Deserializes the given JSON key into a
CNPJ. -
.new(context : YAML::ParseContext, node : YAML::Nodes::Node) : self
Creates
CNPJfrom YAML usingYAML::ParseContext. -
.new(value : String)
Creates a
CNPJfrom aString. -
.new(pull : JSON::PullParser) : self
Creates
CNPJfrom JSON usingJSON::PullParser.
Class Method Summary
-
.parse?(value : String) : self | Nil
Returns a
CNPJif the given String is a valid CNPJ identifier, otherwise returnsnil.
Instance Method Summary
-
#formatted : String
Returns the formatted CNPJ identifier
-
#root : String
Returns the first eight characters of the identifier.
-
#unformatted : String
Returns the unformatted CNPJ identifier
-
#value : String
The raw value used to initialize the CNPJ
Instance methods inherited from struct CadastroID
==(other : self) : Bool
==,
formatted : String
formatted,
to_json(json : JSON::Builder) : Nil
to_json,
to_s(io : IO) : Nilto_s : String to_s, to_yaml(yaml : YAML::Nodes::Builder) : Nil to_yaml, unformatted : String unformatted, value : String value
Constructor methods inherited from struct CadastroID
from_json_object_key?(key : String) : self
from_json_object_key?,
new(context : YAML::ParseContext, node : YAML::Nodes::Node) : selfnew(value : String) : self
new(pull : JSON::PullParser) : self new
Class methods inherited from struct CadastroID
from_rs(rs : DB::ResultSet) : self | Nil
from_rs,
parse?(value : String) : self | Nil
parse?
Constructor Detail
Deserializes the given JSON key into a CNPJ.
NOTE require "cpf_cnpj/json" is required to opt-in to this feature.
Creates CNPJ from YAML using YAML::ParseContext.
NOTE require "cpf_cnpj/yaml" is required to opt-in to this feature.
require "yaml"
require "cpf_cnpj"
require "cpf_cnpj/yaml"
class Example
include YAML::Serializable
property cnpj : CNPJ
end
example = Example.from_yaml("cnpj: 7B.N1F.Y9N/0001-98")
example.cnpj # => #<CNPJ: ...>
Creates a CNPJ from a String.
It accepts formatted and stripped strings
If the String isn't a valid CNPJ, an CNPJ::InvalidValueError exception will
be raised. See .parse if you want a safe way to initialize a CNPJ.
CNPJ.new("VCZ83T1R000106") # => #<CNPJ: ...>
CNPJ.new("24.485.147/0001-87") # => #<CNPJ: ...>
CNPJ.new("1234") # => raises `CNPJ::InvalidValueError`
Creates CNPJ from JSON using JSON::PullParser.
NOTE require "cpf_cnpj/json" is required to opt-in to this feature.
require "json"
require "cpf_cnpj"
require "cpf_cnpj/json"
class Example
include JSON::Serializable
property cnpj : CNPJ
end
example = Example.from_json(%({"cnpj": "7B.N1F.Y9N/0001-98"}))
example.cnpj # => #<CNPJ: ...>
Class Method Detail
Returns a CNPJ if the given String is a valid CNPJ identifier, otherwise
returns nil.
CNPJ.parse?("VCZ83T1R000106") # => #<CNPJ: ...>
CNPJ.parse?("24.485.147/0001-87") # => #<CNPJ: ...>
CNPJ.parse?("1234") # => nil
Instance Method Detail
Returns the formatted CNPJ identifier
cpf = CNPJ.new("24485147000187")
cpf.formatted # => "24.485.147/0001-87"
Returns the first eight characters of the identifier. This is useful for verifying parent and subsidiary companies:
cnpj = CNPJ.new("VCZ83T1R000106")
cnpj.root # => "VCZ83T1R"
other_cnpj = CNPJ.new("VCZ83T1R000289")
other_cnpj.root # => "VCZ83T1R"
Returns the unformatted CNPJ identifier
cpf = CNPJ.new("24.485.147/0001-87")
cpf.unformatted # => "24485147000187"