Method: AdvancedBilling::HistoricUsage.from_hash

Defined in:
lib/advanced_billing/models/historic_usage.rb

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/advanced_billing/models/historic_usage.rb', line 62

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  total_usage_quantity =
    hash.key?('total_usage_quantity') ? hash['total_usage_quantity'] : SKIP
  billing_period_starts_at = if hash.key?('billing_period_starts_at')
                               (DateTimeHelper.from_rfc3339(hash['billing_period_starts_at']) if hash['billing_period_starts_at'])
                             else
                               SKIP
                             end
  billing_period_ends_at = if hash.key?('billing_period_ends_at')
                             (DateTimeHelper.from_rfc3339(hash['billing_period_ends_at']) if hash['billing_period_ends_at'])
                           else
                             SKIP
                           end

  # Clean out expected properties from Hash.
  additional_properties = hash.reject { |k, _| names.value?(k) }

  # Create object from extracted values.
  HistoricUsage.new(total_usage_quantity: total_usage_quantity,
                    billing_period_starts_at: billing_period_starts_at,
                    billing_period_ends_at: billing_period_ends_at,
                    additional_properties: additional_properties)
end