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:, rg: nil, orgao_emissor: nil, id: nil, telefone: nil, celular: nil, email: nil, endereco: nil, endereco_cobranca: nil, cod_contribuinte: nil, situacao: nil, ie: nil, fantasia: nil) ⇒ Customer

Returns a new instance of Customer.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bling_api/customer.rb', line 17

def initialize(nome:, pessoa_juridica:, numero_documento:, rg: nil, orgao_emissor: nil, id: nil, telefone: nil, celular: nil, email: nil, endereco: nil, endereco_cobranca: nil, cod_contribuinte: nil, situacao: nil, ie: nil, fantasia: nil)
  @id = id
  @nome = nome
  @pessoa_juridica = pessoa_juridica
  @telefone = telefone
  @celular = celular
  @numero_documento = numero_documento
  @orgao_emissor = orgao_emissor
  @rg = rg
  @email = email
  @endereco = endereco&.with_indifferent_access
  @endereco_cobranca = endereco_cobranca&.with_indifferent_access
  @cod_contribuinte = cod_contribuinte
  @ie = ie
  @situacao = situacao
  @fantasia = fantasia
end

Instance Attribute Details

#celularObject (readonly)

Returns the value of attribute celular.



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

def celular
  @celular
end

#cod_contribuinteObject (readonly)

Returns the value of attribute cod_contribuinte.



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

def cod_contribuinte
  @cod_contribuinte
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#enderecoObject (readonly)

Returns the value of attribute endereco.



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

def endereco
  @endereco
end

#endereco_cobrancaObject (readonly)

Returns the value of attribute endereco_cobranca.



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

def endereco_cobranca
  @endereco_cobranca
end

#fantasiaObject (readonly)

Returns the value of attribute fantasia.



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

def fantasia
  @fantasia
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#ieObject (readonly)

Returns the value of attribute ie.



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

def ie
  @ie
end

#nomeObject (readonly)

Returns the value of attribute nome.



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

def nome
  @nome
end

#numero_documentoObject (readonly)

Returns the value of attribute numero_documento.



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

def numero_documento
  @numero_documento
end

#orgao_emissorObject (readonly)

Returns the value of attribute orgao_emissor.



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

def orgao_emissor
  @orgao_emissor
end

#pessoa_juridicaObject (readonly)

Returns the value of attribute pessoa_juridica.



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

def pessoa_juridica
  @pessoa_juridica
end

#rgObject (readonly)

Returns the value of attribute rg.



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

def rg
  @rg
end

#situacaoObject (readonly)

Returns the value of attribute situacao.



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

def situacao
  @situacao
end

#telefoneObject (readonly)

Returns the value of attribute telefone.



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

def telefone
  @telefone
end

Class Method Details

.find_by_id(id) ⇒ Object



11
12
13
14
# File 'lib/bling_api/customer.rb', line 11

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



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

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



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
62
63
64
65
66
67
68
69
70
# File 'lib/bling_api/customer.rb', line 35

def build_json
  {
    nome: nome,
    telefone: telefone,
    celular: celular,
    tipo: pessoa_juridica ? "J" : "F",
    indicadorIe: cod_contribuinte,
    ie: ie,
    numeroDocumento: numero_documento,
    orgaoEmissor: orgao_emissor,
    rg: rg,
    fantasia: fantasia,
    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

#createObject



86
87
88
89
# File 'lib/bling_api/customer.rb', line 86

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



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bling_api/customer.rb', line 72

def update attributes
  attributes.each do |key, value|
    if key == :endereco || key == :endereco_cobranca
      value.each do |k, v|
        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(id)
end