Sha256: f3f4f022721b71557f5c09f7a6cb1cb497e86f5a1c0f5afc7acd3c607d9e6dfc

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# Custom array class we use in order to track extra attributes
# on a collection of results.
module Azure
  module Armrest
    class ArmrestCollection < Array
      attr_accessor :continuation_token
      attr_accessor :response_headers

      alias skip_token continuation_token
      alias skip_token= continuation_token=

      class << self
        # Creates and returns a ArmrestCollection object based on JSON input,
        # using +klass+ to generate the list elements. In addition, both the
        # response headers and continuation token are set.
        #
        def create_from_response(response, klass = nil)
          json_response = JSON.parse(response)
          array = new(json_response['value'].map { |hash| klass.new(hash) })

          array.response_headers = response.headers
          array.continuation_token = parse_skip_token(json_response)

          array
        end

        private

        # Parse the skip token value out of the nextLink attribute from a response.
        def parse_skip_token(json)
          return nil unless json['nextLink']
          json['nextLink'][/.*?skipToken=(.*?)$/i, 1]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
azure-armrest-0.3.7 lib/azure/armrest/armrest_collection.rb
azure-armrest-0.3.6 lib/azure/armrest/armrest_collection.rb