1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
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
|