# frozen_string_literal: true require 'logger' require 'base64' require_relative 'api' # Implementation of available methods for SIBA API module SIBAApi # Main client class that implements communication with the API # global_params: # - include_related_objects: int 0-1 0 # - page: int positive 1 # - per_page: int positive 20 class Client < API # SIBAApi::Client.new.calendar(38859, '2022-01-01', '2022-07-31') # ? # ? # # ? # # ? # Boletins: # Apelido # Teste # DZA # 2000-01-01 # # # # # # # # # # 'Apelido' => 'Surname', # 'Nome' => 'Name', # 'Nacionalidade' => 'VEN', # 'Data_Nascimento' => '19990101', # 'Local_Nascimento' => 'Place of Birth', # 'Documento_Identificacao' => '123456789', # 'Pais_Emissor_Documento' => 'YEM', # 'Tipo_Documento' => 'P', # 'Pais_Residencia_Origem' => 'ZMB', # 'Data_Entrada' => '20220801', # 'Data_Saida' => '20220831', # 'Local_Residencia_Origem' => 'Place of Residence', def deliver_bulletins(file_number, bulletins = [], _global_params = {}) logger = Logger.new $stderr logger.level = Logger::DEBUG bulletins_xml = Gyoku.xml( { 'MovimentoBAL' => { 'Unidade_Hoteleira' => build_hotel_unit, 'Boletim_Alojamento' => build_bulletins(bulletins), 'Envio' => build_control_data(file_number), :@xmlns => 'http://sef.pt/BAws' } }, pretty_print: true ) logger.debug(bulletins_xml) bulletins_encoded = Base64.encode64(bulletins_xml) response = request( operation: :entrega_boletins_alojamento, params: { Boletins: bulletins_encoded } ) process_response(response) end protected # # 121212121 # 00 # Hotel teste # teste # Rua da Alegria, 172 # Portalegre # 1000 # 234 # 214017744 # 214017766 # Nuno teste # teste.teste@sef.pt # def build_hotel_unit @hotel_unit_info end # 97 # 2008-05-20T00:00:00 def build_control_data(file_number) { 'Numero_Ficheiro' => file_number, 'Data_Movimento' => DateTime.now.strftime('%FT%T') } end def build_bulletins(bulletins = []) translation_hash = { surname: 'Apelido', name: 'Nome', nationality: 'Nacionalidade', birthdate: 'Data_Nascimento', place_of_birth: 'Local_Nascimento', id_document: 'Documento_Identificacao', document_country: 'Pais_Emissor_Documento', document_type: 'Tipo_Documento', start_date: 'Data_Entrada', end_date: 'Data_Saida', origin_country: 'Pais_Residencia_Origem', origin_place: 'Local_Residencia_Origem' } translated_bulletins = [] bulletins.each do |b| bt = {} translation_hash.each_key do |k| bt[translation_hash[k]] = b[k] end translated_bulletins.push(bt) end translated_bulletins end def process_response(response) result = response case result when Hash result.transform_keys!(&:to_sym) result.each_value do |r| process_response(r) end when Array result.each do |r| process_response(r) end end result end end end