Sha256: 3c0faf95bccb2d53e8ef61a4258eb5354a90a4b64c39ed5ec2d555a1ff2feafd

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'openssl'
require 'net/http'

module CzechPostB2bClient
  module Services
    class XsdsDownloader < ApiCaller
      def initialize(_anything); end

      def steps
        %i[download_xsds]
      end

      private

      attr_reader :xsd_uri

      def download_xsds
        xsd_uris = configuration.namespaces.values
        xsd_uris.each do |uri|
          # check for  `.xsd` at end?
          @xsd_uri = URI.parse(uri)
          download
        end
      end

      def download # rubocop:disable Metrics/AbcSize
        self.response = https_conn.request(request)

        debug_msg = "CzechPost XSD REQUEST: #{request} to #{xsd_uri} with body:\n#{request.body} => #{response.code}"
        CzechPostB2bClient.logger.debug(debug_msg)

        raise "Error in downloading #{xsd_uri} => #{response}" unless response.code.to_i == 200

        save_response_to_file
      rescue *KNOWN_CONNECTION_ERRORS => e
        raise "Error in downloading #{xsd_uri} => #{e.message}"
      end

      def https_conn
        Net::HTTP.start(xsd_uri.host, xsd_uri.port, connection_options)
      end

      def request
        Net::HTTP::Get.new xsd_uri.request_uri, headers
      end

      def save_response_to_file
        filename = xsd_uri.request_uri.split('/').last
        filename += '.xsd' if filename.split('.').last.downcase != 'xsd'

        File.write(filename, response.body)
      end

      def headers
        {}
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
czech_post_b2b_client-1.2.7 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.6 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.5 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.4 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.3 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.2 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.2.1 lib/czech_post_b2b_client/services/xsds_downloader.rb
czech_post_b2b_client-1.1.0 lib/czech_post_b2b_client/services/xsds_downloader.rb