Class: SubscriptionBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, name, plan, *args) ⇒ SubscriptionBuilder

Returns a new instance of SubscriptionBuilder.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/subscription_builder.rb', line 6

def initialize(user, name, plan, *args)
  @user = user
  @name = name
  @plan = plan
  if args[0]
    set_instance_vars_for_args(args[0])
  else
    @trial_days = 0
    @quantity = 1
    @skip_trial = false
  end
end

Instance Attribute Details

#couponObject

Returns the value of attribute coupon.



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

def coupon
  @coupon
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/subscription_builder.rb', line 2

def name
  @name
end

#planObject

Returns the value of attribute plan.



2
3
4
# File 'lib/subscription_builder.rb', line 2

def plan
  @plan
end

#quantityObject

Returns the value of attribute quantity.



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

def quantity
  @quantity
end

#skip_trialObject

Returns the value of attribute skip_trial.



4
5
6
# File 'lib/subscription_builder.rb', line 4

def skip_trial
  @skip_trial
end

#trial_daysObject

Returns the value of attribute trial_days.



4
5
6
# File 'lib/subscription_builder.rb', line 4

def trial_days
  @trial_days
end

#userObject

Returns the value of attribute user.



2
3
4
# File 'lib/subscription_builder.rb', line 2

def user
  @user
end

Instance Method Details

#add(options = {}) ⇒ Object



19
20
21
# File 'lib/subscription_builder.rb', line 19

def add(options = {})
  create(nil, options)
end

#create(token = nil, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/subscription_builder.rb', line 23

def create(token = nil, options = {})
  customer = get_stripe_customer(token, options)
  stripe_subscription = customer.subscriptions.create(pay_load)

  if skip_trial
    trial_ends_at = nil
  else
    trial_ends_at = trial_days ? trial_days.days.from_now.to_time.to_i : nil
  end

  user.subscriptions.create(
    name: name,
    stripe_id: stripe_subscription.id,
    stripe_plan: plan,
    quantity: quantity,
    trial_ends_at: trial_ends_at
  )
end