Class: D4H::API::Collection
- Inherits:
-
Object
- Object
- D4H::API::Collection
- Includes:
- Enumerable
- Defined in:
- lib/d4h/api/collection.rb
Overview
Public: An Enumerable wrapper around a paginated D4H API list response.
Parses the standard D4H v3 list envelope — which contains “results”, “page”, “pageSize”, and “totalSize” — and converts each result into the given model class.
Includes Enumerable, so you can call map, select, first, count, and any other Enumerable method directly on a collection.
Examples
collection = client.member.list
collection.total_size # => 90
collection.page # => 0
collection.map(&:name) # => ["Alice", "Bob", ...]
collection.first.status # => "OPERATIONAL"
collection.select { |m| m.status == "OPERATIONAL" }
# Raw JSON envelope
collection.to_json # => {"results" => [...], "page" => 0, ...}
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Public: Returns the Array of Model instances from this page.
-
#page_size ⇒ Object
readonly
Public: Returns the Array of Model instances from this page.
-
#results ⇒ Object
readonly
Public: Returns the Array of Model instances from this page.
-
#to_json ⇒ Object
readonly
Public: Returns the Array of Model instances from this page.
-
#total_size ⇒ Object
readonly
Public: Returns the Array of Model instances from this page.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Public: Yield each Model in the results array.
-
#initialize(body, model_class:) ⇒ Collection
constructor
Public: Initialize a Collection from a parsed JSON response body.
Constructor Details
#initialize(body, model_class:) ⇒ Collection
Public: Initialize a Collection from a parsed JSON response body.
body - A Hash with “results”, “page”, “pageSize”, “totalSize” keys. model_class - The Model subclass to wrap each result in (e.g. Member, Event).
37 38 39 40 41 42 43 |
# File 'lib/d4h/api/collection.rb', line 37 def initialize(body, model_class:) @to_json = body @results = (body["results"] || []).map { |attrs| model_class.new(attrs) } @page = body["page"] @page_size = body["pageSize"] @total_size = body["totalSize"] end |
Instance Attribute Details
#page ⇒ Object (readonly)
Public: Returns the Array of Model instances from this page. Public: Returns the zero-based page number. Public: Returns the page size (number of results per page). Public: Returns the total number of results across all pages. Public: Returns the original JSON hash envelope.
31 32 33 |
# File 'lib/d4h/api/collection.rb', line 31 def page @page end |
#page_size ⇒ Object (readonly)
Public: Returns the Array of Model instances from this page. Public: Returns the zero-based page number. Public: Returns the page size (number of results per page). Public: Returns the total number of results across all pages. Public: Returns the original JSON hash envelope.
31 32 33 |
# File 'lib/d4h/api/collection.rb', line 31 def page_size @page_size end |
#results ⇒ Object (readonly)
Public: Returns the Array of Model instances from this page. Public: Returns the zero-based page number. Public: Returns the page size (number of results per page). Public: Returns the total number of results across all pages. Public: Returns the original JSON hash envelope.
31 32 33 |
# File 'lib/d4h/api/collection.rb', line 31 def results @results end |
#to_json ⇒ Object (readonly)
Public: Returns the Array of Model instances from this page. Public: Returns the zero-based page number. Public: Returns the page size (number of results per page). Public: Returns the total number of results across all pages. Public: Returns the original JSON hash envelope.
31 32 33 |
# File 'lib/d4h/api/collection.rb', line 31 def to_json @to_json end |
#total_size ⇒ Object (readonly)
Public: Returns the Array of Model instances from this page. Public: Returns the zero-based page number. Public: Returns the page size (number of results per page). Public: Returns the total number of results across all pages. Public: Returns the original JSON hash envelope.
31 32 33 |
# File 'lib/d4h/api/collection.rb', line 31 def total_size @total_size end |
Instance Method Details
#each(&block) ⇒ Object
Public: Yield each Model in the results array.
Yields each Model instance.
50 51 52 |
# File 'lib/d4h/api/collection.rb', line 50 def each(&block) results.each(&block) end |