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) ⇒ Customer

Returns a new instance of Customer.



18
19
20
21
22
23
24
25
26
27
# 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)
  @id = id
  @nome = nome
  @pessoa_juridica = pessoa_juridica
  @telefone = telefone
  @numero_documento = numero_documento
  @email = email
  @endereco = endereco
  @endereco_cobranca = endereco_cobranca
end

Instance Attribute Details

#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.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.access_token).find_contact_by_document_number(tax_id)
  if response_json
    contact = Client.new(BlingApi.access_token).get_contact(response_json["id"])
    new(**build_hash(contact))
  end
end

Instance Method Details

#build_jsonObject



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

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



73
74
75
76
# File 'lib/bling_api/customer.rb', line 73

def create
  response = Client.new(BlingApi.access_token).create_contact(JSON.dump(build_json))
  self.class.find_by_id(response["id"])
end

#update(attributes) ⇒ Object



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

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.access_token).update_contact(id, JSON.dump(build_json))
  self.class.find_by_id(self.id)
end