siba_api 0.1.0 → 0.1.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: 7bbba86823deead2120faa71d5277a25da5a1e4ca99b9b1c60f25fb7fda70678
4
- data.tar.gz: cc0f1f091f4deae42e93d167b56e6704f4df42caf5bbf91933f0e50e8afd75a3
3
+ metadata.gz: 0407b762bb94232363d947d76afaead1441da284c7b7e1aab71a8d9cb4322604
4
+ data.tar.gz: ca5290b3e621ada85990432ccb2544ee5d2548a9369e4e0d1ddf1368ccb89603
5
5
  SHA512:
6
- metadata.gz: 1f452bf2a48e5b6af0cd9b1d7abea1633d495350b8063a19a48c24be246f9fc6ee13ea9cb05f0a34b01784e8bc7cf830696c9879c169bf6723ff18101cc822da
7
- data.tar.gz: fe4feceba7c8f50a01582aecc811b9ae7bbc080425dfad1b9005023ee31f97e16e03af17e8304da0089f83f3cc60c70d57aeadd789f0c6894a266eec472d3781
6
+ metadata.gz: 551cb36918ce40eaf0f8cfa53cd1c874049f3ce68115f051366d2ad667ec73df669457eff3900f696b9fad2deaaae3b8deab5a4ac744313ec31ca0305ed549c6
7
+ data.tar.gz: a6d141af352dfc63ca5b73955ff68746244387581a807262e0736b535762505aba82efb4cb9637e1d4b97406645396f8074f11efc18dfdff99606d1a226fbac9
data/.irbrc CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'irb'
2
4
  require 'irb/completion'
3
5
  require 'rubygems'
data/.rubocop.yml CHANGED
@@ -12,3 +12,10 @@ Layout/LineLength:
12
12
  AllCops:
13
13
  TargetRubyVersion: 2.4.0
14
14
  NewCops: enable
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - 'Rakefile'
19
+ - '**/*.rake'
20
+ - 'test/**/*.rb'
21
+ - 'spec/**/*.rb'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # SIBA API Wrapper
2
2
 
3
+ ![main workflow](https://github.com/dlage/siba_api/actions/workflows/main.yml/badge.svg)
4
+
3
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
4
6
  into a gem. Put your Ruby code in the file `lib/siba_api`. To experiment with that code, run `bin/console` for an
5
7
  interactive prompt.
data/lib/siba_api/api.rb CHANGED
@@ -80,28 +80,18 @@ module SIBAApi
80
80
  response = client.call(operation.to_sym, message: default_params.merge(params))
81
81
  self.last_response = response
82
82
 
83
+ process_operation_response(operation, response)
84
+ end
85
+
86
+ def process_operation_response(operation, response)
83
87
  if response_successful?(response)
84
88
  result = response.body["#{operation}_response".to_sym]["#{operation}_result".to_sym]
85
89
  return response if result == '0'
86
-
87
- parsed_response = parse_response(result)
88
- raise error_class(response.http.code), "Code: #{parsed_response[:codigo_retorno]}, response: #{response.body}, description: #{parsed_response[:descricao]}"
89
90
  end
90
91
 
91
92
  raise error_class(response.http.code), "Code: #{response.http.code}, response: #{response.body}"
92
93
  end
93
94
 
94
- # Error:
95
- # {:erros_ba=>
96
- # {:retorno_ba=>
97
- # {:linha=>"0",
98
- # :codigo_retorno=>"75",
99
- # :descricao=>
100
- # "Linha XML 6. -->The element 'Unidade_Hoteleira' in namespace 'http://sef.pt/BAws' has incomplete content. List of possible elements expected: 'Abreviatura' in namespace 'http://sef.pt/BAws'."},
101
- # :@xmlns=>"http://www.sef.pt/BAws"}}
102
- #
103
- # Success:
104
- #
105
95
  def parse_response(result)
106
96
  inner_response = Nori.new(convert_tags_to: ->(tag) { tag.snakecase.to_sym }).parse(
107
97
  result
@@ -130,7 +120,7 @@ module SIBAApi
130
120
  # :nodoc:
131
121
  case method_name.to_s
132
122
  when /^(.*)\?$/
133
- !!send(Regexp.last_match(1).to_s)
123
+ !send(Regexp.last_match(1).to_s).nil?
134
124
  when /^clear_(.*)$/
135
125
  send("#{Regexp.last_match(1)}=", nil)
136
126
  else
@@ -46,21 +46,7 @@ module SIBAApi
46
46
  # 'Data_Saida' => '20220831',
47
47
  # 'Local_Residencia_Origem' => 'Place of Residence',
48
48
  def deliver_bulletins(file_number, bulletins = [], _global_params = {})
49
- logger = Logger.new $stderr
50
- logger.level = Logger::DEBUG
51
- bulletins_xml = Gyoku.xml(
52
- {
53
- 'MovimentoBAL' => {
54
- 'Unidade_Hoteleira' => build_hotel_unit,
55
- 'Boletim_Alojamento' => build_bulletins(bulletins),
56
- 'Envio' => build_control_data(file_number),
57
- :@xmlns => 'http://sef.pt/BAws'
58
- }
59
- },
60
- pretty_print: true
61
- )
62
- logger.debug(bulletins_xml)
63
- bulletins_encoded = Base64.encode64(bulletins_xml)
49
+ bulletins_encoded = build_encoded_bulletins_xml(file_number, bulletins)
64
50
  response = request(
65
51
  operation: :entrega_boletins_alojamento,
66
52
  params: {
@@ -72,6 +58,19 @@ module SIBAApi
72
58
 
73
59
  protected
74
60
 
61
+ def build_encoded_bulletins_xml(file_number, bulletins)
62
+ bulletins_xml = Gyoku.xml(
63
+ {
64
+ 'MovimentoBAL' => {
65
+ 'Unidade_Hoteleira' => build_hotel_unit, 'Boletim_Alojamento' => build_bulletins(bulletins),
66
+ 'Envio' => build_control_data(file_number), :@xmlns => 'http://sef.pt/BAws'
67
+ }
68
+ },
69
+ pretty_print: true
70
+ )
71
+ Base64.encode64(bulletins_xml)
72
+ end
73
+
75
74
  # <Unidade_Hoteleira>
76
75
  # <Codigo_Unidade_Hoteleira>121212121</Codigo_Unidade_Hoteleira>
77
76
  # <Estabelecimento>00</Estabelecimento>
@@ -93,27 +92,11 @@ module SIBAApi
93
92
  # <Numero_Ficheiro>97</Numero_Ficheiro>
94
93
  # <Data_Movimento>2008-05-20T00:00:00</Data_Movimento>
95
94
  def build_control_data(file_number)
96
- {
97
- 'Numero_Ficheiro' => file_number,
98
- 'Data_Movimento' => DateTime.now.strftime('%FT%T')
99
- }
95
+ { 'Numero_Ficheiro' => file_number, 'Data_Movimento' => DateTime.now.strftime('%FT%T') }
100
96
  end
101
97
 
102
98
  def build_bulletins(bulletins = [])
103
- translation_hash = {
104
- surname: 'Apelido',
105
- name: 'Nome',
106
- nationality: 'Nacionalidade',
107
- birthdate: 'Data_Nascimento',
108
- place_of_birth: 'Local_Nascimento',
109
- id_document: 'Documento_Identificacao',
110
- document_country: 'Pais_Emissor_Documento',
111
- document_type: 'Tipo_Documento',
112
- start_date: 'Data_Entrada',
113
- end_date: 'Data_Saida',
114
- origin_country: 'Pais_Residencia_Origem',
115
- origin_place: 'Local_Residencia_Origem'
116
- }
99
+ translation_hash = bulletin_translation_hash
117
100
  translated_bulletins = []
118
101
  bulletins.each do |b|
119
102
  bt = {}
@@ -125,18 +108,25 @@ module SIBAApi
125
108
  translated_bulletins
126
109
  end
127
110
 
111
+ def bulletin_translation_hash
112
+ {
113
+ surname: 'Apelido', name: 'Nome', nationality: 'Nacionalidade',
114
+ birthdate: 'Data_Nascimento', place_of_birth: 'Local_Nascimento',
115
+ id_document: 'Documento_Identificacao', document_country: 'Pais_Emissor_Documento',
116
+ document_type: 'Tipo_Documento',
117
+ start_date: 'Data_Entrada', end_date: 'Data_Saida',
118
+ origin_country: 'Pais_Residencia_Origem', origin_place: 'Local_Residencia_Origem'
119
+ }
120
+ end
121
+
128
122
  def process_response(response)
129
123
  result = response
130
124
  case result
131
125
  when Hash
132
126
  result.transform_keys!(&:to_sym)
133
- result.each_value do |r|
134
- process_response(r)
135
- end
127
+ result.each_value { |r| process_response(r) }
136
128
  when Array
137
- result.each do |r|
138
- process_response(r)
139
- end
129
+ result.each { |r| process_response(r) }
140
130
  end
141
131
  result
142
132
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SIBAApi
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siba_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-23 00:00:00.000000000 Z
11
+ date: 2022-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gyoku