Class: Decidim::ViaOberta::Api::Request

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/via_oberta/api/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_id:, document_type:, organization:) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
# File 'app/services/decidim/via_oberta/api/request.rb', line 14

def initialize(document_id:, document_type:, organization:)
  @document_id = document_id
  @document_type = document_type
  @organization = organization
  @env = TrustedIds.census_authorization[:env]
  @api_url = TrustedIds.census_authorization[:api_url]
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



22
23
24
# File 'app/services/decidim/via_oberta/api/request.rb', line 22

def api_url
  @api_url
end

#document_idObject

Returns the value of attribute document_id.



22
23
24
# File 'app/services/decidim/via_oberta/api/request.rb', line 22

def document_id
  @document_id
end

#document_typeObject

Returns the value of attribute document_type.



22
23
24
# File 'app/services/decidim/via_oberta/api/request.rb', line 22

def document_type
  @document_type
end

#envObject

Returns the value of attribute env.



22
23
24
# File 'app/services/decidim/via_oberta/api/request.rb', line 22

def env
  @env
end

#organizationObject

Returns the value of attribute organization.



22
23
24
# File 'app/services/decidim/via_oberta/api/request.rb', line 22

def organization
  @organization
end

Instance Method Details

#document_type_idObject



82
83
84
# File 'app/services/decidim/via_oberta/api/request.rb', line 82

def document_type_id
  @document_type_id ||= DOCUMENT_TYPE[document_type] || 3
end

#ineObject



58
59
60
# File 'app/services/decidim/via_oberta/api/request.rb', line 58

def ine
  @ine ||= organization.trusted_ids_census_config&.settings&.dig("ine")
end

#municipal_codeObject



70
71
72
# File 'app/services/decidim/via_oberta/api/request.rb', line 70

def municipal_code
  @municipal_code ||= organization.trusted_ids_census_config&.settings&.dig("municipal_code")
end

#nifObject



62
63
64
# File 'app/services/decidim/via_oberta/api/request.rb', line 62

def nif
  @nif ||= organization.trusted_ids_census_config&.settings&.dig("nif")
end

#organization_nameObject



54
55
56
# File 'app/services/decidim/via_oberta/api/request.rb', line 54

def organization_name
  @organization_name ||= organization.trusted_ids_census_config&.settings&.dig("organization_name") || organization.name
end

#province_codeObject



66
67
68
# File 'app/services/decidim/via_oberta/api/request.rb', line 66

def province_code
  @province_code ||= organization.trusted_ids_census_config&.settings&.dig("province_code")
end

#purposeObject



50
51
52
# File 'app/services/decidim/via_oberta/api/request.rb', line 50

def purpose
  @purpose ||= ENV.fetch("VIA_OBERTA_PURPOSE", nil)
end

#request_bodyObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/services/decidim/via_oberta/api/request.rb', line 86

def request_body
  @request_body ||= <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:open="http://www.openuri.org/"
        xmlns:pet="http://gencat.net/scsp/esquemes/peticion">
      <soapenv:Header/>
      <soapenv:Body>
        <open:procesa>
          <ns1:Peticion xmlns:ns1="http://gencat.net/scsp/esquemes/peticion">
            <ns1:Atributos>
              <ns1:IdPeticion>#{request_id}</ns1:IdPeticion>
              <ns1:NumElementos>1</ns1:NumElementos>
              <ns1:TimeStamp>#{time_stamp}</ns1:TimeStamp>
              <ns1:Estado/>
              <ns1:CodigoCertificado>RESIDENT_MUNICIPI</ns1:CodigoCertificado>
              <ns1:CodigoProducto>PADRO</ns1:CodigoProducto>
              <ns1:DatosAutorizacion>
                <ns1:IdentificadorSolicitante>#{ine}</ns1:IdentificadorSolicitante>
                <ns1:NombreSolicitante>#{organization_name}</ns1:NombreSolicitante>
                <ns1:Finalidad>#{purpose}</ns1:Finalidad>
              </ns1:DatosAutorizacion>
              <ns1:Emisor>
                <ns1:NifEmisor>#{nif}</ns1:NifEmisor>
                <ns1:NombreEmisor>#{organization_name}</ns1:NombreEmisor>
              </ns1:Emisor>
              <ns1:IdSolicitanteOriginal>MAP</ns1:IdSolicitanteOriginal>
              <ns1:NomSolicitanteOriginal>MAP</ns1:NomSolicitanteOriginal>
            </ns1:Atributos>
            <ns1:Solicitudes>
              <ns1:SolicitudTransmision>
                <ns1:DatosGenericos>
                  <ns1:Emisor>
                    <ns1:NifEmisor>#{nif}</ns1:NifEmisor>
                    <ns1:NombreEmisor>#{organization_name}</ns1:NombreEmisor>
                  </ns1:Emisor>
                  <ns1:Solicitante>
                    <ns1:IdentificadorSolicitante>#{ine}</ns1:IdentificadorSolicitante>
                    <ns1:NombreSolicitante>#{organization_name}</ns1:NombreSolicitante>
                    <ns1:Finalidad>#{purpose}</ns1:Finalidad>
                    <ns1:Consentimiento>Si</ns1:Consentimiento>
                  </ns1:Solicitante>
                  <ns1:Transmision>
                    <ns1:CodigoCertificado>RESIDENT_MUNICIPI</ns1:CodigoCertificado>
                    <ns1:IdSolicitud>#{request_id}</ns1:IdSolicitud>
                    <ns1:FechaGeneracion>#{time_stamp[0..9]}</ns1:FechaGeneracion>
                  </ns1:Transmision>
                </ns1:DatosGenericos>
                <ns1:DatosEspecificos>
                  <ns2:peticionResidenteMunicipio xmlns:ns2="http://www.aocat.net/padro">
                    <ns2:numExpediente>#{request_id}</ns2:numExpediente>
                    <ns2:tipoDocumentacion>#{document_type_id}</ns2:tipoDocumentacion>
                    <ns2:documentacion>#{document_id}</ns2:documentacion>
                    <ns2:codigoMunicipio>#{municipal_code}</ns2:codigoMunicipio>
                    <ns2:codigoProvincia>#{province_code}</ns2:codigoProvincia>
                    <ns2:idescat>0</ns2:idescat>
                  </ns2:peticionResidenteMunicipio>
                </ns1:DatosEspecificos>
              </ns1:SolicitudTransmision>
            </ns1:Solicitudes>
          </ns1:Peticion>
        </open:procesa>
      </soapenv:Body>
    </soapenv:Envelope>
  XML
end

#request_idObject



78
79
80
# File 'app/services/decidim/via_oberta/api/request.rb', line 78

def request_id
  @request_id ||= SecureRandom.uuid
end

#responseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/decidim/via_oberta/api/request.rb', line 24

def response
  return @response if defined?(@response)

  begin
    response ||= Faraday.post(url) do |request|
      request.headers["Content-Type"] = "text/xml;charset=UTF-8"
      request.headers["SOAPAction"] = "procesa"
      request.body = request_body
    end
  rescue Faraday::Error => e
    Rails.logger.error "WEBSERVICE CONNECTION ERROR: #{e.message}"
    raise e
  end
  @response ||= Response.new(response)
end

#time_stampObject



74
75
76
# File 'app/services/decidim/via_oberta/api/request.rb', line 74

def time_stamp
  @time_stamp ||= Time.current.strftime("%Y-%m-%dT%H:%M:%S")
end

#urlObject



40
41
42
43
44
45
46
47
48
# File 'app/services/decidim/via_oberta/api/request.rb', line 40

def url
  @url ||= if @api_url.present?
             @api_url
           elsif @env == "production"
             "https://serveis3.iop.aoc.cat/siri-proxy/services/Sincron?wsdl"
           else
             "https://serveis3-pre.iop.aoc.cat/siri-proxy/services/Sincron?wsdl"
           end
end