Sha256: b846e0435017f8d0a590a630c9f1b22fcb234ddb34eed3791939767fe74b30ba

Contents?: true

Size: 1.3 KB

Versions: 6

Compression:

Stored size: 1.3 KB

Contents

require 'json'

module GoogleContactsApi
  # Base class for GroupSet and ContactSet that generically represents
  # a set of results.
  class ResultSet
    include Enumerable
    attr_reader :api
    attr_accessor :total_results, :start_index, :items_per_page, :parsed
    
    # Initialize a new ResultSet from the response, with the given
    # GoogleContacts::Api object if specified.
    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
    
    # Yields to block for each result.
    # Returns an Enumerator if no block is passed.
    def each
      return to_enum(:each) unless block_given?
      @results.each { |x| yield x }
    end
    
    # Return true if there are more results with the same
    # parameters you used
    def has_more?
      # 1-based indexing
      @start_index - 1 + @items_per_page <= @total_results
    end
    
    def inspect #:nodoc:
      "<#{self.class}: @start_index=#{@start_index}, @items_per_page=#{@items_per_page}, @total_results=#{@total_results}>"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
google_contacts_api-0.7.1 lib/google_contacts_api/result_set.rb
google_contacts_api-0.7.0 lib/google_contacts_api/result_set.rb
google_contacts_api-0.6.0 lib/google_contacts_api/result_set.rb
google_contacts_api-0.5.1 lib/google_contacts_api/result_set.rb
google_contacts_api-0.5.0 lib/google_contacts_api/result_set.rb
google_contacts_api-0.4.2 lib/google_contacts_api/result_set.rb