Class: Recurly::Subscription

Inherits:
Resource show all
Defined in:
lib/recurly/subscription.rb,
lib/recurly/subscription/add_ons.rb

Defined Under Namespace

Classes: AddOns

Constant Summary collapse

REFUND_TYPES =

An array of acceptable refund types.

['none', 'full', 'partial'].freeze

Instance Attribute Summary

Attributes inherited from Resource

#attributes, #etag, #response, #uri

Class Method Summary collapse

Methods inherited from Resource

#==, all, associations, belongs_to, #changed, #changed?, #changed_attributes, #changes, collection_name, count, create, create!, define_attribute_methods, #destroy, #destroyed?, embedded!, #errors, find, find_each, first, from_response, from_xml, has_many, has_one, #initialize, #inspect, #marshal_dump, #marshal_load, member_name, member_path, #new_record?, paginate, #persist!, #persisted?, #previous_changes, #read_attribute, reflect_on_association, #reload, resource_name, #save, #save!, scope, scopes, #to_param, #to_xml, #update_attributes, #update_attributes!, #valid?, #write_attribute

Constructor Details

This class inherits a constructor from Recurly::Resource

Class Method Details

.accountAccount

Returns:



21
# File 'lib/recurly/subscription.rb', line 21

belongs_to :account

.activePager<Subscription>

Returns A pager that yields active subscriptions.

Returns:



8
# File 'lib/recurly/subscription.rb', line 8

scope :active,   :state => :active

.canceltrue, false

Cancel a subscription so that it will not renew.

Examples:

 = Account.find 
subscription = .subscriptions.first
subscription.cancel # => true

Returns:

  • (true, false)

    true when successful, false when unable to (e.g., the subscription is not active).



95
96
97
98
99
# File 'lib/recurly/subscription.rb', line 95

def cancel
  return false unless self[:cancel]
  reload self[:cancel].call
  true
end

.canceledPager<Subscription>

Returns A pager that yields canceled subscriptions.

Returns:



9
# File 'lib/recurly/subscription.rb', line 9

scope :canceled, :state => :canceled

.coupon=(coupon) ⇒ Object

Assign a Coupon resource (rather than a coupon code).

Parameters:



68
69
70
71
72
73
# File 'lib/recurly/subscription.rb', line 68

def coupon= coupon
  self.coupon_code = (
    coupon.coupon_code if coupon.respond_to? :coupon_code
  )
  attributes[:coupon] = coupon
end

.expiredPager<Subscription>

Returns A pager that yields expired subscriptions.

Returns:



10
# File 'lib/recurly/subscription.rb', line 10

scope :expired,  :state => :expired

.futurePager<Subscription>

Returns A pager that yields future subscriptions.

Returns:



11
# File 'lib/recurly/subscription.rb', line 11

scope :future,   :state => :future

.in_trialPager<Subscription>

Returns:



14
# File 'lib/recurly/subscription.rb', line 14

scope :in_trial, :state => :in_trial

.initialize(attributes = {}) ⇒ Subscription

Returns A new subscription.

Returns:



45
46
47
# File 'lib/recurly/subscription.rb', line 45

def initialize attributes = {}
  super({ :currency => Recurly.default_currency }.merge attributes)
end

.livePager<Subscription>

Returns:



17
# File 'lib/recurly/subscription.rb', line 17

scope :live,     :state => :live

.past_duePager<Subscription>

Returns A pager that yields past_due subscriptions.

Returns:



18
# File 'lib/recurly/subscription.rb', line 18

scope :past_due, :state => :past_due

.planPlan

Returns:



23
# File 'lib/recurly/subscription.rb', line 23

belongs_to :plan

.plan=(plan) ⇒ Object

Assign a Plan resource (rather than a plan code).

Parameters:



52
53
54
55
# File 'lib/recurly/subscription.rb', line 52

def plan= plan
  self.plan_code = (plan.plan_code if plan.respond_to? :plan_code)
  attributes[:plan] = plan
end

.plan_codeObject



57
58
59
# File 'lib/recurly/subscription.rb', line 57

def plan_code
  self[:plan_code] ||= (plan.plan_code if plan.respond_to? :plan_code)
end

.plan_code=(plan_code) ⇒ Object



61
62
63
# File 'lib/recurly/subscription.rb', line 61

def plan_code= plan_code
  self[:plan_code] = plan_code
end

.reactivatetrue, false

Reactivate a subscription.

Returns:

  • (true, false)

    true when successful, false when unable to (e.g., the subscription is already active), and may raise an exception if the reactivation fails.



132
133
134
135
136
# File 'lib/recurly/subscription.rb', line 132

def reactivate
  return false unless self[:reactivate]
  reload self[:reactivate].call
  true
end

.subscription_add_onsAddOns Also known as: add_ons

Returns:



76
77
78
# File 'lib/recurly/subscription.rb', line 76

def subscription_add_ons
  AddOns.new self, super
end

.subscription_add_ons=(subscription_add_ons) ⇒ Object Also known as: add_ons=

Assign an array of subscription add-ons.



82
83
84
# File 'lib/recurly/subscription.rb', line 82

def subscription_add_ons= subscription_add_ons
  super AddOns.new self, subscription_add_ons
end

.terminate(refund_type = :none) ⇒ true, false Also known as: destroy

Immediately terminate a subscription (with optional refund).

Examples:

 = Account.find 
subscription = .subscriptions.first
subscription.terminate(:partial) # => true

Parameters:

  • refund_type (:none, :full, :partial) (defaults to: :none)

    :none terminates the subscription with no refund (the default), :full refunds the subscription in full, and :partial refunds the subscription in part.

Returns:

  • (true, false)

    true when successful, false when unable to (e.g., the subscription is not active).

Raises:

  • (ArgumentError)

    Invalid refund_type.



117
118
119
120
121
122
123
124
# File 'lib/recurly/subscription.rb', line 117

def terminate refund_type = :none
  return false unless self[:terminate]
  unless REFUND_TYPES.include? refund_type.to_s
    raise ArgumentError, "refund must be one of: #{REFUND_TYPES.join ', '}"
  end
  reload self[:terminate].call(:params => { :refund => refund_type })
  true
end