Sha256: 8809f31f51b51a448e7c76ecda8085c088c192854f5ade0f6df73d27996601ff

Contents?: true

Size: 953 Bytes

Versions: 7

Compression:

Stored size: 953 Bytes

Contents

module Fog
  module Compute
    class DigitalOcean
      class PagingCollection < Fog::Collection

        attribute :next
        attribute :last

        def next_page
          all(page: @next) if @next != @last
        end

        def previous_page
          if @next.to_i > 2
            all(page: @next.to_i - 2)
          end
        end

        private

        def deep_fetch(hash, *path)
          path.inject(hash) do |acc, key|
            acc.respond_to?(:keys) ? acc[key] : nil
          end
        end

        def get_page(link)
          if match = link.match(/page=(?<page>\d+)/)
            match.captures.last
          end
        end

        def get_paged_links(links)
          next_link = deep_fetch(links, "pages", "next").to_s
          last_link = deep_fetch(links, "pages", "last").to_s
          @next = get_page(next_link) || @next
          @last = get_page(last_link) || @last
        end
      end
    end
  end
end

Version data entries

7 entries across 5 versions & 2 rubygems

Version Path
fog-digitalocean-0.4.0 lib/fog/compute/digitalocean/models/paging_collection.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-digitalocean-0.3.0/lib/fog/digitalocean/models/paging_collection.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-digitalocean-0.3.0/lib/fog/digitalocean/models/paging_collection.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-digitalocean-0.3.0/lib/fog/digitalocean/models/paging_collection.rb
fog-digitalocean-0.3.0 lib/fog/digitalocean/models/paging_collection.rb
fog-digitalocean-0.2.0 lib/fog/digitalocean/models/paging_collection.rb
fog-digitalocean-0.1.0 lib/fog/digitalocean/models/paging_collection.rb