Sha256: 4ab0214a5e2a01cebb7a48e29ac743bbb6c68820a558a6fface2f56ad21b0249

Contents?: true

Size: 1.62 KB

Versions: 14

Compression:

Stored size: 1.62 KB

Contents

module VagrantCloud
  class Response
    class Search < Response
      # @return [Account]
      attr_reader :account
      # @return [Hash] search parameters
      attr_reader :search_parameters

      attr_optional :boxes

      def initialize(account:, params:, **opts)
        if !account.is_a?(Account)
          raise TypeError,
            "Expected type `#{Account.name}` but received `#{account.class.name}`"
        end
        @account = account
        @search_parameters = params
        opts[:boxes] = reload_boxes(opts[:boxes])
        super(**opts)
      end

      # @return [Integer]
      def page
        pg = @search_parameters.fetch(:page, 0).to_i
        pg > 0 ? pg : 1
      end

      # @return [Search] previous page of search results
      def previous
        if page <= 1
          raise ArgumentError,
            "Cannot request page results less than one"
        end
        account.searcher.from_response(self) do |s|
          s.prev_page
        end
      end

      # @return [Search] next page of search results
      def next
        account.searcher.from_response(self) do |s|
          s.next_page
        end
      end

      protected

      # Load all the box data into proper instances
      def reload_boxes(boxes)
        org_cache = {}
        boxes.map do |b|
          org_name = b[:username]
          if !org_cache[org_name]
            org_cache[org_name] = account.organization(name: org_name)
          end
          org = org_cache[org_name]
          box = Box.new(organization: org, **b)
          org.boxes = org.boxes + [box]
          org.clean!
          box
        end
      end
    end
  end
end

Version data entries

14 entries across 13 versions & 2 rubygems

Version Path
vagrant_cloud-3.1.2 lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.1.1 lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.1.0 lib/vagrant_cloud/response/search.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.5/lib/vagrant_cloud/response/search.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.5/lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.5 lib/vagrant_cloud/response/search.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/vagrant_cloud-3.0.2/lib/vagrant_cloud/response/search.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/vagrant_cloud-3.0.4/lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.4 lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.3 lib/vagrant_cloud/response/search.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/vagrant_cloud-3.0.2/lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.2 lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.1 lib/vagrant_cloud/response/search.rb
vagrant_cloud-3.0.0 lib/vagrant_cloud/response/search.rb