bling_api 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7660ede0b54064fd04c509c67fd578f9366b0f72d4a27b367b89738725234b3
4
- data.tar.gz: eaac49092264042c1f125973c75faa62e1b7b9f9df95cede03d22c87c68cc526
3
+ metadata.gz: 735aea047eb407f37c037ab030f21ab13eec0f874c0060c1f5a13de14d122358
4
+ data.tar.gz: 161cc0f05caaeb6e076f4e6a927960f17e8082d36cdace8de3ea4416bdc7af4e
5
5
  SHA512:
6
- metadata.gz: e4487f802f3fb8d64cd73545964007f2df58aa7f830fb712ca3ee462c9ab3413e5e6eeaac6d5c25a8b6c661c840ab294068349ad062e7934ed64a94b9d71bb07
7
- data.tar.gz: 0ce79f0fcb362ad8b93092801e268ddf210efec6fe08dce520a24fdf3d8f76090dfd8b5718e5111783d299a2dbd61e6d5cb4a3695e676c58f9f10df598b0377e
6
+ metadata.gz: bdebc58ab7ace6955a016f8bdd19f4080b44cc39fd5db06b194bed2cc271b5dc3cebdf9d6d7547858ac0219935a6a9cf7ac48f8a2bb48f19e2004a86ff4a792c
7
+ data.tar.gz: 969908c5a187bca1e628f6fa7c8d296cf359ab9533181f2bce56dba7a40c9f3ff7fdcd5625a8bc84703aa0f2ad5bd0af2ce08d397e806ad602c18da01f336bcb
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bling_api (0.2.0)
4
+ bling_api (1.0.2)
5
5
  ac
6
6
  base64
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- ac (0.1.1)
11
+ ac (0.2.0)
12
12
  typhoeus
13
13
  attr_extras (7.1.0)
14
14
  base64 (0.2.0)
@@ -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
+ ```
@@ -1,6 +1,8 @@
1
1
  module BlingApi
2
2
  class Client < Ac::Base
3
3
  BASE_URL = "https://www.bling.com.br/Api/v3"
4
+ SAVE_RESPONSES = true
5
+
4
6
  attr_reader :access_token
5
7
  def initialize(access_token = nil)
6
8
  @headers = {
@@ -10,46 +12,46 @@ module BlingApi
10
12
  end
11
13
 
12
14
  def update_contact(contact_id, body)
13
- response = put("/contatos/#{contact_id}", body: body, headers: @headers) { _1.code == 204 }
15
+ put("/contatos/#{contact_id}", body: body, headers: @headers) { |response| validate_response(response, "") }
14
16
  end
15
17
 
16
18
  def get_order(order_id)
17
- response = get("/pedidos/vendas/#{order_id}") { _1.json["data"] }
19
+ response = get("/pedidos/vendas/#{order_id}") { |response| validate_response(response, "data") }
18
20
  response.json["data"]
19
21
  end
20
22
 
21
23
  def get_contact(contact_id)
22
- response = get("/contatos/#{contact_id}") { _1.json["data"] }
24
+ response = get("/contatos/#{contact_id}") { |response| validate_response(response, "data") }
23
25
  response.json["data"]
24
26
  end
25
27
 
26
28
  def get_seller(seller_id)
27
- response = get("/vendedores/#{seller_id}") { _1.json["data"] }
29
+ response = get("/vendedores/#{seller_id}") { |response| validate_response(response, "data") }
28
30
  response.json["data"]
29
31
  end
30
32
 
31
33
  def get_sellers(page, limit, situation)
32
- response = get("/vendedores?pagina=#{page}&limite=#{limit}&situacaoContato=#{situation}") { _1.json["data"] }
34
+ response = get("/vendedores?pagina=#{page}&limite=#{limit}&situacaoContato=#{situation}") { |response| validate_response(response, "data") }
33
35
  response.json["data"]
34
36
  end
35
37
 
36
38
  def find_contact_by_document_number(document_number)
37
- response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") { _1.json["data"] }
39
+ response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") { |response| validate_response(response, "data") }
38
40
  response.json["data"].first
39
41
  end
40
42
 
41
43
  def find_product_by_sku(sku)
42
- response = get("/produtos?codigo=#{sku}") { _1.json["data"] }
44
+ response = get("/produtos?codigo=#{sku}") { |response| validate_response(response, "data") }
43
45
  response.json["data"].first
44
46
  end
45
47
 
46
48
  def create_contact(body)
47
- response = post("/contatos", body: body, headers: @headers) { _1.json["data"] }
49
+ response = post("/contatos", body: body, headers: @headers) { |response| validate_response(response, "data") }
48
50
  response.json["data"]
49
51
  end
50
52
 
51
53
  def create_order(body)
52
- response = post("/pedidos/vendas", body: body, headers: @headers) { _1.json["data"] }
54
+ response = post("/pedidos/vendas", body: body, headers: @headers) { |response| validate_response(response, "data") }
53
55
  response.json["data"]
54
56
  end
55
57
 
@@ -59,8 +61,13 @@ module BlingApi
59
61
  Authorization: "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}"
60
62
  }
61
63
  body = "grant_type=refresh_token&refresh_token=#{refresh_token}"
62
- response = post("/oauth/token", body: body, headers: headers) { _1.json["refresh_token"] }
64
+ response = post("/oauth/token", body: body, headers: headers) { |response| validate_response(response, "refresh_token") }
63
65
  response.json
64
66
  end
67
+
68
+ def validate_response(response, required_response_key)
69
+ puts response.json unless response.success?
70
+ ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
71
+ end
65
72
  end
66
73
  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.2"
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.2
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-10-28 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: ''