Class: SportsApi::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, paging:, paging_total:, paging_current:) ⇒ Collection

Returns a new instance of Collection.



22
23
24
25
26
27
28
# File 'lib/sports_api/collection.rb', line 22

def initialize(data:, total:, paging:, paging_total:, paging_current:)
  @data = data
  @total = total
  @paging = paging
  @paging_total = paging_total
  @paging_current = paging_current
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



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

def current
  @current
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

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

Example usage: from_reponse(response, key: “response”, type: League) SportsApi::Collection.from_response(response, type: SportsApi::Football::League)



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sports_api/collection.rb', line 10

def self.from_response(response, type:, key: 'response')
  body = response.body

  new(
    data: body[key].map { |attrs| type.new(attrs) },
    total: body['results'],
    paging: body['paging'],
    paging_total: body.dig('paging', 'total'),
    paging_current: body.dig('paging', 'current')
  )
end