Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk allocations #125

Merged
merged 3 commits into from
Jan 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: ruby
before_install:
- gem update --system
- gem update bundler
rvm:
- 2.1.3
- 2.0.0
Expand Down
27 changes: 22 additions & 5 deletions lib/chargify_api_ares/resources/allocation.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
module Chargify

class Allocation < Base

self.prefix = "/subscriptions/:subscription_id/components/:component_id/"

self.prefix = "/subscriptions/:subscription_id/components/:component_id/"

def self.bulk_create_prefix(opts = {})
subscription_id = opts[:subscription_id]
raise ArgumentError, 'subscription_id required' if subscription_id.nil?

"/subscriptions/#{subscription_id}/allocations.#{format.extension}"
end

def self.bulk_create(opts = {})
return [] if opts[:allocations].blank?

subscription_id = opts.delete(:subscription_id)
raise ArgumentError, 'subscription_id required' if subscription_id.nil?

response = connection.post(
bulk_create_prefix(subscription_id: subscription_id),
format.encode(opts),
headers
)
instantiate_collection(format.decode(response.body))
end
end

end
91 changes: 91 additions & 0 deletions spec/resources/allocation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'spec_helper'

describe Chargify::Allocation do
let(:subscription_id) { 123 }
let(:component_id) { 456 }
let(:allocations) do
[
{
component_id: component_id,
quantity: 1,
memo: 'test'
}
]
end
describe '.bulk_create' do
context 'when no allocations are specified' do
it 'returns an empty array' do
result = Chargify::Allocation.bulk_create(allocations: [])
expect(result).to be_empty
end
end

context 'when the subscription_id is missing' do
it 'raises' do
expect {
Chargify::Allocation.bulk_create(allocations: allocations)
}.to raise_error(/subscription_id required/)
end
end

context 'with correct parameters' do
let(:json_body) do
[{
allocation: {
component_id: component_id,
subscription_id: subscription_id,
quantity: 1,
previous_quantity: 0,
memo: 'test',
timestamp: Time.now.to_s(:iso8601),
proration_upgrade_scheme: 'prorate-attempt-capture',
proration_downgrade_scheme: 'no-prorate',
payment: {
amount_in_cents: 2000,
success: true,
memo: 'Payment for: Prorated component allocation',
id: 123
}
}
}].to_json
end

let(:headers) do
{
'Content-Type' => 'application/json',
'Authorization' => 'Basic foobar=='
}
end

before do
FakeWeb.clean_registry
Chargify::Allocation.format = :json
FakeWeb.register_uri(
:post,
URI.join(
test_domain,
Chargify::Allocation.bulk_create_prefix(
subscription_id: subscription_id
)
),
body: json_body,
content_type: 'application/json'
)
end

after do
FakeWeb.clean_registry
end

it 'returns a collection of allocations' do
result = Chargify::Allocation.bulk_create(
subscription_id: subscription_id,
allocations: allocations
)
expect(result.size).to eq 1
expect(result.first).to be_a Chargify::Allocation
expect(result.first.payment).to be_a Chargify::Allocation::Payment
end
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
config.after(:each, :fake_resource) do
ActiveResource::FakeResource.disable
end

config.before(:each) do
Chargify.configure {}
end
end

def test_domain
Expand Down