Class: D4H::API::Collection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#pageObject (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_sizeObject (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

#resultsObject (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_jsonObject (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_sizeObject (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