Class: Ghub::API::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/ghub/api/page.rb

Overview

Represents a page of an API response.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Page

Returns a new instance of Page.



29
30
31
32
# File 'lib/ghub/api/page.rb', line 29

def initialize(response, **)
  @response = response
  super(**)
end

Class Method Details

.of(index = 1, bodies: [], &request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ghub/api/page.rb', line 17

def self.of index = 1, bodies: [], &request
  yield(index).fmap { |response| new response }
              .fmap { |page| [page, bodies.including(page.body)] }
              .bind do |page, amalgam|
                if page.last?
                  Success page.to_response(amalgam)
                else
                  of page.next, bodies: amalgam, &request
                end
              end
end

Instance Method Details

#bodyObject



38
# File 'lib/ghub/api/page.rb', line 38

def body = response.parse

#last?Boolean

Returns:

  • (Boolean)


36
# File 'lib/ghub/api/page.rb', line 36

def last? = navigation(:last).zero?

#nextObject



34
# File 'lib/ghub/api/page.rb', line 34

def next = navigation __method__

#to_response(content = []) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/ghub/api/page.rb', line 40

def to_response content = []
  return response if content.empty?

  response.class.new request: response.request,
                     headers: response.headers,
                     body: content.to_json,
                     status: response.status,
                     version: response.version
end