bling_api 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '048de75fb5cc935a0ed4c2b4708ff9c9f9603bfdd8f96442035e351eaf2b8ead'
4
+ data.tar.gz: 75904baa8b3aea1339d73d663adef8518d60a3ecb9dafa07740a855b79113c6b
5
+ SHA512:
6
+ metadata.gz: 6231c38d87ac59d1809c6fb346a6a4fe992c73480f8cbd8179c76934327b38e02524e1e77a083b66309e2e4141fb3a55ce551bcb17e8473541feefc3c1d6615b
7
+ data.tar.gz: 3cc354154211ee42fd8abc69316ed7837be5f8762a8368b7c0c07b4ca55a82158b1c203e3f74aa0df4fdda9f5f57af20c8f61ff46f5512eeb621de918dd24233
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bling_api.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "tldr"
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bling_api (0.1.0)
5
+ ac
6
+ base64
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ac (0.1.1)
12
+ typhoeus
13
+ attr_extras (7.1.0)
14
+ base64 (0.1.1)
15
+ concurrent-ruby (1.2.2)
16
+ diff-lcs (1.5.0)
17
+ ethon (0.16.0)
18
+ ffi (>= 1.15.0)
19
+ ffi (1.16.3)
20
+ optimist (3.1.0)
21
+ patience_diff (1.2.0)
22
+ optimist (~> 3.0)
23
+ rake (13.1.0)
24
+ super_diff (0.10.0)
25
+ attr_extras (>= 6.2.4)
26
+ diff-lcs
27
+ patience_diff
28
+ tldr (0.9.5)
29
+ concurrent-ruby (~> 1.2)
30
+ super_diff (~> 0.10)
31
+ typhoeus (1.4.0)
32
+ ethon (>= 0.9.0)
33
+
34
+ PLATFORMS
35
+ x86_64-linux
36
+
37
+ DEPENDENCIES
38
+ bling_api!
39
+ rake (~> 13.0)
40
+ tldr
41
+
42
+ BUNDLED WITH
43
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 caio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # BlingApi
2
+
3
+ TODO: Delete this and the text below, and describe your gem
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.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bling_api.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,57 @@
1
+ module BlingApi
2
+ class Client < Ac::Base
3
+ BASE_URL = "https://www.bling.com.br/Api/v3"
4
+ attr_reader :access_token
5
+ def initialize(access_token=nil)
6
+ @headers = {
7
+ "Content-Type": "application/json"
8
+ }
9
+ super access_token
10
+ end
11
+
12
+ def update_contact(contact_id, body)
13
+ response = put("/contatos/#{contact_id}", body: body, headers: @headers){_1.code==204}
14
+ end
15
+
16
+ def get_order(order_id)
17
+ response = get("/pedidos/vendas/#{order_id}") {_1.json["data"]}
18
+ response.json["data"]
19
+ end
20
+
21
+ def get_contact(contact_id)
22
+ response = get("/contatos/#{contact_id}") {_1.json["data"]}
23
+ response.json["data"]
24
+ end
25
+
26
+ def find_contact_by_document_number(document_number)
27
+ response = get("/contatos?numeroDocumento=#{document_number.gsub(/\D/, "")}") {_1.json["data"]}
28
+ response.json["data"].first
29
+ end
30
+
31
+ def find_product_by_sku(sku)
32
+ response = get("/produtos?codigo=#{sku}") {_1.json["data"]}
33
+ response.json["data"].first
34
+ end
35
+
36
+ def create_contact(body)
37
+ response = post("/contatos", body: body, headers: @headers) {_1.json["data"]}
38
+ response.json["data"]
39
+ end
40
+
41
+ def create_order(body)
42
+ response = post("/pedidos/vendas", body: body, headers: @headers) {_1.json["data"]}
43
+ response.json["data"]
44
+ end
45
+
46
+ def refresh_token(client_id:, client_secret:, refresh_token:)
47
+ headers = {
48
+ "Content-Type": "application/x-www-form-urlencoded",
49
+ "Authorization": "Basic #{Base64.strict_encode64("#{client_id}:#{client_secret}")}"
50
+ }
51
+ body = "grant_type=refresh_token&refresh_token=#{refresh_token}"
52
+ response = post("/oauth/token", body: body, headers: headers) {_1.json["refresh_token"]}
53
+ response.json
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,96 @@
1
+ module BlingApi
2
+ class Customer
3
+ ACCESS_TOKEN = "0039bb7d2d3e3c6777983361204dc168ecbf1ef8"
4
+
5
+ def self.find_by_tax_id tax_id
6
+ response_json = Client.new(ACCESS_TOKEN).find_contact_by_document_number(tax_id)
7
+ if response_json
8
+ contact = Client.new(ACCESS_TOKEN).get_contact(response_json["id"])
9
+ new(**build_hash(contact))
10
+ end
11
+ end
12
+
13
+ def self.find_by_id id
14
+ contact = Client.new(ACCESS_TOKEN).get_contact(id)
15
+ new(**build_hash(contact))
16
+ end
17
+
18
+ attr_reader :nome, :pessoa_juridica, :telefone, :numero_documento, :id, :email, :endereco, :endereco_cobranca
19
+ def initialize(nome:, pessoa_juridica:, numero_documento:, id:nil, telefone:nil, email:nil, endereco:nil, endereco_cobranca:nil)
20
+ @id = id
21
+ @nome = nome
22
+ @pessoa_juridica = pessoa_juridica
23
+ @telefone = telefone
24
+ @numero_documento = numero_documento
25
+ @email = email
26
+ @endereco = endereco
27
+ @endereco_cobranca = endereco_cobranca
28
+ end
29
+
30
+ def build_json
31
+ {
32
+ "nome": nome,
33
+ "telefone": telefone,
34
+ "tipo": pessoa_juridica ? "J" : "F",
35
+ "numeroDocumento": numero_documento,
36
+ "email": email,
37
+ "endereco": {
38
+ "geral": {
39
+ "endereco": endereco["endereco"],
40
+ "cep": endereco["cep"],
41
+ "bairro": endereco["bairro"],
42
+ "municipio": endereco["municipio"],
43
+ "uf": endereco["uf"],
44
+ "numero": endereco["numero"],
45
+ "complemento": endereco["complemento"]
46
+ },
47
+ "cobranca": {
48
+ "endereco": endereco_cobranca["endereco"],
49
+ "cep": endereco_cobranca["cep"],
50
+ "bairro": endereco_cobranca["bairro"],
51
+ "municipio": endereco_cobranca["municipio"],
52
+ "uf": endereco_cobranca["uf"],
53
+ "numero": endereco_cobranca["numero"],
54
+ "complemento": endereco_cobranca["complemento"]
55
+ }
56
+ }
57
+ }
58
+ end
59
+
60
+ def update attributes
61
+ attributes.each do |key, value|
62
+ if key == :endereco || key == :endereco_cobranca
63
+ value.each do |k, v|
64
+ self.send(key)[k] = v
65
+ end
66
+ else
67
+ instance_variable_set("@#{key}", value)
68
+ end
69
+ end
70
+ Client.new(ACCESS_TOKEN).update_contact(id, JSON.dump(build_json))
71
+ self.class.find_by_id(self.id)
72
+ end
73
+
74
+ def create
75
+ response = Client.new(ACCESS_TOKEN).create_contact(JSON.dump(build_json))
76
+ self.class.find_by_id(response["id"])
77
+ end
78
+
79
+ private
80
+
81
+ def self.build_hash json_response
82
+ {
83
+ id: json_response["id"],
84
+ nome: json_response["nome"],
85
+ telefone: json_response["telefone"],
86
+ pessoa_juridica: json_response["tipo"] == "J",
87
+ numero_documento: json_response["numeroDocumento"],
88
+ email: json_response["email"],
89
+ endereco: json_response["endereco"]["geral"],
90
+ endereco_cobranca: json_response["endereco"]["cobranca"]
91
+ }
92
+ end
93
+
94
+ end
95
+ end
96
+
@@ -0,0 +1,90 @@
1
+ module BlingApi
2
+ class Order
3
+ ACCESS_TOKEN = "0039bb7d2d3e3c6777983361204dc168ecbf1ef8"
4
+
5
+ def self.find_by_id id
6
+ response = Client.new(ACCESS_TOKEN).get_order(id)
7
+ new(**build_hash(response))
8
+ end
9
+
10
+ attr_reader :id, :numero, :id_contato, :items, :parcelas, :numero_loja, :data, :total_produtos, :total, :id_loja, :numero_pedido_compra, :observacoes, :observacoes_internas, :valor_desconto, :unidade_desconto, :id_categoria, :transporte, :id_vendedor
11
+ def initialize(id:nil, numero:nil, id_contato:, items:, parcelas:nil, numero_loja:, data:, total_produtos:, total:, id_loja:, numero_pedido_compra:nil, observacoes:, observacoes_internas:, valor_desconto:, unidade_desconto:, id_categoria:, transporte:nil, id_vendedor:)
12
+ @id = id
13
+ @numero = numero
14
+ @id_contato = id_contato
15
+ @items = items
16
+ @parcelas = parcelas
17
+ @numero_loja = numero_loja
18
+ @data = data
19
+ @total_produtos = total_produtos
20
+ @total = total
21
+ @id_loja = id_loja
22
+ @numero_pedido_compra = numero_pedido_compra
23
+ @observacoes = observacoes
24
+ @observacoes_internas = observacoes_internas
25
+ @valor_desconto = valor_desconto
26
+ @unidade_desconto = unidade_desconto
27
+ @id_categoria = id_categoria
28
+ @transporte = transporte
29
+ @id_vendedor = id_vendedor
30
+ end
31
+
32
+ def build_json
33
+ {
34
+ "numeroLoja": numero_loja,
35
+ "data": data,
36
+ "loja": {
37
+ "id": id_loja
38
+ },
39
+ "contato": {
40
+ "id": id_contato
41
+ },
42
+ "itens": items,
43
+ "vendedor": {
44
+ "id": id_vendedor
45
+ },
46
+ "transporte": transporte,
47
+ "parcelas": parcelas,
48
+ "valorDesconto": valor_desconto,
49
+ "totalProdutos": total_produtos,
50
+ "total": total,
51
+ "observacoes": observacoes,
52
+ "observacoesInternas": observacoes_internas,
53
+ "categoria": {
54
+ "id": id_categoria
55
+ }
56
+ }
57
+ end
58
+
59
+ def create
60
+ response = Client.new(ACCESS_TOKEN).create_order(JSON.dump(build_json))
61
+ self.class.find_by_id(response["id"])
62
+ end
63
+
64
+ private
65
+
66
+ def self.build_hash json_response
67
+ {
68
+ id: json_response["id"],
69
+ id_contato: json_response["contato"]["id"],
70
+ items: json_response["itens"],
71
+ parcelas: json_response["parcelas"],
72
+ numero_loja: json_response["numeroLoja"],
73
+ numero: json_response["numero"],
74
+ data: json_response["data"],
75
+ total_produtos: json_response["totalProdutos"],
76
+ total: json_response["total"],
77
+ id_loja: json_response["loja"]["id"],
78
+ numero_pedido_compra: json_response["numeroPedidoCompra"],
79
+ observacoes: json_response["observacoes"],
80
+ observacoes_internas: json_response["observacoesInternas"],
81
+ valor_desconto: json_response["valorDesconto"],
82
+ unidade_desconto: json_response["unidadeDesconto"],
83
+ id_categoria: json_response["categoria"]["id"],
84
+ transporte: json_response["transporte"],
85
+ id_vendedor: json_response["vendedor"]["id"]
86
+ }
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,30 @@
1
+ module BlingApi
2
+ class Product
3
+ ACCESS_TOKEN = "0039bb7d2d3e3c6777983361204dc168ecbf1ef8"
4
+
5
+ def self.find_by_sku sku
6
+ response_json = Client.new(ACCESS_TOKEN).find_product_by_sku(sku)
7
+ if response_json
8
+ new(**build_hash(response_json))
9
+ end
10
+ end
11
+
12
+ attr_reader :id, :nome, :sku
13
+ def initialize(id:, nome:, sku:)
14
+ @id = id
15
+ @nome = nome
16
+ @sku = sku
17
+ end
18
+
19
+ private
20
+
21
+ def self.build_hash json_response
22
+ {
23
+ id: json_response["id"],
24
+ nome: json_response["nome"],
25
+ sku: json_response["codigo"]
26
+ }
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlingApi
4
+ VERSION = "0.1.0"
5
+ end
data/lib/bling_api.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ac"
4
+ require "base64"
5
+ require_relative "bling_api/version"
6
+ require_relative "bling_api/client"
7
+ require_relative "bling_api/customer"
8
+ require_relative "bling_api/order"
9
+ require_relative "bling_api/product"
10
+
11
+ module BlingApi
12
+ class Error < StandardError; end
13
+
14
+ class Configuration
15
+ attr_accessor :access_token
16
+
17
+ def initialize
18
+ @api_key = nil
19
+ end
20
+ end
21
+
22
+ class << self
23
+ attr_accessor :access_token
24
+ end
25
+
26
+ def self.configure
27
+ self.confiration ||= Configuration.new
28
+ yield confiration
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bling_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - caio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ac
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: base64
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - caioif4@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - Gemfile.lock
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/bling_api.rb
54
+ - lib/bling_api/client.rb
55
+ - lib/bling_api/customer.rb
56
+ - lib/bling_api/order.rb
57
+ - lib/bling_api/product.rb
58
+ - lib/bling_api/version.rb
59
+ homepage: https://github.com/CaioGarcia1
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/CaioGarcia1
64
+ source_code_uri: https://github.com/CaioGarcia1
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.6.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.4.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: ''
84
+ test_files: []