serialize and order the plans

This commit is contained in:
Rimian Perkins 2019-11-01 10:18:57 +11:00
parent 5fe08c110f
commit 3dfa261c19
3 changed files with 23 additions and 4 deletions

View File

@ -10,8 +10,11 @@ module DiscoursePatrons
begin
plans = ::Stripe::Plan.list(active: true)
# TODO: Serialize. Remove some attributes like meta.group_name
render_json_dump plans.data
serialized = plans[:data].map do |plan|
plan.to_h.slice(:id, :amount, :currency, :interval)
end.sort_by { |plan| plan[:amount] }
render_json_dump serialized
rescue ::Stripe::InvalidRequestError => e
return render_json_error e.message

View File

@ -13,7 +13,5 @@ export default Ember.Helper.helper(function(params) {
currencySign = "$";
}
return currencySign + params.map(p => p.toUpperCase()).join(' ');
});

View File

@ -9,6 +9,24 @@ module DiscoursePatrons
::Stripe::Plan.expects(:list).with(active: true)
get "/patrons/plans.json"
end
it "orders and serialises the plans" do
::Stripe::Plan.expects(:list).returns({
data: [
{ id: 'plan_id123', amount: 1220, currency: 'aud', interval: 'year', metadata: {} },
{ id: 'plan_id234', amount: 1399, currency: 'usd', interval: 'year', metadata: {} },
{ id: 'plan_id678', amount: 1000, currency: 'aud', interval: 'week', metadata: {} }
]
})
get "/patrons/plans.json"
expect(JSON.parse(response.body)).to eq([
{ "amount" => 1000, "currency" => "aud", "id" => "plan_id678", "interval"=>"week" },
{ "amount" => 1220, "currency" => "aud", "id" => "plan_id123", "interval"=>"year" },
{ "amount" => 1399, "currency" => "usd", "id" => "plan_id234", "interval"=>"year" }
])
end
end
end
end