Class: Recurly::Coupon

Inherits:
Resource show all
Defined in:
lib/recurly/coupon.rb

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

.expiredPager<Coupon>

Returns A pager that yields expired coupons.

Returns:

  • (Pager<Coupon>)

    A pager that yields expired coupons.



7
# File 'lib/recurly/coupon.rb', line 7

scope :expired,    :state => :expired

.maxed_outPager<Coupon>

Returns A pager that yields maxed_out coupons.

Returns:

  • (Pager<Coupon>)

    A pager that yields maxed_out coupons.



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

scope :maxed_out,  :state => :maxed_out

.redeem(account_or_code, currency = nil) ⇒ true

Redeem a coupon with a given account or account code.

Examples:

coupon = Coupon.find coupon_code
coupon.redeem 

coupon = Coupon.find coupon_code
 = Account.find 
coupon.redeem 

Parameters:

  • account_or_code (Account, String)

Returns:

  • (true)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/recurly/coupon.rb', line 51

def redeem , currency = nil
  return false unless self[:redeem]

   = if .is_a? Account
    .
  else
    
  end

  Redemption.from_response self[:redeem].call(
    :body => (redemption = redemptions.new(
      :account_code => ,
      :currency     => currency || Recurly.default_currency
    )).to_xml
  )
rescue API::UnprocessableEntity => e
  redemption.apply_errors e
  redemption
end

.redeem!(account_code, currency = nil) ⇒ Object

Raises:



71
72
73
74
75
# File 'lib/recurly/coupon.rb', line 71

def redeem! , currency = nil
  redemption = redeem , currency
  raise Invalid.new(self) unless redemption.persisted?
  redemption
end

.redeemablePager<Coupon>

Returns A pager that yields redeemable coupons.

Returns:

  • (Pager<Coupon>)

    A pager that yields redeemable coupons.



6
# File 'lib/recurly/coupon.rb', line 6

scope :redeemable, :state => :redeemable

.redemptionsPager<Redemption>, []

Returns:



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

has_many :redemptions

.savetrue, false

Saves new records only.

Returns:

  • (true, false)

Raises:

See Also:



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

def save
  return super if new_record?
  raise Recurly::Error, "#{self.class.collection_name} cannot be updated"
end