Sha256: 81804b8e9063cbb423a84b5b91db99d8dcd6058e22d87a7b1137fb412ceadaf8

Contents?: true

Size: 1.47 KB

Versions: 11

Compression:

Stored size: 1.47 KB

Contents

module Aws
  module Paging
    class Pager

      def initialize(rules)
        if rules['more_results']
          @more_results = underscore(rules['more_results'])
        end
        if rules['limit_key']
          @limit_key = underscore(rules['limit_key']).to_sym
        end
        map_tokens(rules)
      end

      # @return [Symbol, nil]
      attr_reader :limit_key

      # @param [Seahorse::Client::Response] response
      # @return [Hash]
      def next_tokens(response)
        @tokens.each.with_object({}) do |(source, target), next_tokens|
          value = JMESPath.search(source, response.data)
          next_tokens[target.to_sym] = value unless empty_value?(value)
        end
      end

      # @param [Seahorse::Client::Response] response
      # @return [Boolean]
      def truncated?(response)
        if @more_results
          JMESPath.search(@more_results, response.data)
        else
          !next_tokens(response).empty?
        end
      end

      private

      def map_tokens(rules)
        input = Array(rules['input_token'])
        output = Array(rules['output_token'])
        @tokens = {}
        input.each.with_index do |key, n|
          @tokens[underscore(output[n])] = underscore(key)
        end
      end

      def underscore(str)
        str.
          gsub(' or ', '||').
          gsub(/\w+/) { |part| Seahorse::Util.underscore(part) }
      end

      def empty_value?(value)
        value.nil? || value == [] || value == {}
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
aws-sdk-core-2.0.29 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.28 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.27 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.26 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.25 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.24 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.23 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.22 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.21 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.20 lib/aws-sdk-core/paging/pager.rb
aws-sdk-core-2.0.19 lib/aws-sdk-core/paging/pager.rb