Class: BlingApi::Customer

Inherits:
Object
  • Object
show all
Defined in:
lib/bling_api/customer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nome:, pessoa_juridica:, numero_documento:, id: nil, telefone: nil, email: nil, endereco: nil, endereco_cobranca: nil, cod_contribuinte: nil) ⇒ Customer

Returns a new instance of Customer.



18
19
20
21
22
23
24
25
26
27
28
# 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)
  @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
end

Instance Attribute Details

#cod_contribuinteObject (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

#emailObject (readonly)

Returns the value of attribute email.



17
18
19
# File 'lib/bling_api/customer.rb', line 17

def email
  @email
end

#enderecoObject (readonly)

Returns the value of attribute endereco.



17
18
19
# File 'lib/bling_api/customer.rb', line 17

def endereco
  @endereco
end

#endereco_cobrancaObject (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

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/bling_api/customer.rb', line 17

def id
  @id
end

#nomeObject (readonly)

Returns the value of attribute nome.



17
18
19
# File 'lib/bling_api/customer.rb', line 17

def nome
  @nome
end

#numero_documentoObject (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_juridicaObject (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

#telefoneObject (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_jsonObject



30
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
# File 'lib/bling_api/customer.rb', line 30

def build_json
  {
    "nome": nome,
    "telefone": telefone,
    "tipo": pessoa_juridica ? "J" : "F",
    "indicadorIe": cod_contribuinte,
    "numeroDocumento": numero_documento,
    "email": email,
    "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

#createObject



75
76
77
78
# File 'lib/bling_api/customer.rb', line 75

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bling_api/customer.rb', line 61

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