Class: Teambox::ResultSet

Inherits:
Object
  • Object
show all
Includes:
ReferenceList
Defined in:
lib/teambox-client/result_set.rb

Overview

This represents a list of objects along with referenced objects returned by the API.

Basic Usage

A Teambox::ResultSet is usually returned from a get request from Teambox::Client. e.g.:

list = client.get('/projects') # Teambox::ResultSet
puts "Returned #{list.length} projects"

The Teambox API only returns a limited amount of objects per request (currently 50), so the methods prev, next, and empty? are provided to navigate the list of objects. e.g. To get all projects, you can use something like:

project_list = []
list = client.get('/projects')
while !list.empty?
  items += list.objects
  list = list.prev
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReferenceList

#generate_references, #get_or_make_reference, #get_or_make_references, #get_reference, #set_reference

Constructor Details

#initialize(client, request, objects, references) ⇒ ResultSet

:nodoc:



26
27
28
29
30
31
32
33
34
35
# File 'lib/teambox-client/result_set.rb', line 26

def initialize(client, request, objects, references) #:nodoc:
  @client = client
  @request = request
  generate_references(references)
  @objects = objects.map { |o| Teambox.create_model(o['type'], o, self) }
  
  id_list = objects.map{|obj| obj['id']}.sort
  @first_id = id_list[0]
  @last_id = id_list[-1]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/teambox-client/result_set.rb', line 24

def client
  @client
end

#first_idObject (readonly)

Returns the value of attribute first_id.



24
25
26
# File 'lib/teambox-client/result_set.rb', line 24

def first_id
  @first_id
end

#last_idObject (readonly)

Returns the value of attribute last_id.



24
25
26
# File 'lib/teambox-client/result_set.rb', line 24

def last_id
  @last_id
end

#objectsObject (readonly)

Returns the value of attribute objects.



24
25
26
# File 'lib/teambox-client/result_set.rb', line 24

def objects
  @objects
end

#referencesObject (readonly)

Returns the value of attribute references.



24
25
26
# File 'lib/teambox-client/result_set.rb', line 24

def references
  @references
end

Instance Method Details

#[](idx) ⇒ Object

Indexes into object array



68
69
70
# File 'lib/teambox-client/result_set.rb', line 68

def [](idx)
  @objects[idx]
end

#each(&block) ⇒ Object

Yields for each object



38
39
40
# File 'lib/teambox-client/result_set.rb', line 38

def each(&block)
  @objects.each(&block)
end

#empty?Boolean

Is the object list empty?

Returns:

  • (Boolean)


53
54
55
# File 'lib/teambox-client/result_set.rb', line 53

def empty?
  @objects.length == 0
end

#lengthObject

Length of object list



48
49
50
# File 'lib/teambox-client/result_set.rb', line 48

def length
  @objects.length
end

#map(&block) ⇒ Object

Yields for each object



43
44
45
# File 'lib/teambox-client/result_set.rb', line 43

def map(&block)
  @objects.map(&block)
end

#nextObject

Gets newer items as a Teambox::ResultSet



63
64
65
# File 'lib/teambox-client/result_set.rb', line 63

def next
  @client.get(@request[:url], {}, :query => (@request[:query]).merge(:since_id => @last_id))
end

#prevObject

Gets older items as a Teambox::ResultSet



58
59
60
# File 'lib/teambox-client/result_set.rb', line 58

def prev
  @client.get(@request[:url], {}, :query => (@request[:query]).merge(:max_id => @first_id))
end