module Billing module Stripe module SubscriptionPeriod module_function def times(stripe_sub) item = stripe_sub.items&.data&.first start_at = read_value(stripe_sub, :current_period_start) || read_value(item, :current_period_start) end_at = read_value(stripe_sub, :current_period_end) || read_value(item, :current_period_end) [unix_time(start_at), unix_time(end_at)] end def read_value(object, field) return nil if object.blank? return nil unless object.respond_to?(field) object.public_send(field) end def unix_time(value) return nil if value.blank? Time.zone.at(value.to_i) end end end end