Skip to content

Commit

Permalink
Add support for Transactions API
Browse files Browse the repository at this point in the history
  • Loading branch information
rlivsey committed Jul 21, 2011
1 parent e47de51 commit df28c5d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rspreedly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'rspreedly/complimentary_subscription'
require 'rspreedly/complimentary_time_extension'
require 'rspreedly/lifetime_complimentary_subscription'
require 'rspreedly/transaction'

module RSpreedly

Expand Down
39 changes: 39 additions & 0 deletions lib/rspreedly/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module RSpreedly

class Transaction < Base

attr_accessor :active,
:amount,
:created_at,
:currency_code,
:description,
:detail_type,
:expires,
:id,
:invoice_id,
:start_time,
:terms,
:updated_at,
:price,
:subscriber_customer_id,
:detail

class << self

# Get a list of 50 transactions
# Passing :since => id will get the 50 transactions since that one
# GET /api/v4/[short site name]/transactions.xml
def all(opts={})
query_opts = {}
if opts[:since]
query_opts[:query] = {:since_id => opts[:since]}
end

response = api_request(:get, "/transactions.xml", query_opts)
return [] unless response.has_key?("transactions")
response["transactions"].collect{|data| Transaction.new(data)}
end

end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/no_transactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<nil-classes type="array"/>
63 changes: 63 additions & 0 deletions spec/fixtures/transactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<transactions type="array">
<transaction>
<amount type="decimal">24.0</amount>
<created-at type="datetime">2009-09-26T03:06:30Z</created-at>
<currency-code>USD</currency-code>
<description>Subscription</description>
<detail-type>Subscription</detail-type>
<expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
<id type="integer">20</id>
<invoice-id type="integer">64</invoice-id>
<start-time type="datetime">2009-09-26T03:06:30Z</start-time>
<terms>3 months</terms>
<updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
<price>$24.00</price>
<subscriber-customer-id>39053</subscriber-customer-id>
<detail>
<payment-method>visa</payment-method>
<recurring type="boolean">false</recurring>
<feature-level type="string">example</feature-level>
</detail>
</transaction>
<transaction>
<amount type="decimal">24.0</amount>
<created-at type="datetime">2009-09-26T03:06:30Z</created-at>
<currency-code>USD</currency-code>
<description>Subscription</description>
<detail-type>Subscription</detail-type>
<expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
<id type="integer">20</id>
<invoice-id type="integer">64</invoice-id>
<start-time type="datetime">2009-09-26T03:06:30Z</start-time>
<terms>3 months</terms>
<updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
<price>$24.00</price>
<subscriber-customer-id>39053</subscriber-customer-id>
<detail>
<payment-method>visa</payment-method>
<recurring type="boolean">false</recurring>
<feature-level type="string">example</feature-level>
</detail>
</transaction>
<transaction>
<amount type="decimal">24.0</amount>
<created-at type="datetime">2009-09-26T03:06:30Z</created-at>
<currency-code>USD</currency-code>
<description>Subscription</description>
<detail-type>Subscription</detail-type>
<expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
<id type="integer">20</id>
<invoice-id type="integer">64</invoice-id>
<start-time type="datetime">2009-09-26T03:06:30Z</start-time>
<terms>3 months</terms>
<updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
<price>$24.00</price>
<subscriber-customer-id>39053</subscriber-customer-id>
<detail>
<payment-method>visa</payment-method>
<recurring type="boolean">false</recurring>
<feature-level type="string">example</feature-level>
</detail>
</transaction>
</transactions>
28 changes: 28 additions & 0 deletions spec/transaction_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe RSpreedly::Transaction do

describe ".all" do
it "should return an empty array if there are no transactions" do
stub_request(:get, spreedly_url("/transactions.xml")).
to_return(:body => fixture("no_transactions.xml"), :status => 200)

RSpreedly::Transaction.all.should == []
end

it "should return an array of transactions if there are any to find" do
stub_request(:get, spreedly_url("/transactions.xml")).
to_return(:body => fixture("transactions.xml"), :status => 200)

RSpreedly::Transaction.all.size.should == 3 # there are 3 in the fixture
RSpreedly::Transaction.all.select{|x| x.is_a?(RSpreedly::Transaction )}.size.should == 3
end

it "should allow specifying the ID of the transaction to start (since_id)" do
stub_request(:get, spreedly_url("/transactions.xml?since_id=123")).
to_return(:body => fixture("transactions.xml"), :status => 200)

RSpreedly::Transaction.all(:since => 123)
WebMock.should have_requested(:get, spreedly_url("/transactions.xml?since_id=123"))
end
end
end

0 comments on commit df28c5d

Please sign in to comment.