Class: ApiSports::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, current_page:, total_pages:) ⇒ Collection

Returns a new instance of Collection.



17
18
19
20
21
22
# File 'lib/api_sports/collection.rb', line 17

def initialize(data:, total:, current_page:, total_pages:)
  @data = data
  @total = total
  @current_page = current_page
  @total_pages = total_pages
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



5
6
7
# File 'lib/api_sports/collection.rb', line 5

def current_page
  @current_page
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/api_sports/collection.rb', line 5

def data
  @data
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/api_sports/collection.rb', line 5

def total
  @total
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



5
6
7
# File 'lib/api_sports/collection.rb', line 5

def total_pages
  @total_pages
end

Class Method Details

.from_response(response, type:, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/api_sports/collection.rb', line 7

def self.from_response(response, type:, &block)
  body = response.body
  new(
    data: block.nil? ? body["response"].map { |attrs| type.new(attrs) } : yield(body),
    total: body["results"],
    current_page: body["paging"]["current"],
    total_pages: body["paging"]["total"]
  )
end