Class: IdealPostcodes::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:) ⇒ Collection

Returns a new instance of Collection.



20
21
22
23
# File 'lib/ideal_postcodes/collection.rb', line 20

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.from_response(response, type:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ideal_postcodes/collection.rb', line 5

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

  if body["result"] and body["result"].include? "hits"
    data = body["result"]["hits"]
  else
    data = body["result"]
  end

  new(
    data: data.map { |attrs| type.new(attrs) },
    total: data.count
  )
end