struct CNPJ

Overview

Represents a CNPJ (Cadastro Nacional de Pessoas Jurídica) identifier.

A CNPJ object is designed to never hold an invalid value, so you can assume that a CNPJ object will always hold a valid value.

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.new("11111111111111") # => raises `ArgumentError`

CNPJ.parse("11111111111111")     # => nil
CNPJ.parse("24.485.147/0001-87") # => #<CNPJ:0x104fe0ae0 @value="24.485.147/0001-87">

Defined in:

cnpj.cr
cnpj/validator.cr

Constant Summary

VERSION = "1.0.0"

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(value : String) : 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.


[View source]

Class Method Detail

def self.parse(value : String) : CNPJ | Nil #

Returns a CNPJ if the given String is a valid CNPJ identifier, otherwise returns nil.


[View source]

Instance Method Detail

def formatted : String #

Returns the formatted CNPJ identifier

cpf = CNPJ.new("24485147000187")
cpf.formatted # => "24.485.147/0001-87"

[View source]
def unformatted : String #

Returns the unformatted CNPJ identifier

cpf = CNPJ.new("24.485.147/0001-87")
cpf.unformatted # => "24485147000187"

[View source]
def value : String #

[View source]