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:, codigo: nil, 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, financeiro: nil, pais: nil, tipos_contato: nil, pessoa_contato: nil, vendedor: nil, dados_adicionais: nil) ⇒ Customer



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bling_api/customer.rb', line 18

def initialize(nome:, codigo: nil, 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, financeiro: nil, pais: nil, tipos_contato: nil, pessoa_contato: nil, vendedor: nil, dados_adicionais: nil)
  @id = id
  @nome = nome
  @pessoa_juridica = pessoa_juridica
  @codigo = codigo
  @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
  @financeiro = financeiro
  @pais = pais
  @tipos_contato = tipos_contato
  @pessoa_contato = pessoa_contato
  @vendedor = vendedor
  @dados_adicionais = dados_adicionais
end

Instance Attribute Details

#celularObject (readonly)

Returns the value of attribute celular.



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

def celular
  @celular
end

#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

#codigoObject (readonly)

Returns the value of attribute codigo.



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

def codigo
  @codigo
end

#dados_adicionaisObject (readonly)

Returns the value of attribute dados_adicionais.



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

def dados_adicionais
  @dados_adicionais
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

#fantasiaObject (readonly)

Returns the value of attribute fantasia.



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

def fantasia
  @fantasia
end

#financeiroObject (readonly)

Returns the value of attribute financeiro.



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

def financeiro
  @financeiro
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#ieObject (readonly)

Returns the value of attribute ie.



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

def ie
  @ie
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

#orgao_emissorObject (readonly)

Returns the value of attribute orgao_emissor.



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

def orgao_emissor
  @orgao_emissor
end

#paisObject (readonly)

Returns the value of attribute pais.



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

def pais
  @pais
end

#pessoa_contatoObject (readonly)

Returns the value of attribute pessoa_contato.



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

def pessoa_contato
  @pessoa_contato
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

#rgObject (readonly)

Returns the value of attribute rg.



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

def rg
  @rg
end

#situacaoObject (readonly)

Returns the value of attribute situacao.



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

def situacao
  @situacao
end

#telefoneObject (readonly)

Returns the value of attribute telefone.



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

def telefone
  @telefone
end

#tipos_contatoObject (readonly)

Returns the value of attribute tipos_contato.



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

def tipos_contato
  @tipos_contato
end

#vendedorObject (readonly)

Returns the value of attribute vendedor.



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

def vendedor
  @vendedor
end

Class Method Details

.build_hash(json_response) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bling_api/customer.rb', line 111

def self.build_hash json_response
  {
    id: json_response["id"],
    nome: json_response["nome"],
    codigo: json_response["codigo"], 
    situacao: json_response["situacao"],
    telefone: json_response["telefone"],
    celular: json_response["celular"],
    fantasia: json_response["fantasia"],
    pessoa_juridica: json_response["tipo"] == "J",
    numero_documento: json_response["numeroDocumento"],
    rg: json_response["rg"],
    orgao_emissor: json_response["orgaoEmissor"],
    cod_contribuinte: json_response["indicadorIe"],
    ie: json_response["ie"],
    email: json_response["email"],
    endereco: json_response["endereco"]["geral"],
    endereco_cobranca: json_response["endereco"]["cobranca"],
    vendedor: json_response["vendedor"],
    dados_adicionais: json_response["dadosAdicionais"],
    financeiro: json_response["financeiro"],
    pais: json_response["pais"],
    tipos_contato: json_response["tiposContato"],
    pessoa_contato: json_response["pessoaContato"]
  }
end

.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



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bling_api/customer.rb', line 43

def build_json
  {
    nome: nome,
    telefone: phone_formatted(telefone),
    codigo: codigo,
    celular: phone_formatted(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,
    vendedor: vendedor,
    dadosAdicionais: dados_adicionais,
    pais: pais,
    tiposContato: tipos_contato,
    pessoaContato: pessoa_contato,
    financeiro: financeiro,
    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



101
102
103
104
# File 'lib/bling_api/customer.rb', line 101

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

#phone_formatted(phone) ⇒ Object



106
107
108
109
# File 'lib/bling_api/customer.rb', line 106

def phone_formatted(phone)
  return "" unless phone
  phone.gsub(/^\+55/, "")
end

#update(attributes) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bling_api/customer.rb', line 87

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