Method: AdvancedBilling::Coupon.from_hash

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

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/advanced_billing/models/coupon.rb', line 287

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  id = hash.key?('id') ? hash['id'] : SKIP
  name = hash.key?('name') ? hash['name'] : SKIP
  code = hash.key?('code') ? hash['code'] : SKIP
  description = hash.key?('description') ? hash['description'] : SKIP
  amount = hash.key?('amount') ? hash['amount'] : SKIP
  amount_in_cents =
    hash.key?('amount_in_cents') ? hash['amount_in_cents'] : SKIP
  product_family_id =
    hash.key?('product_family_id') ? hash['product_family_id'] : SKIP
  product_family_name =
    hash.key?('product_family_name') ? hash['product_family_name'] : SKIP
  start_date = if hash.key?('start_date')
                 (DateTimeHelper.from_rfc3339(hash['start_date']) if hash['start_date'])
               else
                 SKIP
               end
  end_date = if hash.key?('end_date')
               (DateTimeHelper.from_rfc3339(hash['end_date']) if hash['end_date'])
             else
               SKIP
             end
  percentage = hash.key?('percentage') ? hash['percentage'] : SKIP
  recurring = hash.key?('recurring') ? hash['recurring'] : SKIP
  recurring_scheme =
    hash.key?('recurring_scheme') ? hash['recurring_scheme'] : SKIP
  duration_period_count =
    hash.key?('duration_period_count') ? hash['duration_period_count'] : SKIP
  duration_interval =
    hash.key?('duration_interval') ? hash['duration_interval'] : SKIP
  duration_interval_unit =
    hash.key?('duration_interval_unit') ? hash['duration_interval_unit'] : SKIP
  duration_interval_span =
    hash.key?('duration_interval_span') ? hash['duration_interval_span'] : SKIP
  allow_negative_balance =
    hash.key?('allow_negative_balance') ? hash['allow_negative_balance'] : SKIP
  archived_at = if hash.key?('archived_at')
                  (DateTimeHelper.from_rfc3339(hash['archived_at']) if hash['archived_at'])
                else
                  SKIP
                end
  conversion_limit =
    hash.key?('conversion_limit') ? hash['conversion_limit'] : SKIP
  stackable = hash.key?('stackable') ? hash['stackable'] : SKIP
  compounding_strategy =
    hash.key?('compounding_strategy') ? hash['compounding_strategy'] : SKIP
  use_site_exchange_rate =
    hash.key?('use_site_exchange_rate') ? hash['use_site_exchange_rate'] : SKIP
  created_at = if hash.key?('created_at')
                 (DateTimeHelper.from_rfc3339(hash['created_at']) if hash['created_at'])
               else
                 SKIP
               end
  updated_at = if hash.key?('updated_at')
                 (DateTimeHelper.from_rfc3339(hash['updated_at']) if hash['updated_at'])
               else
                 SKIP
               end
  discount_type = hash.key?('discount_type') ? hash['discount_type'] : SKIP
  exclude_mid_period_allocations =
    hash.key?('exclude_mid_period_allocations') ? hash['exclude_mid_period_allocations'] : SKIP
  apply_on_cancel_at_end_of_period =
    hash.key?('apply_on_cancel_at_end_of_period') ? hash['apply_on_cancel_at_end_of_period'] : SKIP
  apply_on_subscription_expiration =
    hash.key?('apply_on_subscription_expiration') ? hash['apply_on_subscription_expiration'] : SKIP
  # Parameter is an array, so we need to iterate through it
  coupon_restrictions = nil
  unless hash['coupon_restrictions'].nil?
    coupon_restrictions = []
    hash['coupon_restrictions'].each do |structure|
      coupon_restrictions << (CouponRestriction.from_hash(structure) if structure)
    end
  end

  coupon_restrictions = SKIP unless hash.key?('coupon_restrictions')

  # Clean out expected properties from Hash.
  names.each_value { |k| hash.delete(k) }

  # Create object from extracted values.
  Coupon.new(id: id,
             name: name,
             code: code,
             description: description,
             amount: amount,
             amount_in_cents: amount_in_cents,
             product_family_id: product_family_id,
             product_family_name: product_family_name,
             start_date: start_date,
             end_date: end_date,
             percentage: percentage,
             recurring: recurring,
             recurring_scheme: recurring_scheme,
             duration_period_count: duration_period_count,
             duration_interval: duration_interval,
             duration_interval_unit: duration_interval_unit,
             duration_interval_span: duration_interval_span,
             allow_negative_balance: allow_negative_balance,
             archived_at: archived_at,
             conversion_limit: conversion_limit,
             stackable: stackable,
             compounding_strategy: compounding_strategy,
             use_site_exchange_rate: use_site_exchange_rate,
             created_at: created_at,
             updated_at: updated_at,
             discount_type: discount_type,
             exclude_mid_period_allocations: exclude_mid_period_allocations,
             apply_on_cancel_at_end_of_period: apply_on_cancel_at_end_of_period,
             apply_on_subscription_expiration: apply_on_subscription_expiration,
             coupon_restrictions: coupon_restrictions,
             additional_properties: hash)
end