Class: GoogleContactsApi::ResultSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google_contacts_api/result_set.rb

Overview

Base class for GroupSet and ContactSet that generically represents a set of results.

Direct Known Subclasses

ContactSet, GroupSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_body, api = nil) ⇒ ResultSet

Initialize a new ResultSet from the response, with the given GoogleContacts::Api object if specified.



13
14
15
16
17
18
19
20
# File 'lib/google_contacts_api/result_set.rb', line 13

def initialize(response_body, api = nil)
  @api = api
  @parsed = Hashie::Mash.new(JSON.parse(response_body))
  @total_results = @parsed.feed["openSearch$totalResults"]["$t"].to_i
  @start_index = @parsed.feed["openSearch$startIndex"]["$t"].to_i
  @items_per_page = @parsed.feed["openSearch$itemsPerPage"]["$t"].to_i
  @results = []
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



8
9
10
# File 'lib/google_contacts_api/result_set.rb', line 8

def api
  @api
end

#items_per_pageObject

Returns the value of attribute items_per_page.



9
10
11
# File 'lib/google_contacts_api/result_set.rb', line 9

def items_per_page
  @items_per_page
end

#parsedObject

Returns the value of attribute parsed.



9
10
11
# File 'lib/google_contacts_api/result_set.rb', line 9

def parsed
  @parsed
end

#start_indexObject

Returns the value of attribute start_index.



9
10
11
# File 'lib/google_contacts_api/result_set.rb', line 9

def start_index
  @start_index
end

#total_resultsObject

Returns the value of attribute total_results.



9
10
11
# File 'lib/google_contacts_api/result_set.rb', line 9

def total_results
  @total_results
end

Instance Method Details

#eachObject

Yields to block for each result. Returns an Enumerator if no block is passed.



24
25
26
27
# File 'lib/google_contacts_api/result_set.rb', line 24

def each
  return to_enum(:each) unless block_given?
  @results.each { |x| yield x }
end

#has_more?Boolean

Return true if there are more results with the same parameters you used

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/google_contacts_api/result_set.rb', line 31

def has_more?
  # 1-based indexing
  @start_index - 1 + @items_per_page <= @total_results
end

#inspectObject

:nodoc:



36
37
38
# File 'lib/google_contacts_api/result_set.rb', line 36

def inspect #:nodoc:
  "<#{self.class}: @start_index=#{@start_index}, @items_per_page=#{@items_per_page}, @total_results=#{@total_results}>"
end