Skip to content

Commit

Permalink
Add subscription previewing
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarton committed May 14, 2014
1 parent 6a43f37 commit 0d55115
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/recurly/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Recurly
class Subscription < Resource
autoload :AddOns, 'recurly/subscription/add_ons'

class NotPreviewableError < StandardError; end

# @macro [attach] scope
# @scope class
# @return [Pager<Subscription>] A pager that yields +$1+ subscriptions.
Expand Down Expand Up @@ -51,6 +53,20 @@ class Subscription < Resource
)
alias to_param uuid

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

def preview
raise NotPreviewableError.new('Cannot preview an existing subscription') unless new_record?

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

# @return [Subscription] A new subscription.
def initialize attributes = {}
super({ :currency => Recurly.default_currency }.merge attributes)
Expand Down
10 changes: 10 additions & 0 deletions spec/recurly/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@
subscription.invoice.must_equal nil
end
end
end

describe 'previewing' do
it 'cannot preview an existing subscription' do
stub_api_request :get, 'subscriptions/abcdef1234567890', 'subscriptions/show-200-noinvoice'

subscription = Subscription.find 'abcdef1234567890'
assert_raises Subscription::NotPreviewableError do
subscription.preview
end
end
end
end

0 comments on commit 0d55115

Please sign in to comment.