Class: Gcloud::Search::Document::List

Inherits:
Array
  • Object
show all
Defined in:
lib/gcloud/search/document/list.rb

Overview

Document::List is a special case Array with additional values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = []) ⇒ List

Create a new Document::List with an array of Document instances.



29
30
31
# File 'lib/gcloud/search/document/list.rb', line 29

def initialize arr = []
  super arr
end

Instance Attribute Details

#tokenObject

If not empty, indicates that there are more records that match the request and this value should be passed to continue.



25
26
27
# File 'lib/gcloud/search/document/list.rb', line 25

def token
  @token
end

Class Method Details

.from_response(resp, index) ⇒ Object

New Documents::List from a response object.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gcloud/search/document/list.rb', line 61

def self.from_response resp, index #:nodoc:
  data = JSON.parse resp.body
  documents = new(Array(data["documents"]).map do |doc_hash|
    Document.from_hash doc_hash
  end)
  documents.instance_eval do
    @token = data["nextPageToken"]
    @index = index
  end
  documents
rescue JSON::ParserError
  raise ApiError.from_response(resp)
end

Instance Method Details

#allObject

Retrieves all documents by repeatedly loading pages until #next? returns false. Returns the list instance for method chaining.



50
51
52
53
54
55
56
57
# File 'lib/gcloud/search/document/list.rb', line 50

def all
  while next?
    next_documents = self.next
    push(*next_documents)
    self.token = next_documents.token
  end
  self
end

#nextObject

Retrieve the next page of documents.



41
42
43
44
45
# File 'lib/gcloud/search/document/list.rb', line 41

def next
  return nil unless next?
  ensure_index!
  @index.documents token: token
end

#next?Boolean

Whether there a next page of documents.

Returns:

  • (Boolean)


35
36
37
# File 'lib/gcloud/search/document/list.rb', line 35

def next?
  !token.nil?
end