class VagrantCloud::Search

Attributes

account[R]

@return [Account]

Public Class Methods

new(access_token: nil, account: nil, client: nil) click to toggle source

Create a new search instance

@param [String] access_token Authentication token @param [Account] account Account instance @param [Client] client Client instance @return [Search]

# File lib/vagrant_cloud/search.rb, line 12
def initialize(access_token: nil, account: nil, client: nil)
  args = {access_token: access_token, account: account, client: client}.compact
  if args.size > 1
    raise ArgumentError,
      "Search accepts `access_token`, `account`, or `client` but received multiple (#{args.keys.join(", ")})"
  end
  if client
    if !client.is_a?(Client)
      raise TypeError,
        "Expecting type `#{Client.name}` but received `#{client.class.name}`"
    end
    @account = Account.new(client: client)
  elsif account
    if !account.is_a?(Account)
      raise TypeError,
        "Expecting type `#{Account.name}` but received `#{account.class.name}`"
    end
    @account = account
  else
    @account = Account.new(access_token: access_token)
  end
  @params = {}
  @lock = Mutex.new
end

Public Instance Methods

active?() click to toggle source

@return [Boolean] Search terms are stored

# File lib/vagrant_cloud/search.rb, line 90
def active?
  !@params.empty?
end
clear!() click to toggle source

Clear the currently cached search parameters

@return [self]

# File lib/vagrant_cloud/search.rb, line 97
def clear!
  @lock.synchronize { @params.clear }
  self
end
from_response(response) { |seed(**search_parameters)| ... } click to toggle source

Generate a new instance seeded with search parameters from given response

@param [Response::Search] response Search response @yieldparam [Search] Seeded search instance @return [Object] result of given block

# File lib/vagrant_cloud/search.rb, line 116
def from_response(response)
  s = self.class.new(account: account)
  yield s.seed(**response.search_parameters)
end
next_page() click to toggle source

Request the next page of the search results

@param [Response::Search]

# File lib/vagrant_cloud/search.rb, line 63
def next_page
  @lock.synchronize do
    if @params.empty?
      raise ArgumentError, "No active search currently cached"
    end
    page = @params[:page].to_i
    page = 1 if page < 1
    @params[:page] = page + 1
    execute
  end
end
prev_page() click to toggle source

Request the previous page of the search results

@param [Response::Search]

# File lib/vagrant_cloud/search.rb, line 78
def prev_page
  @lock.synchronize do
    if @params.empty?
      raise ArgumentError, "No active search currently cached"
    end
    page = @params[:page].to_i - 1
    @params[:page] = page < 1 ? 1 : page
    execute
  end
end
seed(**params) click to toggle source

Seed the parameters

@return [self]

# File lib/vagrant_cloud/search.rb, line 105
def seed(**params)
  @lock.synchronize { @params = params }
  self
end

Protected Instance Methods

execute() click to toggle source

@return [Response::Search]

# File lib/vagrant_cloud/search.rb, line 124
def execute
  r = account.client.search(**@params)
  Response::Search.new(account: account, params: @params, **r)
end