Class: BlingApi::Customer

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

Constant Summary collapse

ACCESS_TOKEN =
"0039bb7d2d3e3c6777983361204dc168ecbf1ef8"

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.



19
20
21
22
23
24
25
26
27
28
# File 'lib/bling_api/customer.rb', line 19

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.



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

def email
  @email
end

#enderecoObject (readonly)

Returns the value of attribute endereco.



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

def endereco
  @endereco
end

#endereco_cobrancaObject (readonly)

Returns the value of attribute endereco_cobranca.



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

def endereco_cobranca
  @endereco_cobranca
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nomeObject (readonly)

Returns the value of attribute nome.



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

def nome
  @nome
end

#numero_documentoObject (readonly)

Returns the value of attribute numero_documento.



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

def numero_documento
  @numero_documento
end

#pessoa_juridicaObject (readonly)

Returns the value of attribute pessoa_juridica.



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

def pessoa_juridica
  @pessoa_juridica
end

#telefoneObject (readonly)

Returns the value of attribute telefone.



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

def telefone
  @telefone
end

Class Method Details

.find_by_id(id) ⇒ Object



13
14
15
16
# File 'lib/bling_api/customer.rb', line 13

def self.find_by_id id
  contact = Client.new(ACCESS_TOKEN).get_contact(id)
  new(**build_hash(contact))
end

.find_by_tax_id(tax_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/bling_api/customer.rb', line 5

def self.find_by_tax_id tax_id
  response_json = Client.new(ACCESS_TOKEN).find_contact_by_document_number(tax_id)
  if response_json
    contact = Client.new(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
# File 'lib/bling_api/customer.rb', line 30

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



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

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

#update(attributes) ⇒ Object



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

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