Class: SoracomSummary::Billing

Inherits:
Object
  • Object
show all
Defined in:
lib/soracom_summary/billing.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imsi:, device_id:, date:, bill_item_name:, amount:) ⇒ Billing

Returns a new instance of Billing.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/soracom_summary/billing.rb', line 49

def initialize(
  imsi:,
  device_id:,
  date:,
  bill_item_name:,
  amount:)
  @imsi = imsi
  @device_id = device_id
  @date = date
  @bill_item_name = bill_item_name
  @amount = amount
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



3
4
5
# File 'lib/soracom_summary/billing.rb', line 3

def amount
  @amount
end

#bill_item_nameObject

Returns the value of attribute bill_item_name.



3
4
5
# File 'lib/soracom_summary/billing.rb', line 3

def bill_item_name
  @bill_item_name
end

#dateObject

Returns the value of attribute date.



3
4
5
# File 'lib/soracom_summary/billing.rb', line 3

def date
  @date
end

#device_idObject

Returns the value of attribute device_id.



3
4
5
# File 'lib/soracom_summary/billing.rb', line 3

def device_id
  @device_id
end

#imsiObject

Returns the value of attribute imsi.



3
4
5
# File 'lib/soracom_summary/billing.rb', line 3

def imsi
  @imsi
end

Class Method Details

.group_by_origin(billings, time) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/soracom_summary/billing.rb', line 30

def group_by_origin(billings, time)
  day = time.strftime('%Y%m%d')

  # 対象期間の請求をフィルタする
  day_billings = billings.select { |billing| billing.date == day }
  
  day_billings_by_origin = day_billings
  .group_by { |billing| billing.imsi || billing.device_id || billing.bill_item_name }
  .map { |origin, group| [origin, sum(group)] }
  .to_h

  day_billings_by_origin
end

.sum(billings) ⇒ Object



44
45
46
# File 'lib/soracom_summary/billing.rb', line 44

def sum(billings)
  billings.inject(0.0) { |sum, billing| sum + billing.amount.to_f }
end

.summary(billings, time) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/soracom_summary/billing.rb', line 5

def summary(billings, time)
  month_text = time.strftime('%Y%m')
  day = time.strftime('%Y%m%d').to_i

  # 対象期間の請求をフィルタする
  month_billings = billings.select { |billing| billing.date[0, 6] == month_text && billing.date.to_i <= day }
  day_billings = billings.select { |billing| billing.date.to_i == day }
  billing_summary = { 'billings-month-total' => sum(month_billings), 'billings-day-total' => sum(day_billings) }

  # 請求を項目ごとにグループ化する
  month_billings_by_item = month_billings
    .group_by { |billing| billing.bill_item_name }
    .map { |item, group| ["billings-month-item-#{item}", sum(group) ]}
    .to_h
  billing_summary.merge!(month_billings_by_item)

  day_billings_by_item = day_billings
    .group_by { |billing| billing.bill_item_name }
    .map { |item, group| ["billings-day-item-#{item}", sum(group) ]}
    .to_h
  billing_summary.merge!(day_billings_by_item)

  billing_summary
end