Class: SportsApi::Collection
- Inherits:
-
Object
- Object
- SportsApi::Collection
- Defined in:
- lib/sports_api/collection.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.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).
Instance Method Summary collapse
-
#initialize(data:, total:, paging:, paging_total:, paging_current:) ⇒ Collection
constructor
A new instance of Collection.
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
#current ⇒ Object (readonly)
Returns the value of attribute current.
5 6 7 |
# File 'lib/sports_api/collection.rb', line 5 def current @current end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/sports_api/collection.rb', line 5 def data @data end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
5 6 7 |
# File 'lib/sports_api/collection.rb', line 5 def results @results end |
#total ⇒ Object (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 |