Sha256: 3d200ee967d8c09114618c799ec6ccde327084b371c67e808311a1a9c3b5f8d7

Contents?: true

Size: 1.94 KB

Versions: 90

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Aws
  module Endpoints
    # Does substitutions for templated endpoint strings

    # This class is deprecated. It is used by the Runtime endpoint
    # resolution approach. It has been replaced by a code generated
    # approach in each service gem. It can be removed in a new
    # major version. It has to exist because
    # old service gems can use a new core version.
    # @api private
    module Templater
      class << self
        def resolve(string, parameters, assigns)
          # scans for strings in curly brackets {}
          string.scan(/\{.+?\}/).each do |capture|
            value = capture[1..-2] # strips curly brackets
            string = string.gsub(capture, replace(value, parameters, assigns))
          end
          string
        end

        private

        # Replaces the captured value with values from parameters or assign
        def replace(capture, parameters, assigns)
          # Pound sigil is used for getAttr calls
          indexes = capture.split('#')

          # no sigil found, just do substitution
          if indexes.size == 1
            extract_value(capture, parameters, assigns)
          # sigil was found, need to call getAttr
          elsif indexes.size == 2
            ref, property = indexes
            param = extract_value(ref, parameters, assigns)
            Matchers.attr(param, property)
          else
            raise "Invalid templatable value: #{capture}"
          end
        end

        # Checks both parameters and assigns hash for the referenced value
        def extract_value(key, parameters, assigns)
          if assigns.key?(key)
            assigns[key]
          elsif parameters.class.singleton_class::PARAM_MAP.key?(key)
            member_name = parameters.class.singleton_class::PARAM_MAP[key]
            parameters[member_name]
          else
            raise "Templatable value not found: #{key}"
          end
        end
      end
    end
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
aws-sdk-core-3.220.2 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.220.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.220.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.219.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.218.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.218.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.217.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.217.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.216.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.216.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.215.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.215.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.214.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.214.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.213.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.212.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.211.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.210.0 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.209.1 lib/aws-sdk-core/endpoints/templater.rb
aws-sdk-core-3.209.0 lib/aws-sdk-core/endpoints/templater.rb