Sha256: a289026f5add2cfa23734a386f7a589b21aa880f687b587ff38ba5ceb7aa4950

Contents?: true

Size: 646 Bytes

Versions: 2

Compression:

Stored size: 646 Bytes

Contents

module Vultr
  class Collection
    attr_reader :data, :total, :next_cursor, :prev_cursor

    def self.from_response(response, key:, type:)
      body = response.body
      new(
        data: body[key].map { |attrs| type.new(attrs) },
        total: body.dig("meta", "total"),
        next_cursor: body.dig("meta", "links", "next"),
        prev_cursor: body.dig("meta", "links", "prev")
      )
    end

    def initialize(data:, total:, next_cursor:, prev_cursor:)
      @data = data
      @total = total
      @next_cursor = next_cursor.empty? ? nil : next_cursor
      @prev_cursor = prev_cursor.empty? ? nil : prev_cursor
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vultr-2.0.0 lib/vultr/collection.rb
vultr-1.0.0 lib/vultr/collection.rb