Class: Plurall::Schools
- Inherits:
-
Object
- Object
- Plurall::Schools
- Includes:
- Enumerable
- Defined in:
- lib/plurall/schools.rb
Overview
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Internal: Returns the GraphQL API response as a GraphQL::Client::Response.
Instance Method Summary collapse
-
#all(&block) ⇒ Object
Public: Iterates all schools.
-
#data ⇒ Object
Public: Returns the list of schools as a Hash.
-
#each(&block) ⇒ Object
Public: Iterates the list of schools.
-
#initialize(response) ⇒ Schools
constructor
Internal: Create a new object from a GraphQL API response.
-
#inspect ⇒ Object
Internal: Returns a string representation of the list.
-
#next ⇒ Object
Public: Retrieve the next page of schools.
-
#next? ⇒ Boolean
Public: Whether there are more schools available.
-
#next_cursor ⇒ Object
Internal: The next cursor used for pagination.
-
#prev ⇒ Object
Public: Retrieve the previous page of schools.
-
#prev? ⇒ Boolean
Public: Whether there are previous schools available.
-
#prev_cursor ⇒ Object
Internal: The previous cursor used for pagination.
-
#total_count ⇒ Object
Public: Returns the number of schools as an Integer.
Constructor Details
#initialize(response) ⇒ Schools
Internal: Create a new object from a GraphQL API response.
response - The GraphQL API response as a GraphQL::Client::Response.
22 23 24 |
# File 'lib/plurall/schools.rb', line 22 def initialize response @response = response end |
Instance Attribute Details
#response ⇒ Object (readonly)
Internal: Returns the GraphQL API response as a GraphQL::Client::Response.
15 16 17 |
# File 'lib/plurall/schools.rb', line 15 def response @response end |
Instance Method Details
#all(&block) ⇒ Object
Public: Iterates all schools.
Several API calls may be made to retrieve all by repeatedly calling #next until #next? returns ‘false`.
Examples
Plurall.schools.all do |school|
puts school.legal_name
end
Yields a school object to the block.
Returns an Enumerator when no block argument is given, or nothing when when a block argument is given.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/plurall/schools.rb', line 72 def all &block return enum_for :all unless block page = self loop do page.each(&block) page = page.next break if page.nil? end end |
#data ⇒ Object
Public: Returns the list of schools as a Hash.
34 35 36 |
# File 'lib/plurall/schools.rb', line 34 def data response.data.to_h end |
#each(&block) ⇒ Object
51 52 53 54 55 |
# File 'lib/plurall/schools.rb', line 51 def each &block return enum_for :each unless block response.data.schools.first.edges.lazy.map(&:node).each(&block) end |
#inspect ⇒ Object
Internal: Returns a string representation of the list.
165 166 167 |
# File 'lib/plurall/schools.rb', line 165 def inspect "#<#{self.class.name}:0x#{(object_id * 2).to_s(16).rjust(16, "0")} schools:#{total_count}>" end |
#next ⇒ Object
Public: Retrieve the next page of schools.
Returns the next page as Plurall::Schools, or nil.
113 114 115 116 117 118 |
# File 'lib/plurall/schools.rb', line 113 def next return nil unless next? next_response = Plurall::Api.schools after: next_cursor self.class.new next_response end |
#next? ⇒ Boolean
93 94 95 |
# File 'lib/plurall/schools.rb', line 93 def next? response.data.schools.first.page_info.has_next_page end |
#next_cursor ⇒ Object
Internal: The next cursor used for pagination.
Returns the next cursor as a String, or nil.
102 103 104 105 106 |
# File 'lib/plurall/schools.rb', line 102 def next_cursor return nil unless next? response.data.schools.first.page_info.end_cursor end |
#prev ⇒ Object
155 156 157 158 159 160 |
# File 'lib/plurall/schools.rb', line 155 def prev return nil unless prev? prev_response = Plurall::Api.schools before: prev_cursor self.class.new prev_response end |
#prev? ⇒ Boolean
130 131 132 |
# File 'lib/plurall/schools.rb', line 130 def prev? response.data.schools.first.page_info.has_previous_page end |
#prev_cursor ⇒ Object
Internal: The previous cursor used for pagination.
Returns the previous cursor as a String, or nil.
139 140 141 142 143 |
# File 'lib/plurall/schools.rb', line 139 def prev_cursor return nil unless prev? response.data.schools.first.page_info.start_cursor end |
#total_count ⇒ Object
Public: Returns the number of schools as an Integer.
28 29 30 |
# File 'lib/plurall/schools.rb', line 28 def total_count response.data.schools.first.total_count end |