Sha256: 4b2b0abaaa52c23963f62a663f293c2a07fe1739795d07737f472f5afe11eb9c

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module MyTankInfo
  class TankReconciliationRecordCollection
    attr_reader :data, :size, :site_id, :reconciliation_period, :started_at, :ended_at

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

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

    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
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
my_tank_info-1.0.1 lib/my_tank_info/tank_reconciliation_record_collection.rb
my_tank_info-1.0.0 lib/my_tank_info/tank_reconciliation_record_collection.rb