Sha256: dfba73c56f06efc92acb35014c4bb60eb10552abb182b5ce13e0cfd318c26067

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require 'securerandom'

module Yoti
  module DynamicSharingService
    class Share
      attr_reader :share_url, :ref_id

      def initialize(data)
        @share_url = data['qrcode']
        @ref_id = data['ref_id']
      end
    end

    def self.create_share_url(scenario)
      yoti_request = Yoti::Request
                     .builder
                     .with_http_method('POST')
                     .with_base_url(Yoti.configuration.api_endpoint)
                     .with_endpoint("qrcodes/apps/#{Yoti.configuration.client_sdk_id}")
                     .with_query_param('appId', Yoti.configuration.client_sdk_id)
                     .with_payload(scenario)
                     .build

      begin
        create_share_url_parse_response yoti_request.execute
      rescue Yoti::RequestError => e
        raise if e.response.nil?

        case e.response.code
        when '400'
          raise InvalidDataError
        when '404'
          raise ApplicationNotFoundError
        else
          raise UnknownHTTPError, e.response.code
        end
      end
    end

    def self.create_share_url_parse_response(response)
      Share.new JSON.parse response.body
    end

    class InvalidDataError < StandardError
      def initialize(msg = 'JSON is incorrect, contains invalid data')
        super
      end
    end

    class ApplicationNotFoundError < StandardError
      def initialize(msg = 'Application was not found')
        super
      end
    end

    class UnknownHTTPError < StandardError
      def initialize(code = nil, msg = 'Unknown HTTP Error')
        msg = "#{msg}: #{code}" unless code.nil?
        super msg
      end
    end

    # @deprecated no longer used - will be removed in 2.0.0
    def self.create_share_url_query
      "?nonce=#{SecureRandom.uuid}&timestamp=#{Time.now.to_i}"
    end

    # @deprecated no longer used - will be removed in 2.0.0
    def self.create_share_url_endpoint
      "/qrcodes/apps/#{Yoti.configuration.client_sdk_id}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yoti-1.10.0 lib/yoti/dynamic_share_service/share_url.rb
yoti-1.9.0 lib/yoti/dynamic_share_service/share_url.rb
yoti-1.8.0 lib/yoti/dynamic_share_service/share_url.rb
yoti-1.7.1 lib/yoti/dynamic_share_service/share_url.rb
yoti-1.7.0 lib/yoti/dynamic_share_service/share_url.rb