Class: PersonaApi::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/persona_api/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, next_page:, prev_page:) ⇒ Collection

Returns a new instance of Collection.



14
15
16
17
18
# File 'lib/persona_api/collection.rb', line 14

def initialize(data:, next_page:, prev_page:)
  @data = data
  @next_page = next_page.nil? ? nil : data.last.id
  @prev_page = prev_page.nil? ? nil : data.first.id
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/persona_api/collection.rb', line 3

def data
  @data
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



3
4
5
# File 'lib/persona_api/collection.rb', line 3

def next_page
  @next_page
end

#prev_pageObject (readonly)

Returns the value of attribute prev_page.



3
4
5
# File 'lib/persona_api/collection.rb', line 3

def prev_page
  @prev_page
end

Class Method Details

.from_response(response, key:, type:) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/persona_api/collection.rb', line 5

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body[key].map{ |attrs| type.new(attrs) },
    next_page: body.dig("links", "next"),
    prev_page: body.dig("links", "prev")
  )
end