Sha256: df264c30fbfff6038931b0324af1f1562dc122ebf70467868dde7c78b467d023

Contents?: true

Size: 1.77 KB

Versions: 996

Compression:

Stored size: 1.77 KB

Contents

module Aws
  # @api private
  class Pager

    def initialize(rules)
      @more_results = rules['more_results']
      @more_results = underscore(@more_results) if @more_results
      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

    # @api private
    def prev_tokens(response)
      @tokens.each.with_object({}) do |(_, target), tokens|
        value = JMESPath.search(target, response.context.params)
        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 = self.next_tokens(response)
        prev_tokens = self.prev_tokens(response)
        !(next_tokens.empty? || next_tokens == prev_tokens)
      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 == [] || value == {}
    end

  end
end

Version data entries

996 entries across 996 versions & 2 rubygems

Version Path
aws-sdk-core-2.11.592 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.591 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.590 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.589 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.588 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.587 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.586 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.585 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.584 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.583 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.582 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.581 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.580 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.579 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.578 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.577 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.576 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.575 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.574 lib/aws-sdk-core/pager.rb
aws-sdk-core-2.11.573 lib/aws-sdk-core/pager.rb