Class: MyTankInfo::TankReconciliationRecordCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/my_tank_info/tank_reconciliation_record_collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, reconciliation_period:) ⇒ TankReconciliationRecordCollection

Returns a new instance of TankReconciliationRecordCollection.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 17

def initialize(data:, reconciliation_period:)
  @data = data
  @size = @data.size
  @reconciliation_period = reconciliation_period

  @site_id = @data.first&.site_id
  @started_at = @data.min_by(&:started_at)&.started_at
  @ended_at = @data.max_by(&:started_at)&.started_at

  @volume_uom = @data.first&.volume_uom
  @height_uom = @data.first&.height_uom
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def data
  @data
end

#ended_atObject (readonly)

Returns the value of attribute ended_at.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def ended_at
  @ended_at
end

#height_uomObject (readonly)

Returns the value of attribute height_uom.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def height_uom
  @height_uom
end

#reconciliation_periodObject (readonly)

Returns the value of attribute reconciliation_period.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def reconciliation_period
  @reconciliation_period
end

#site_idObject (readonly)

Returns the value of attribute site_id.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def site_id
  @site_id
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def size
  @size
end

#started_atObject (readonly)

Returns the value of attribute started_at.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def started_at
  @started_at
end

#volume_uomObject (readonly)

Returns the value of attribute volume_uom.



5
6
7
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 5

def volume_uom
  @volume_uom
end

Class Method Details

.from_response(response, reconciliation_period:) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 8

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

  @collection = new(
    data: body.map { |attrs| TankReconciliationRecord.new(attrs) },
    reconciliation_period: reconciliation_period
  )
end

Instance Method Details

#tanksObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/my_tank_info/tank_reconciliation_record_collection.rb', line 30

def tanks
  @data.map { |record|
    records = @data.select { _1.tank_number == record.tank_number }.sort_by(&:started_at)
    summary =
      TankReconciliationRecordSummary.new(
        records,
        capacity: record.total_tanks_capacity,
        reconciliation_period: @reconciliation_period
      )

    Tank.new(
      name: record.name,
      product_name: record.product_name,
      tank_number: record.tank_number,
      tank_numbers: record.tank_numbers,
      capacity: record.total_tanks_capacity,
      reconciliation_records: records,
      reconciliation_summary: summary,
      passed?: summary.passed?,
      failed?: summary.failed?
    )
  }.uniq { |tank| tank.name }
    .sort_by(&:tank_number)
end