enotas_api 0.1.0 → 1.0.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +6 -2
  4. data/CHANGELOG.md +3 -1
  5. data/README.md +85 -76
  6. data/Rakefile +3 -6
  7. data/lib/enotas_api/common/entity.rb +7 -19
  8. data/lib/enotas_api/common/json_result.rb +42 -0
  9. data/lib/enotas_api/common/raw_result.rb +21 -0
  10. data/lib/enotas_api/common/request.rb +32 -27
  11. data/lib/enotas_api/request_provider.rb +29 -16
  12. data/lib/enotas_api/support/{dynamic_attributes.rb → attributable.rb} +12 -9
  13. data/lib/enotas_api/support/encoding_helper.rb +0 -5
  14. data/lib/enotas_api/support/type_handler.rb +30 -0
  15. data/lib/enotas_api/v1/baixar_pdf_nfs.rb +15 -0
  16. data/lib/enotas_api/v1/baixar_pdf_nfs_id_externo.rb +15 -0
  17. data/lib/enotas_api/v1/baixar_xml_nfs.rb +15 -0
  18. data/lib/enotas_api/v1/baixar_xml_nfs_id_externo.rb +15 -0
  19. data/lib/enotas_api/v1/cancelar_nfs.rb +13 -0
  20. data/lib/enotas_api/v1/cancelar_nfs_id_externo.rb +13 -0
  21. data/lib/enotas_api/v1/consultar_caracteristicas_prefeitura.rb +1 -6
  22. data/lib/enotas_api/v1/consultar_cidades_servico_municipal_unificado.rb +3 -3
  23. data/lib/enotas_api/v1/consultar_empresa.rb +1 -6
  24. data/lib/enotas_api/v1/consultar_nfs.rb +13 -0
  25. data/lib/enotas_api/v1/consultar_nfs_id_externo.rb +13 -0
  26. data/lib/enotas_api/v1/consultar_servicos_municipais.rb +1 -7
  27. data/lib/enotas_api/v1/desabilitar_empresa.rb +13 -0
  28. data/lib/enotas_api/v1/emitir_nfs.rb +13 -0
  29. data/lib/enotas_api/v1/entities/empresa.rb +23 -20
  30. data/lib/enotas_api/v1/entities/empresa_configuracoes.rb +9 -7
  31. data/lib/enotas_api/v1/entities/empresa_endereco.rb +20 -0
  32. data/lib/enotas_api/v1/entities/nfs.rb +19 -0
  33. data/lib/enotas_api/v1/entities/nfs_cliente.rb +16 -0
  34. data/lib/enotas_api/v1/entities/nfs_cliente_endereco.rb +17 -0
  35. data/lib/enotas_api/v1/entities/nfs_servico.rb +21 -0
  36. data/lib/enotas_api/v1/habilitar_empresa.rb +13 -0
  37. data/lib/enotas_api/v1/incluir_atualizar_empresa.rb +13 -0
  38. data/lib/enotas_api/v1/listar_empresas.rb +2 -2
  39. data/lib/enotas_api/v1/listar_notas_fiscais.rb +17 -0
  40. data/lib/enotas_api/v1/vincular_certificado.rb +14 -0
  41. data/lib/enotas_api/v1/vincular_logotipo.rb +14 -0
  42. data/lib/enotas_api/version.rb +1 -1
  43. metadata +26 -6
  44. data/lib/enotas_api/common/result.rb +0 -45
  45. data/lib/enotas_api/support/conversion_helper.rb +0 -31
  46. data/lib/enotas_api/v1/entities/endereco.rb +0 -18
@@ -4,27 +4,37 @@ require 'net/http'
4
4
 
5
5
  module EnotasApi
6
6
  class RequestProvider
7
- def get(url, headers = {})
8
- request(Net::HTTP::Get, url, nil, headers)
7
+ def get(url)
8
+ request(obj: Net::HTTP::Get, url: url)
9
9
  end
10
10
 
11
- def post(url, body, headers = {})
12
- request(Net::HTTP::Post, url, body, headers)
11
+ def post(url, body)
12
+ request(obj: Net::HTTP::Post, url: url, body: body)
13
+ end
14
+
15
+ def post_form(url, form_data)
16
+ request(obj: Net::HTTP::Post, url: url, form_data: form_data.to_a)
17
+ end
18
+
19
+ def delete(url)
20
+ request(obj: Net::HTTP::Delete, url: url)
13
21
  end
14
22
 
15
23
  private
16
24
 
17
- def request(request_obj, url, body, headers)
18
- track_request_time(url) do
19
- uri = URI(url)
25
+ def request(obj:, url:, body: nil, form_data: nil, headers: {})
26
+ uri = URI(url)
27
+
28
+ http = Net::HTTP.new(uri.host, uri.port)
29
+ http.use_ssl = true if url.start_with?('https')
20
30
 
21
- http = Net::HTTP.new(uri.host, uri.port)
22
- http.use_ssl = true if url.start_with?('https')
31
+ request = obj.new(uri)
32
+ configure_request_headers(request, headers)
23
33
 
24
- request = request_obj.new(uri)
25
- configure_request_headers(request, headers)
34
+ request.body = body if body
35
+ request.set_form(form_data, 'multipart/form-data') if form_data
26
36
 
27
- request.body = body if body
37
+ track_request_time(request) do
28
38
  response = http.request(request)
29
39
 
30
40
  [response.code.to_i, response.body]
@@ -34,17 +44,20 @@ module EnotasApi
34
44
  def configure_request_headers(request, custom_headers)
35
45
  {
36
46
  'Authorization' => "Basic #{EnotasApi::Configuration.current.api_key}",
47
+ "Accept": 'application/json',
37
48
  'Content-Type' => 'application/json',
38
- 'User-Agent' => 'Enotas-Ruby-SDK',
39
- "Accept": 'application/json'
49
+ 'User-Agent' => 'Enotas-Ruby-SDK'
40
50
  }.merge(custom_headers).each { |key, value| request[key] = value }
41
51
  end
42
52
 
43
- def track_request_time(url)
53
+ def track_request_time(request)
44
54
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
45
55
  value = yield
46
56
  finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
47
- EnotasApi::Configuration.current.logger.info("[RequestProvider] #{url} done in #{(finish - start).round(2)} seconds")
57
+
58
+ info = "#{request.method} #{request.path}"
59
+ time = "#{(finish - start).round(2)} seconds"
60
+ EnotasApi::Configuration.current.logger.info("[RequestProvider] #{info} done in #{time}")
48
61
  value
49
62
  end
50
63
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'conversion_helper'
3
+ require_relative 'type_handler'
4
4
 
5
5
  module EnotasApi
6
- module DynamicAttributes
6
+ module Attributable
7
7
  def self.included(base)
8
8
  base.extend ClassMethods
9
9
  end
@@ -17,7 +17,11 @@ module EnotasApi
17
17
  end
18
18
 
19
19
  def attribute_changed?(attribute)
20
- attributes_changed.includes?(attribute)
20
+ attributes_changed.include?(attribute)
21
+ end
22
+
23
+ def attributes
24
+ self.class.attributes
21
25
  end
22
26
 
23
27
  def set(attributes)
@@ -34,16 +38,15 @@ module EnotasApi
34
38
 
35
39
  module ClassMethods
36
40
  def attribute(name, type)
37
- raise EnotasApi::Error, "Type #{type} not supported" unless EnotasApi::ConversionHelper.support_type?(type)
41
+ handler = EnotasApi::TypeHandler.new(type)
38
42
 
39
43
  (@attributes ||= {})[name] = type
40
-
41
- define_method name do
42
- instance_variable_get("@#{name}")
43
- end
44
+ attr_reader name
44
45
 
45
46
  define_method "#{name}=" do |value|
46
- instance_variable_set("@#{name}", EnotasApi::ConversionHelper.convert_value(type, value))
47
+ handler.validate!(value)
48
+
49
+ instance_variable_set("@#{name}", handler.entity? && value.is_a?(Hash) ? type.new(value) : value)
47
50
  (@attributes_changed ||= []) << name
48
51
  end
49
52
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erb'
4
- require 'CGI'
5
4
 
6
5
  module EnotasApi
7
6
  module EncodingHelper
@@ -9,10 +8,6 @@ module EnotasApi
9
8
  ERB::Util.url_encode(value)
10
9
  end
11
10
 
12
- def query_encode(value)
13
- CGI.escape(value)
14
- end
15
-
16
11
  def encode_query_params(params)
17
12
  URI.encode_www_form(params.compact { |_k, v| v.nil? })
18
13
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnotasApi
4
+ class TypeHandler
5
+ VALIDATOR = {
6
+ boolean: ->(value, _type) { value.is_a?(TrueClass) || value.is_a?(FalseClass) },
7
+ decimal: ->(value, _type) { value.is_a?(Float) || value.is_a?(Integer) },
8
+ integer: ->(value, _type) { value.is_a?(Integer) },
9
+ string: ->(value, _type) { value.is_a?(String) },
10
+ entity: ->(value, type) { value.is_a?(type) || value.is_a?(Hash) }
11
+ }.freeze
12
+
13
+ def initialize(type)
14
+ @type = type
15
+ @validator_type = type.is_a?(Class) && type < EnotasApi::Entity ? :entity : type
16
+ @validator = VALIDATOR[@validator_type]
17
+ raise EnotasApi::Error, "Type #{type} not supported" unless @validator
18
+ end
19
+
20
+ def entity?
21
+ @validator_type == :entity
22
+ end
23
+
24
+ def validate!(value)
25
+ return if value.nil? || @validator.call(value, @type)
26
+
27
+ raise EnotasApi::Error, "Invalid value set '#{value}:#{value.class}' for type '#{@type}'"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+ require_relative '../common/raw_result'
5
+
6
+ module EnotasApi
7
+ module V1
8
+ class BaixarPdfNfs < EnotasApi::Request
9
+ def initialize(empresa_id, nfs_id)
10
+ uri = "/v1/empresas/#{url_encode(empresa_id)}/nfes/#{url_encode(nfs_id)}/pdf"
11
+ super(uri: uri, result_object: EnotasApi::RawResult)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+ require_relative '../common/raw_result'
5
+
6
+ module EnotasApi
7
+ module V1
8
+ class BaixarPdfNfsIdExterno < EnotasApi::Request
9
+ def initialize(empresa_id, id_externo)
10
+ uri = "/v1/empresas/#{url_encode(empresa_id)}/nfes/porIdExterno/#{url_encode(id_externo)}/pdf"
11
+ super(uri: uri, result_object: EnotasApi::RawResult)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+ require_relative '../common/raw_result'
5
+
6
+ module EnotasApi
7
+ module V1
8
+ class BaixarXmlNfs < EnotasApi::Request
9
+ def initialize(empresa_id, nfs_id)
10
+ uri = "/v1/empresas/#{url_encode(empresa_id)}/nfes/#{url_encode(nfs_id)}/xml"
11
+ super(uri: uri)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+ require_relative '../common/raw_result'
5
+
6
+ module EnotasApi
7
+ module V1
8
+ class BaixarXmlNfsIdExterno < EnotasApi::Request
9
+ def initialize(empresa_id, id_externo)
10
+ uri = "/v1/empresas/#{url_encode(empresa_id)}/nfes/porIdExterno/#{url_encode(id_externo)}/xml"
11
+ super(uri: uri)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class CancelarNfs < EnotasApi::Request
8
+ def initialize(empresa_id, nfs_id)
9
+ super(method: :DELETE, uri: "/v1/empresas/#{url_encode(empresa_id)}/nfes/#{url_encode(nfs_id)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class CancelarNfsIdExterno < EnotasApi::Request
8
+ def initialize(empresa_id, id_externo)
9
+ super(method: :DELETE, uri: "/v1/empresas/#{url_encode(empresa_id)}/nfes/porIdExterno/#{url_encode(id_externo)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -6,12 +6,7 @@ module EnotasApi
6
6
  module V1
7
7
  class ConsultarCaracteristicasPrefeitura < EnotasApi::Request
8
8
  def initialize(codigo_ibge_cidade)
9
- @codigo_ibge_cidade = codigo_ibge_cidade
10
- super()
11
- end
12
-
13
- def uri
14
- "/v1/estados/cidades/#{url_encode(@codigo_ibge_cidade)}/provedor"
9
+ super(uri: "/v1/estados/cidades/#{url_encode(codigo_ibge_cidade)}/provedor")
15
10
  end
16
11
  end
17
12
  end
@@ -4,11 +4,11 @@ require_relative '../common/request'
4
4
 
5
5
  module EnotasApi
6
6
  module V1
7
- class ConsultarCidadesServicioMunicipalUnificado < EnotasApi::Request
7
+ class ConsultarCidadesServicoMunicipalUnificado < EnotasApi::Request
8
8
  paginable
9
9
 
10
- def uri
11
- '/v1/servicos/cidades'
10
+ def initialize
11
+ super(uri: '/v1/servicos/cidades')
12
12
  end
13
13
  end
14
14
  end
@@ -6,12 +6,7 @@ module EnotasApi
6
6
  module V1
7
7
  class ConsultarEmpresa < EnotasApi::Request
8
8
  def initialize(id)
9
- @id = id
10
- super()
11
- end
12
-
13
- def uri
14
- "/v1/empresas/#{url_encode(@id)}"
9
+ super(uri: "/v1/empresas/#{url_encode(id)}")
15
10
  end
16
11
  end
17
12
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class ConsultarNfs < EnotasApi::Request
8
+ def initialize(empresa_id, nfe_id)
9
+ super(uri: "/v1/empresas/#{url_encode(empresa_id)}/nfes/#{url_encode(nfe_id)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class ConsultarNfsIdExterno < EnotasApi::Request
8
+ def initialize(empresa_id, id_externo)
9
+ super(uri: "/v1/empresas/#{url_encode(empresa_id)}/nfes/porIdExterno/#{url_encode(id_externo)}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -9,13 +9,7 @@ module EnotasApi
9
9
  filterable :descricao, :contains
10
10
 
11
11
  def initialize(uf, nome_cidade)
12
- @uf = uf
13
- @nome_cidade = nome_cidade
14
- super()
15
- end
16
-
17
- def uri
18
- "/v1/estados/#{url_encode(@uf)}/cidades/#{url_encode(@nome_cidade)}/servicos"
12
+ super(uri: "/v1/estados/#{url_encode(uf)}/cidades/#{url_encode(nome_cidade)}/servicos")
19
13
  end
20
14
  end
21
15
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class DesabilitarEmpresa < EnotasApi::Request
8
+ def initialize(empresa_id)
9
+ super(method: :POST, uri: "/v1/empresas/#{url_encode(empresa_id)}/desabilitar")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/request'
4
+
5
+ module EnotasApi
6
+ module V1
7
+ class EmitirNfs < EnotasApi::Request
8
+ def initialize(empresa_id, nfs)
9
+ super(method: :POST, uri: "/v1/empresas/#{url_encode(empresa_id)}/nfes", content: nfs.to_json)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,28 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../common/entity'
4
- require_relative 'endereco'
4
+ require_relative 'empresa_endereco'
5
5
  require_relative 'empresa_configuracoes'
6
6
 
7
7
  module EnotasApi
8
- class Empresa < EnotasApi::Entity
9
- attribute :id, :string
10
- attribute :cnpj, :string
11
- attribute :inscricaoMunicipal, :string
12
- attribute :inscricaoEstadual, :string
13
- attribute :razaoSocial, :string
14
- attribute :nomeFantasia, :string
15
- attribute :optanteSimplesNacional, :boolean
16
- attribute :email, :string
17
- attribute :telefoneComercial, :string
18
- attribute :incentivadorCultural, :boolean
19
- attribute :regimeEspecialTributacao, :string
20
- attribute :codigoServicoMunicipal, :string
21
- attribute :cnae, :string
22
- attribute :aliquotaIss, :decimal
23
- attribute :descricaoServico, :string
24
- attribute :endereco, EnotasApi::Endereco
25
- attribute :configuracoesNFSeHomologacao, EnotasApi::EmpresaConfiguracoes
26
- attribute :configuracoesNFSeProducao, EnotasApi::EmpresaConfiguracoes
8
+ module V1
9
+ class Empresa < EnotasApi::Entity
10
+ attribute :id, :string
11
+ attribute :cnpj, :string
12
+ attribute :inscricaoMunicipal, :string
13
+ attribute :inscricaoEstadual, :string
14
+ attribute :razaoSocial, :string
15
+ attribute :nomeFantasia, :string
16
+ attribute :optanteSimplesNacional, :boolean
17
+ attribute :email, :string
18
+ attribute :telefoneComercial, :string
19
+ attribute :incentivadorCultural, :boolean
20
+ attribute :regimeEspecialTributacao, :string
21
+ attribute :codigoServicoMunicipal, :string
22
+ attribute :cnae, :string
23
+ attribute :aliquotaIss, :decimal
24
+ attribute :descricaoServico, :string
25
+ attribute :itemListaServicoLC116, :string
26
+ attribute :endereco, EnotasApi::V1::EmpresaEndereco
27
+ attribute :configuracoesNFSeHomologacao, EnotasApi::V1::EmpresaConfiguracoes
28
+ attribute :configuracoesNFSeProducao, EnotasApi::V1::EmpresaConfiguracoes
29
+ end
27
30
  end
28
31
  end
@@ -3,12 +3,14 @@
3
3
  require_relative '../../common/entity'
4
4
 
5
5
  module EnotasApi
6
- class EmpresaConfiguracoes < EnotasApi::Entity
7
- attribute :sequencialNFe, :integer
8
- attribute :serieNFe, :string
9
- attribute :sequencialLoteNFe, :integer
10
- attribute :usuarioAcessoProvedor, :string
11
- attribute :senhaAcessoProvedor, :string
12
- attribute :tokenAcessoProvedor, :string
6
+ module V1
7
+ class EmpresaConfiguracoes < EnotasApi::Entity
8
+ attribute :sequencialNFe, :integer
9
+ attribute :serieNFe, :string
10
+ attribute :sequencialLoteNFe, :integer
11
+ attribute :usuarioAcessoProvedor, :string
12
+ attribute :senhaAcessoProvedor, :string
13
+ attribute :tokenAcessoProvedor, :string
14
+ end
13
15
  end
14
16
  end