Module: Springboard::Client::Collection

Includes:
Enumerable
Included in:
Resource
Defined in:
lib/springboard/client/collection.rb

Overview

Mixin provides Resource with special methods for convenient interaction with collection resources.

Instance Method Summary collapse

Instance Method Details

#countInteger

Performs a request and returns the number of resources in the collection.

Raises:


33
34
35
# File 'lib/springboard/client/collection.rb', line 33

def count
  call_client(:count)
end

#each(&block) ⇒ Object

Iterates over all results from the collection and yields each one to the block, fetching pages as needed.

Raises:


14
15
16
# File 'lib/springboard/client/collection.rb', line 14

def each(&block)
  call_client(:each, &block)
end

#each_page(&block) ⇒ Object

Iterates over each page of results and yields the page to the block, fetching as needed.

Raises:


23
24
25
# File 'lib/springboard/client/collection.rb', line 23

def each_page(&block)
  call_client(:each_page, &block)
end

#empty?Boolean

Returns true if count is greater than zero, else false.

Raises:

See Also:


45
46
47
# File 'lib/springboard/client/collection.rb', line 45

def empty?
  count <= 0
end

#filter(new_filters) ⇒ Resource

Returns a new resource with the given filters added to the query string.


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/springboard/client/collection.rb', line 57

def filter(new_filters)
  new_filters = JSON.parse(new_filters) if new_filters.is_a?(String)
  if filters = query['_filter']
    filters = JSON.parse(filters)
    filters = [filters] unless filters.is_a?(Array)
    filters.push(new_filters)
  else
    filters = new_filters
  end
  query('_filter' => filters.to_json)
end

#firstBody

Performs a request to get the first result of the first page of the collection and returns it.

Raises:


102
103
104
105
# File 'lib/springboard/client/collection.rb', line 102

def first
  response = query(:per_page => 1, :page => 1).get!
  response[:results].first
end

#only(*fields) ⇒ Resource

Returns a new resource with the given fields added to the query string as _only parameters.

Examples:

resource.only('id', :public_id)

91
92
93
# File 'lib/springboard/client/collection.rb', line 91

def only(*fields)
  query('_only' => fields)
end

#sort(*sorts) ⇒ Resource

Returns a new resource with the given sorts added to the query string.

Examples:

resource.sort('id,desc', 'name', '[email protected],desc', :description)

78
79
80
# File 'lib/springboard/client/collection.rb', line 78

def sort(*sorts)
  query('sort' => sorts)
end

#while_results(&block) ⇒ Object

Performs repeated GET requests to the resource and yields results to the given block as long as the response includes more results.

Raises:


112
113
114
115
116
117
118
# File 'lib/springboard/client/collection.rb', line 112

def while_results(&block)
  loop do
    results = get![:results]
    break if results.nil? || results.empty?
    results.each(&block)
  end
end