Class: FindworkApi::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count:, next_page:, previous_page:, data:) ⇒ Collection

Returns a new instance of Collection.



16
17
18
19
20
21
# File 'lib/findwork_api/collection.rb', line 16

def initialize(count:, next_page:, previous_page:, data:)
  @count = count
  @next_page = next_page.nil? ? nil : next_page
  @previous_page = previous_page.nil? ? nil : previous_page
  @data = data
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



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

def count
  @count
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



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

def next_page
  @next_page
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



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

def previous_page
  @previous_page
end

Class Method Details

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



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

def self.from_response(response, key:, type:)
  body = response.body
  # Nested values can be appended as additional arguments to dig
  new(
    count: body.dig("count"),
    next_page: body.dig("next"),
    previous_page: body.dig("previous"),
    data: body[key].map { |attrs| type.new(attrs) }
  )
end