Class: BlingApi::Customer
- Inherits:
-
Object
- Object
- BlingApi::Customer
- Defined in:
- lib/bling_api/customer.rb
Instance Attribute Summary collapse
-
#cod_contribuinte ⇒ Object
readonly
Returns the value of attribute cod_contribuinte.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#endereco ⇒ Object
readonly
Returns the value of attribute endereco.
-
#endereco_cobranca ⇒ Object
readonly
Returns the value of attribute endereco_cobranca.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#nome ⇒ Object
readonly
Returns the value of attribute nome.
-
#numero_documento ⇒ Object
readonly
Returns the value of attribute numero_documento.
-
#pessoa_juridica ⇒ Object
readonly
Returns the value of attribute pessoa_juridica.
-
#situacao ⇒ Object
readonly
Returns the value of attribute situacao.
-
#telefone ⇒ Object
readonly
Returns the value of attribute telefone.
Class Method Summary collapse
Instance Method Summary collapse
- #build_json ⇒ Object
- #create ⇒ Object
-
#initialize(nome:, pessoa_juridica:, numero_documento:, id: nil, telefone: nil, email: nil, endereco: nil, endereco_cobranca: nil, cod_contribuinte: nil, situacao:) ⇒ Customer
constructor
A new instance of Customer.
- #update(attributes) ⇒ Object
Constructor Details
#initialize(nome:, pessoa_juridica:, numero_documento:, id: nil, telefone: nil, email: nil, endereco: nil, endereco_cobranca: nil, cod_contribuinte: nil, situacao:) ⇒ Customer
Returns a new instance of Customer.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bling_api/customer.rb', line 18 def initialize(nome:, pessoa_juridica:, numero_documento:, id:nil, telefone:nil, email:nil, endereco:nil, endereco_cobranca:nil, cod_contribuinte:nil, situacao:) @id = id @nome = nome @pessoa_juridica = pessoa_juridica @telefone = telefone @numero_documento = numero_documento @email = email @endereco = endereco&.with_indifferent_access @endereco_cobranca = endereco_cobranca&.with_indifferent_access @cod_contribuinte = cod_contribuinte @situacao = situacao end |
Instance Attribute Details
#cod_contribuinte ⇒ Object (readonly)
Returns the value of attribute cod_contribuinte.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def cod_contribuinte @cod_contribuinte end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def email @email end |
#endereco ⇒ Object (readonly)
Returns the value of attribute endereco.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def endereco @endereco end |
#endereco_cobranca ⇒ Object (readonly)
Returns the value of attribute endereco_cobranca.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def endereco_cobranca @endereco_cobranca end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def id @id end |
#nome ⇒ Object (readonly)
Returns the value of attribute nome.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def nome @nome end |
#numero_documento ⇒ Object (readonly)
Returns the value of attribute numero_documento.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def numero_documento @numero_documento end |
#pessoa_juridica ⇒ Object (readonly)
Returns the value of attribute pessoa_juridica.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def pessoa_juridica @pessoa_juridica end |
#situacao ⇒ Object (readonly)
Returns the value of attribute situacao.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def situacao @situacao end |
#telefone ⇒ Object (readonly)
Returns the value of attribute telefone.
17 18 19 |
# File 'lib/bling_api/customer.rb', line 17 def telefone @telefone end |
Class Method Details
.find_by_id(id) ⇒ Object
12 13 14 15 |
# File 'lib/bling_api/customer.rb', line 12 def self.find_by_id id contact = Client.new(BlingApi.configuration.access_token).get_contact(id) new(**build_hash(contact)) end |
.find_by_tax_id(tax_id) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/bling_api/customer.rb', line 4 def self.find_by_tax_id tax_id response_json = Client.new(BlingApi.configuration.access_token).find_contact_by_document_number(tax_id) if response_json contact = Client.new(BlingApi.configuration.access_token).get_contact(response_json["id"]) new(**build_hash(contact)) end end |
Instance Method Details
#build_json ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bling_api/customer.rb', line 31 def build_json { "nome": nome, "telefone": telefone, "tipo": pessoa_juridica ? "J" : "F", "indicadorIe": cod_contribuinte, "numeroDocumento": numero_documento, "email": email, "situacao": situacao, "endereco": { "geral": { "endereco": endereco["endereco"], "cep": endereco["cep"], "bairro": endereco["bairro"], "municipio": endereco["municipio"], "uf": endereco["uf"], "numero": endereco["numero"], "complemento": endereco["complemento"] }, "cobranca": { "endereco": endereco_cobranca["endereco"], "cep": endereco_cobranca["cep"], "bairro": endereco_cobranca["bairro"], "municipio": endereco_cobranca["municipio"], "uf": endereco_cobranca["uf"], "numero": endereco_cobranca["numero"], "complemento": endereco_cobranca["complemento"] } } } end |
#create ⇒ Object
77 78 79 80 |
# File 'lib/bling_api/customer.rb', line 77 def create response = Client.new(BlingApi.configuration.access_token).create_contact(JSON.dump(build_json)) self.class.find_by_id(response["id"]) end |
#update(attributes) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bling_api/customer.rb', line 63 def update attributes attributes.each do |key, value| if key == :endereco || key == :endereco_cobranca value.each do |k, v| self.send(key)[k] = v end else instance_variable_set("@#{key}", value) end end Client.new(BlingApi.configuration.access_token).update_contact(id, JSON.dump(build_json)) self.class.find_by_id(self.id) end |