bling_api 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7660ede0b54064fd04c509c67fd578f9366b0f72d4a27b367b89738725234b3
4
- data.tar.gz: eaac49092264042c1f125973c75faa62e1b7b9f9df95cede03d22c87c68cc526
3
+ metadata.gz: 725280eed39d0e30e05931c9a9cc4728e7abc0a285b61f90c78a893098cfcdf6
4
+ data.tar.gz: 9a45a45083e53fd24515d3c83c5675364e6e071f933d568cfc03843b8c768ef1
5
5
  SHA512:
6
- metadata.gz: e4487f802f3fb8d64cd73545964007f2df58aa7f830fb712ca3ee462c9ab3413e5e6eeaac6d5c25a8b6c661c840ab294068349ad062e7934ed64a94b9d71bb07
7
- data.tar.gz: 0ce79f0fcb362ad8b93092801e268ddf210efec6fe08dce520a24fdf3d8f76090dfd8b5718e5111783d299a2dbd61e6d5cb4a3695e676c58f9f10df598b0377e
6
+ metadata.gz: cc5a72286528d648629bd68e705d1079c295ce32e1b49aafc49c6995c47a07b276229fe8bd94edd5dcdf696ab710a2375108da1057ba656818689b98848a8f6e
7
+ data.tar.gz: cb8499a2c765182040e7754e94ecb27c8026f47a3192c9c43f04105f9545dd437302d4001118b9ff973e8b7a49336a8a5ca5d5bf59707728c54f13c8faf588ef
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bling_api (0.2.0)
4
+ bling_api (1.0.1)
5
5
  ac
6
6
  base64
7
7
 
@@ -33,6 +33,7 @@ GEM
33
33
 
34
34
  PLATFORMS
35
35
  arm64-darwin-22
36
+ arm64-darwin-23
36
37
  x86_64-linux
37
38
 
38
39
  DEPENDENCIES
data/README.md CHANGED
@@ -1,37 +1,120 @@
1
1
  # BlingApi
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Wrapper de integração com a API v3 do ERP Bling!.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bling_api`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ [Documentação API v3 Bling!](https://developer.bling.com.br/bling-api)
6
6
 
7
- ## Installation
7
+ ### Funcionalidades:
8
+ - Clientes: create, update, find_by_id, find_by_tax_id (CPF/CNPJ)
9
+ - Pedido de venda: create, find_by_id
10
+ - Produtos: find_by_sku
11
+ - Vendedores: get_sellers (listagem), find_by_id
8
12
 
9
- Add bling_api to your Gemfile:
13
+ ## Instalação
14
+
15
+ Adicionar bling_api no Gemfile:
10
16
 
11
17
  ```ruby
12
18
  gem 'bling_api'
13
19
  ```
20
+ ou
21
+ ```shell
22
+ bundle add bling_api
23
+ ```
14
24
 
15
- Bundle your dependencies and run the installation generator:
25
+ Instalar a gem:
16
26
 
17
27
  ```shell
18
28
  bin/rails generate bling_api:install
19
29
  ```
20
30
 
21
- ## Usage
22
-
23
- TODO: Write usage instructions here
31
+ ## Uso
24
32
 
25
- ## Development
26
-
27
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
-
29
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
-
31
- ## Contributing
33
+ Configurar access_token no initializer ou direto no código:
34
+ ```ruby
35
+ BlingApi.configure do |config|
36
+ config.access_token = "ACCESS_TOKEN"
37
+ end
38
+ ```
32
39
 
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bling_api.
40
+ ### Exemplos
34
41
 
35
- ## License
42
+ #### Token
36
43
 
37
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+ ```ruby
45
+ api_client = BlingApi::Client.new.refresh_token(client_id: "123456", client_secret: "101112131415", refresh_token: "abcde12345")
46
+ #retorna o JSON da resposta
47
+ puts api_client["access_token"] # "a1b2c3d4e5"
48
+ puts api_client["expires_in"] # 21600
49
+ ```
50
+ #### Produto
51
+ ```ruby
52
+ # Encontrar um produto a partir do SKU
53
+ product = BlingApi::Product.find_by_sku("123456")
54
+ puts product.sku # "123456"
55
+ ```
56
+ #### Pedido de venda
57
+ ```ruby
58
+ # Instancia o objeto
59
+ order = BlingApi::Order.new(
60
+ id_contato: 16693257776,
61
+ items: [{
62
+ id: 16219735350,
63
+ quantidade: 1,
64
+ valor: 20.25,
65
+ produto: {
66
+ id: 16219735350
67
+ }
68
+ }],
69
+ parcelas: [{
70
+ dataVencimento: "2024-06-06",
71
+ valor: 20.25,
72
+ formaPagamento: {
73
+ id: 4776248
74
+ }
75
+ }],
76
+ numero_loja: "ABC-123",
77
+ data: "2024-06-06",
78
+ total_produtos: 20.25,
79
+ total: 20.25,
80
+ id_loja: 204683421,
81
+ numero_pedido_compra: "ABC-123",
82
+ observacoes: "Observação",
83
+ observacoes_internas: "Obervação interna",
84
+ valor_desconto: 0.00,
85
+ id_categoria: 14653794318,
86
+ transporte: {
87
+ fretePorConta: 1,
88
+ frete: 0.00,
89
+ quantidadeVolumes: 1,
90
+ contato: {
91
+ nome: "LOGISTICA S.A"
92
+ },
93
+ etiqueta: {
94
+ nome: "João da Silva",
95
+ endereco: "Rua da Saudade",
96
+ numero: "120",
97
+ complemento: "",
98
+ municipio: "São Paulo",
99
+ uf: "SP",
100
+ bairro: "Centro",
101
+ cep: "01002010",
102
+ nomePais: "Brasil"
103
+ },
104
+ volumes: [
105
+ {
106
+ servico: "SEDEX",
107
+ codigoRastreamento: ""
108
+ }
109
+ ]
110
+ },
111
+ id_vendedor: 15596298834,
112
+ pessoa_juridica: false,
113
+ numero_documento: "123.123.123-87"
114
+ )
115
+
116
+ # Cria o pedido de venda
117
+ created_order = order.create
118
+
119
+ puts created_order.numero # 102030
120
+ ```
@@ -10,46 +10,46 @@ module BlingApi
10
10
  end
11
11
 
12
12
  def update_contact(contact_id, body)
13
- response = put("/contatos/#{contact_id}", body: body, headers: @headers) { _1.code == 204 }
13
+ put("/contatos/#{contact_id}", body: body, headers: @headers) { |response| validate_response(response, "") }
14
14
  end
15
15
 
16
16
  def get_order(order_id)
17
- response = get("/pedidos/vendas/#{order_id}") { _1.json["data"] }
17
+ response = get("/pedidos/vendas/#{order_id}") { |response| validate_response(response, "data") }
18
18
  response.json["data"]
19
19
  end
20
20
 
21
21
  def get_contact(contact_id)
22
- response = get("/contatos/#{contact_id}") { _1.json["data"] }
22
+ response = get("/contatos/#{contact_id}") { |response| validate_response(response, "data") }
23
23
  response.json["data"]
24
24
  end
25
25
 
26
26
  def get_seller(seller_id)
27
- response = get("/vendedores/#{seller_id}") { _1.json["data"] }
27
+ response = get("/vendedores/#{seller_id}") { |response| validate_response(response, "data") }
28
28
  response.json["data"]
29
29
  end
30
30
 
31
31
  def get_sellers(page, limit, situation)
32
- response = get("/vendedores?pagina=#{page}&limite=#{limit}&situacaoContato=#{situation}") { _1.json["data"] }
32
+ response = get("/vendedores?pagina=#{page}&limite=#{limit}&situacaoContato=#{situation}") { |response| validate_response(response, "data") }
33
33
  response.json["data"]
34
34
  end
35
35
 
36
36
  def find_contact_by_document_number(document_number)
37
- response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") { _1.json["data"] }
37
+ response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") { |response| validate_response(response, "data") }
38
38
  response.json["data"].first
39
39
  end
40
40
 
41
41
  def find_product_by_sku(sku)
42
- response = get("/produtos?codigo=#{sku}") { _1.json["data"] }
42
+ response = get("/produtos?codigo=#{sku}") { |response| validate_response(response, "data") }
43
43
  response.json["data"].first
44
44
  end
45
45
 
46
46
  def create_contact(body)
47
- response = post("/contatos", body: body, headers: @headers) { _1.json["data"] }
47
+ response = post("/contatos", body: body, headers: @headers) { |response| validate_response(response, "data") }
48
48
  response.json["data"]
49
49
  end
50
50
 
51
51
  def create_order(body)
52
- response = post("/pedidos/vendas", body: body, headers: @headers) { _1.json["data"] }
52
+ response = post("/pedidos/vendas", body: body, headers: @headers) { |response| validate_response(response, "data") }
53
53
  response.json["data"]
54
54
  end
55
55
 
@@ -59,8 +59,13 @@ module BlingApi
59
59
  Authorization: "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}"
60
60
  }
61
61
  body = "grant_type=refresh_token&refresh_token=#{refresh_token}"
62
- response = post("/oauth/token", body: body, headers: headers) { _1.json["refresh_token"] }
62
+ response = post("/oauth/token", body: body, headers: headers) { |response| validate_response(response, "refresh_token") }
63
63
  response.json
64
64
  end
65
+
66
+ def validate_response(response, required_response_key)
67
+ puts response.json unless response.success?
68
+ ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
69
+ end
65
70
  end
66
71
  end
@@ -102,8 +102,6 @@ module BlingApi
102
102
  self.class.find_by_id(response["id"])
103
103
  end
104
104
 
105
- private
106
-
107
105
  def self.build_hash json_response
108
106
  {
109
107
  id: json_response["id"],
@@ -65,8 +65,6 @@ module BlingApi
65
65
  self.class.find_by_id(response["id"])
66
66
  end
67
67
 
68
- private
69
-
70
68
  def self.build_hash json_response
71
69
  {
72
70
  id: json_response["id"],
@@ -14,8 +14,6 @@ module BlingApi
14
14
  @sku = sku
15
15
  end
16
16
 
17
- private
18
-
19
17
  def self.build_hash json_response
20
18
  {
21
19
  id: json_response["id"],
@@ -27,8 +27,6 @@ module BlingApi
27
27
  @situacao = situacao
28
28
  end
29
29
 
30
- private
31
-
32
30
  def self.build_hash json_response
33
31
  {
34
32
  id: json_response.dig("id"),
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlingApi
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bling_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - caio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-16 00:00:00.000000000 Z
11
+ date: 2024-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ac
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  - !ruby/object:Gem::Version
81
81
  version: '0'
82
82
  requirements: []
83
- rubygems_version: 3.5.3
83
+ rubygems_version: 3.5.10
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: ''