Class: BlingApi::Order

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_contato:, items:, numero_loja:, data:, total_produtos:, total:, id_loja:, observacoes:, observacoes_internas:, valor_desconto:, id_categoria:, id_vendedor:, id: nil, numero: nil, parcelas: nil, numero_pedido_compra: nil, transporte: nil, pessoa_juridica: nil, numero_documento: nil) ⇒ Order

Returns a new instance of Order.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bling_api/order.rb', line 10

def initialize(id_contato:, items:, numero_loja:, data:, total_produtos:, total:, id_loja:, observacoes:, observacoes_internas:, valor_desconto:, id_categoria:, id_vendedor:, id: nil, numero: nil, parcelas: nil, numero_pedido_compra: nil, transporte: nil, pessoa_juridica: nil, numero_documento: nil)
  @id = id
  @numero = numero
  @id_contato = id_contato
  @items = items
  @parcelas = parcelas
  @numero_loja = numero_loja
  @data = data
  @total_produtos = total_produtos
  @total = total
  @id_loja = id_loja
  @numero_pedido_compra = numero_pedido_compra
  @observacoes = observacoes
  @observacoes_internas = observacoes_internas
  @valor_desconto = valor_desconto
  @id_categoria = id_categoria
  @transporte = transporte
  @id_vendedor = id_vendedor
  @pessoa_juridica = pessoa_juridica
  @numero_documento = numero_documento
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def id
  @id
end

#id_categoriaObject (readonly)

Returns the value of attribute id_categoria.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def id_categoria
  @id_categoria
end

#id_contatoObject (readonly)

Returns the value of attribute id_contato.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def id_contato
  @id_contato
end

#id_lojaObject (readonly)

Returns the value of attribute id_loja.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def id_loja
  @id_loja
end

#id_vendedorObject (readonly)

Returns the value of attribute id_vendedor.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def id_vendedor
  @id_vendedor
end

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def items
  @items
end

#numeroObject (readonly)

Returns the value of attribute numero.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def numero
  @numero
end

#numero_documentoObject (readonly)

Returns the value of attribute numero_documento.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def numero_documento
  @numero_documento
end

#numero_lojaObject (readonly)

Returns the value of attribute numero_loja.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def numero_loja
  @numero_loja
end

#numero_pedido_compraObject (readonly)

Returns the value of attribute numero_pedido_compra.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def numero_pedido_compra
  @numero_pedido_compra
end

#observacoesObject (readonly)

Returns the value of attribute observacoes.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def observacoes
  @observacoes
end

#observacoes_internasObject (readonly)

Returns the value of attribute observacoes_internas.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def observacoes_internas
  @observacoes_internas
end

#parcelasObject (readonly)

Returns the value of attribute parcelas.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def parcelas
  @parcelas
end

#pessoa_juridicaObject (readonly)

Returns the value of attribute pessoa_juridica.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def pessoa_juridica
  @pessoa_juridica
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def total
  @total
end

#total_produtosObject (readonly)

Returns the value of attribute total_produtos.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def total_produtos
  @total_produtos
end

#transporteObject (readonly)

Returns the value of attribute transporte.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def transporte
  @transporte
end

#valor_descontoObject (readonly)

Returns the value of attribute valor_desconto.



9
10
11
# File 'lib/bling_api/order.rb', line 9

def valor_desconto
  @valor_desconto
end

Class Method Details

.build_hash(json_response) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bling_api/order.rb', line 69

def self.build_hash json_response
  {
    id: json_response["id"],
    id_contato: json_response["contato"]["id"],
    pessoa_juridica: json_response.dig("contato", "tipoPessoa") == "J",
    numero_documento: json_response.dig("contato", "numeroDocumento"),
    items: json_response["itens"],
    parcelas: json_response["parcelas"],
    numero_loja: json_response["numeroLoja"],
    numero: json_response["numero"],
    data: json_response["data"],
    total_produtos: json_response["totalProdutos"],
    total: json_response["total"],
    id_loja: json_response["loja"]["id"],
    numero_pedido_compra: json_response["numeroPedidoCompra"],
    observacoes: json_response["observacoes"],
    observacoes_internas: json_response["observacoesInternas"],
    valor_desconto: json_response["desconto"]["valor"],
    id_categoria: json_response["categoria"]["id"],
    transporte: json_response["transporte"],
    id_vendedor: json_response["vendedor"]["id"]
  }
end

.find_by_id(id) ⇒ Object



4
5
6
7
# File 'lib/bling_api/order.rb', line 4

def self.find_by_id id
  response = Client.new(BlingApi.configuration.access_token).get_order(id)
  new(**build_hash(response))
end

Instance Method Details

#build_jsonObject



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
60
61
62
# File 'lib/bling_api/order.rb', line 32

def build_json
  {
    numeroLoja: numero_loja,
    data: data,
    loja: {
      id: id_loja
    },
    contato: {
      id: id_contato,
      tipoPessoa: pessoa_juridica ? "J" : "F",
      numeroDocumento: numero_documento
    },
    itens: items,
    vendedor: {
      id: id_vendedor
    },
    transporte: transporte,
    parcelas: parcelas,
    desconto: {
      valor: valor_desconto,
      unidade: "REAL"
    },
    totalProdutos: total_produtos,
    total: total,
    observacoes: observacoes,
    observacoesInternas: observacoes_internas,
    categoria: {
      id: id_categoria
    }
  }
end

#createObject



64
65
66
67
# File 'lib/bling_api/order.rb', line 64

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