Sha256: 9e77aa9db503e2ba2f1e9c75fb2b3f6d4c47a7e75e0c9babdf781eb2c9881b21

Contents?: true

Size: 1.15 KB

Versions: 13

Compression:

Stored size: 1.15 KB

Contents

require 'json'

module GoogleContactsApi
  # Base class for GroupSet and ContactSet
  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
    def each
      @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

13 entries across 13 versions & 1 rubygems

Version Path
google_contacts_api-0.4.0 lib/google_contacts_api/result_set.rb
google_contacts_api-0.3.3 lib/google_contacts_api/result_set.rb
google_contacts_api-0.3.2 lib/google_contacts_api/result_set.rb
google_contacts_api-0.3.1 lib/google_contacts_api/result_set.rb
google_contacts_api-0.3.0 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.7 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.6 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.5 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.4 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.3 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.2 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.1 lib/google_contacts_api/result_set.rb
google_contacts_api-0.2.0 lib/google_contacts_api/result_set.rb