Class: Plurall::Schools

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/plurall/schools.rb

Overview

Public: A list of schools that supports pagination.

Examples

Plurall.schools.each do |school|
  puts school.legal_name
end

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#responseObject (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

#dataObject

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

Public: Iterates the list of schools.

Examples

Plurall.schools.each 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.



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

#inspectObject

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

#nextObject

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

Public: Whether there are more schools available.

Examples

schools = Plurall.schools school_id: 123456
schools.next?

Returns true or false.



93
94
95
# File 'lib/plurall/schools.rb', line 93

def next?
  response.data.schools.first.page_info.has_next_page
end

#next_cursorObject

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

#prevObject

Public: Retrieve the previous page of schools.

Examples

schools = Plurall.schools school_id: 123456
prev_schools = schools.prev

Returns the previous page as Plurall::Schools, or nil.



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

Public: Whether there are previous schools available.

Examples

schools = Plurall.schools school_id: 123456
schools.prev?

Returns true or false.



130
131
132
# File 'lib/plurall/schools.rb', line 130

def prev?
  response.data.schools.first.page_info.has_previous_page
end

#prev_cursorObject

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_countObject

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