Class: SoracomSummary::Billing
- Inherits:
-
Object
- Object
- SoracomSummary::Billing
- Defined in:
- lib/soracom_summary/billing.rb
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#bill_item_name ⇒ Object
Returns the value of attribute bill_item_name.
-
#date ⇒ Object
Returns the value of attribute date.
-
#device_id ⇒ Object
Returns the value of attribute device_id.
-
#imsi ⇒ Object
Returns the value of attribute imsi.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(imsi:, device_id:, date:, bill_item_name:, amount:) ⇒ Billing
constructor
A new instance of Billing.
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
#amount ⇒ Object
Returns the value of attribute amount.
3 4 5 |
# File 'lib/soracom_summary/billing.rb', line 3 def amount @amount end |
#bill_item_name ⇒ Object
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 |
#date ⇒ Object
Returns the value of attribute date.
3 4 5 |
# File 'lib/soracom_summary/billing.rb', line 3 def date @date end |
#device_id ⇒ Object
Returns the value of attribute device_id.
3 4 5 |
# File 'lib/soracom_summary/billing.rb', line 3 def device_id @device_id end |
#imsi ⇒ Object
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 |