Sha256: bca6bcc6c52378274285fd54b0814ce5588b5fe8b7d1eab76c0b4694b7c0a600

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Recurly
  class GiftCard < Resource
    belongs_to :invoice
    belongs_to :gifter_account, class_name: :Account, readonly: false
    belongs_to :recipient_account, class_name: :Account, readonly: false
    has_one :delivery, readonly: false, class_name: :Delivery
    has_one :address, readonly: false, class_name: :Address

    define_attribute_methods %w(
      balance_in_cents
      currency
      created_at
      delivered_at
      id
      product_code
      redeemed_at
      redemption_code
      unit_amount_in_cents
      updated_at
      canceled_at
    )

    def self.preview(attributes = {})
      new(attributes) { |record| record.preview }
    end

    def preview
      clear_errors
      @response = API.send(:post, "#{path}/preview", to_xml)
      reload response
    rescue API::UnprocessableEntity => e
      apply_errors e
    end

    def redeem(recipient_account_code)
      clear_errors
      xml = <<-XML
        <recipient_account>
            <account_code>#{recipient_account_code}</account_code>
        </recipient_account>
      XML
      @response = API.send(:post, "#{self.class.collection_path}/#{redemption_code}/redeem", xml)
      reload response
    rescue API::UnprocessableEntity => e
      apply_errors e
    end

    private

    def xml_keys
      keys = super
      keys << 'redemption_code' if redemption_code? && !redemption_code_changed?
      keys.sort
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
recurly-2.10.1 lib/recurly/gift_card.rb