Class: SimpleAdvice::Collection
- Inherits:
-
Object
- Object
- SimpleAdvice::Collection
- Defined in:
- lib/simple_advice/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data:, total:) ⇒ Collection
constructor
A new instance of Collection.
Constructor Details
#initialize(data:, total:) ⇒ Collection
Returns a new instance of Collection.
18 19 20 21 |
# File 'lib/simple_advice/collection.rb', line 18 def initialize(data:, total:) @data = data @total = total end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/simple_advice/collection.rb', line 5 def data @data end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
5 6 7 |
# File 'lib/simple_advice/collection.rb', line 5 def total @total end |
Class Method Details
.from_response(response, key:, type:) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/simple_advice/collection.rb', line 7 def self.from_response(response, key:, type:) body = JSON.parse(response.body) data = body[key] return new(data: [], total: 0) unless data new( data: data.map { |attrs| type.new(attrs) }, total: body["total_results"].to_i ) end |