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, 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:



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

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).



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

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

.in_trialPager<Subscription>

Returns A pager that yields in_trial subscriptions.

Returns:



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

scope :in_trial,     :state => :in_trial

.initialize(attributes = {}) ⇒ Subscription

Returns A new subscription.

Returns:



36
37
38
# File 'lib/recurly/subscription.rb', line 36

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

.non_renewingPager<Subscription>

Returns A pager that yields non_renewing subscriptions.

Returns:



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

scope :non_renewing, :state => :non_renewing

.planPlan

Returns:



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

belongs_to :plan

.plan=(plan) ⇒ Object

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

Parameters:



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

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

.plan_codeObject



48
49
50
# File 'lib/recurly/subscription.rb', line 48

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

.plan_code=(plan_code) ⇒ Object



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

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.



112
113
114
115
116
# File 'lib/recurly/subscription.rb', line 112

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

.subscription_add_onsAddOns Also known as: add_ons

Returns:



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

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.



63
64
65
# File 'lib/recurly/subscription.rb', line 63

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

.terminate(refund_type = :none) ⇒ true, false

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.



98
99
100
101
102
103
104
105
# File 'lib/recurly/subscription.rb', line 98

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