Skip to content

Commit

Permalink
Merge pull request #6 from AudienseCo/js2xml-fix
Browse files Browse the repository at this point in the history
Fixes the JS to XML converter
  • Loading branch information
IvanGuardado authored Aug 16, 2017
2 parents 1ad7a7e + 04c7ad9 commit 7e9e3b5
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 221 deletions.
8 changes: 8 additions & 0 deletions lib/jsToXml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var xml2js = require('xml2js');
var builder = new xml2js.Builder();

function jsToXml(input) {
return builder.buildObject(input);
}

module.exports = jsToXml;
36 changes: 18 additions & 18 deletions lib/recurly.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function(){
var Js2xml = require('js2xml').Js2Xml,
Client = require('./client'),
utils = require('./utils'),
router = require('./routes/');
var jsToXml = require('./jsToXml');
var Client = require('./client');
var utils = require('./utils');
var router = require('./routes/');

module.exports = function(config){
var routes = router.routes(config.API_VERSION);
Expand All @@ -17,10 +17,10 @@
t.request(utils.addParams(routes.accounts.get, {account_code: accountcode}), callback);
},
create: function(details, callback){
t.request(routes.accounts.create, callback, new Js2xml('account',details).toString());
t.request(routes.accounts.create, callback, jsToXml(details));
},
update: function(accountcode, details, callback){
t.request(utils.addParams(routes.accounts.update, {account_code: accountcode}), callback, new Js2xml('account', details).toString());
t.request(utils.addParams(routes.accounts.update, {account_code: accountcode}), callback, jsToXml(details));
},
close: function(accountcode, callback){
t.request(utils.addParams(routes.accounts.close, {account_code: accountcode}), callback);
Expand All @@ -35,7 +35,7 @@
t.request(utils.addParams(routes.adjustments.get, {account_code: accountcode}), callback);
},
create: function(accountcode, details, callback){
t.request(utils.addParams(routes.adjustments.create, {account_code: accountcode}), callback, new Js2xml('adjustment',details).toString());
t.request(utils.addParams(routes.adjustments.create, {account_code: accountcode}), callback, jsToXml(details));
},
remove: function(uuid, callback){
t.request(utils.addParams(routes.adjustments.remove, {uuid: uuid}), callback);
Expand All @@ -45,7 +45,7 @@
//http://docs.recurly.com/api/billing-info
this.billingInfo = {
update: function(accountcode, details, callback){
t.request(utils.addParams(routes.billingInfo.update, {account_code: accountcode} ), callback, new Js2xml('billing_info', details).toString());
t.request(utils.addParams(routes.billingInfo.update, {account_code: accountcode} ), callback, jsToXml(details));
},
get: function(accountcode, callback){
t.request(utils.addParams(routes.billingInfo.get, {account_code: accountcode} ), callback);
Expand All @@ -64,7 +64,7 @@
t.request(utils.addParams(routes.coupons.get, {coupon_code: couponcode}), callback);
},
create: function(details, callback){
t.request(routes.coupons.create, callback, new Js2xml('coupon', details).toString());
t.request(routes.coupons.create, callback, jsToXml(details));
},
deactivate: function(couponcode, callback){
t.request(utils.addParams(routes.coupons.deactivate, {coupon_code: couponcode}), callback);
Expand All @@ -73,7 +73,7 @@

this.couponRedemption = {
redeem: function(couponcode, details, callback){
t.request(utils.addParams(routes.couponRedemption.redeem, {coupon_code: couponcode}), callback, new Js2xml('redemption', details).toString());
t.request(utils.addParams(routes.couponRedemption.redeem, {coupon_code: couponcode}), callback, jsToXml(details));
},
get: function(accountcode, callback){
t.request(utils.addParams(routes.couponRedemption.get, {account_code: accountcode}), callback);
Expand Down Expand Up @@ -101,7 +101,7 @@
t.request(utils.addParams(routes.invoices.get, {invoice_number: invoicenumber}), callback);
},
create: function(accountcode, details, callback){
t.request(utils.addParams(routes.invoices.create, {account_code: accountcode}), callback, new Js2xml('invoice', details).toString());
t.request(utils.addParams(routes.invoices.create, {account_code: accountcode}), callback, jsToXml(details));
},
markSuccessful: function(invoicenumber, callback){
t.request(utils.addParams(routes.invoices.markSuccessful, {invoice_number: invoicenumber}), callback);
Expand All @@ -125,10 +125,10 @@
t.request(utils.addParams(routes.plans.get, {plan_code: plancode}), callback);
},
create: function(details, callback){
t.request(routes.plans.create, callback, new Js2xml('plan', details).toString());
t.request(routes.plans.create, callback, jsToXml(details));
},
update: function(plancode, details, callback){
t.request(utils.addParams(routes.plans.update, {plan_code: plancode}), callback, new Js2xml('plan', details).toString());
t.request(utils.addParams(routes.plans.update, {plan_code: plancode}), callback, jsToXml(details));
},
remove: function(plancode, callback){
t.request(utils.addParams(routes.plans.remove, {plan_code: plancode}), callback);
Expand All @@ -143,15 +143,15 @@
t.request(utils.addParams(routes.planAddons.get, {plan_code: plancode, addon_code: addoncode}), callback);
},
create: function(plancode, details, callback){
t.request(utils.addParams(routes.planAddons.create, {plan_code: plancode}), callback, new Js2xml('add_on', details).toString());
t.request(utils.addParams(routes.planAddons.create, {plan_code: plancode}), callback, jsToXml(details));
},
update: function(plancode, addoncode, details, callback){
t.request(utils.addParams(
routes.planAddons.update,
{ plan_code: plancode,
add_on_code: addoncode
}),
callback, new Js2xml('add_on', details).toString());
callback, jsToXml(details));
},
remove: function(plancode, addoncode, callback){
t.request(utils.addParams(routes.planAddons.remove, {plan_code: plancode, add_on_code: addoncode}), callback);
Expand All @@ -170,10 +170,10 @@
t.request(utils.addParams(routes.subscriptions.get, {uuid: uuid}), callback);
},
create: function(details, callback){
t.request(routes.subscriptions.create, callback, new Js2xml('subscription', details).toString());
t.request(routes.subscriptions.create, callback, jsToXml(details));
},
update: function(uuid, details, callback){
t.request(utils.addParams(routes.subscriptions.update, {uuid: uuid}), callback, new Js2xml('subscription', details).toString());
t.request(utils.addParams(routes.subscriptions.update, {uuid: uuid}), callback, jsToXml(details));
},
cancel: function(uuid, callback){
t.request(utils.addParams(routes.subscriptions.cancel, {uuid: uuid}), callback);
Expand Down Expand Up @@ -203,7 +203,7 @@
t.request(utils.addParams(routes.transactions.get, {'id': id}), callback);
},
create: function(details, callback){
t.request(routes.transactions.create, callback, new Js2xml('transaction', details).toString());
t.request(routes.transactions.create, callback, jsToXml(details));
},
refund: function(id, callback, amount){
var route = utils.addParams(routes.transactions.refund, {id: id});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "name" : "node-recurly"
, "description" : "Library for accessing the api for the Recurly recurring billing service."
, "keywords" : [ "recurly", "e-commerce", "recurring billing" ]
, "version" : "2.0.2"
, "version" : "2.1.0"
, "homepage" : "https://github.com/robrighter/node-recurly"
, "author" : "Rob Righter <robrighter@gmail.com> (http://github.com/robrighter)"
, "contributors" : [
Expand Down
9 changes: 0 additions & 9 deletions test/config-example.js

This file was deleted.

218 changes: 218 additions & 0 deletions test/jsToXml_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
'use strict';

const assert = require('assert');
const jsToXml = require('../lib/jsToXml');

describe('JS to XML converter', () => {

it('should build an entire object correctly', () => {
const output = jsToXml(input());
assert.equal(output, expected());
});

const input = () => ({
"subscription": {
"$": {
"href": "https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4"
},
"account": {
"$": {
"href": "https://api.recurly.com/v2/accounts/123"
}
},
"invoice": {
"$": {
"href": "https://api.recurly.com/v2/invoices/2594"
}
},
"plan": {
"$": {
"href": "https://api.recurly.com/v2/plans/insights_pro"
},
"plan_code": "insights_pro",
"name": "Insights Pro"
},
"uuid": "3f406bb050dd05ad0e8db14b22b449b4",
"state": "active",
"unit_amount_in_cents": {
"_": "19900",
"$": {
"type": "integer"
}
},
"currency": "USD",
"quantity": {
"_": "1",
"$": {
"type": "integer"
}
},
"activated_at": {
"_": "2017-08-11T14:08:59Z",
"$": {
"type": "datetime"
}
},
"canceled_at": {
"$": {
"nil": "nil"
}
},
"expires_at": {
"$": {
"nil": "nil"
}
},
"total_billing_cycles": {
"$": {
"nil": "nil"
}
},
"remaining_billing_cycles": {
"$": {
"nil": "nil"
}
},
"current_period_started_at": {
"_": "2017-08-11T14:08:59Z",
"$": {
"type": "datetime"
}
},
"current_period_ends_at": {
"_": "2017-09-11T14:08:59Z",
"$": {
"type": "datetime"
}
},
"trial_started_at": {
"$": {
"nil": "nil"
}
},
"trial_ends_at": {
"$": {
"nil": "nil"
}
},
"terms_and_conditions": "",
"customer_notes": "",
"tax_in_cents": {
"_": "9429",
"$": {
"type": "integer"
}
},
"tax_type": "vat",
"tax_region": "ES",
"tax_rate": {
"_": "0.21",
"$": {
"type": "float"
}
},
"po_number": "",
"net_terms": {
"_": "0",
"$": {
"type": "integer"
}
},
"collection_method": "automatic",
"subscription_add_ons": {
"$": {
"type": "array"
},
"subscription_add_on": {
"add_on_code": "consumer-insights_pro",
"unit_amount_in_cents": {
"_": "25000",
"$": {
"type": "integer"
}
},
"quantity": {
"_": "1",
"$": {
"type": "integer"
}
}
}
},
"a": [
{
"$": {
"name": "cancel",
"href": "https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/cancel",
"method": "put"
}
},
{
"$": {
"name": "terminate",
"href": "https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/terminate",
"method": "put"
}
},
{
"$": {
"name": "postpone",
"href": "https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/postpone",
"method": "put"
}
},
{
"$": {
"name": "notes",
"href": "https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/notes",
"method": "put"
}
}
]
}
});

const expected = () => `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<subscription href="https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4">
<account href="https://api.recurly.com/v2/accounts/123"/>
<invoice href="https://api.recurly.com/v2/invoices/2594"/>
<plan href="https://api.recurly.com/v2/plans/insights_pro">
<plan_code>insights_pro</plan_code>
<name>Insights Pro</name>
</plan>
<uuid>3f406bb050dd05ad0e8db14b22b449b4</uuid>
<state>active</state>
<unit_amount_in_cents type="integer">19900</unit_amount_in_cents>
<currency>USD</currency>
<quantity type="integer">1</quantity>
<activated_at type="datetime">2017-08-11T14:08:59Z</activated_at>
<canceled_at nil="nil"/>
<expires_at nil="nil"/>
<total_billing_cycles nil="nil"/>
<remaining_billing_cycles nil="nil"/>
<current_period_started_at type="datetime">2017-08-11T14:08:59Z</current_period_started_at>
<current_period_ends_at type="datetime">2017-09-11T14:08:59Z</current_period_ends_at>
<trial_started_at nil="nil"/>
<trial_ends_at nil="nil"/>
<terms_and_conditions/>
<customer_notes/>
<tax_in_cents type="integer">9429</tax_in_cents>
<tax_type>vat</tax_type>
<tax_region>ES</tax_region>
<tax_rate type="float">0.21</tax_rate>
<po_number/>
<net_terms type="integer">0</net_terms>
<collection_method>automatic</collection_method>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_code>consumer-insights_pro</add_on_code>
<unit_amount_in_cents type="integer">25000</unit_amount_in_cents>
<quantity type="integer">1</quantity>
</subscription_add_on>
</subscription_add_ons>
<a name="cancel" href="https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/cancel" method="put"/>
<a name="terminate" href="https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/terminate" method="put"/>
<a name="postpone" href="https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/postpone" method="put"/>
<a name="notes" href="https://api.recurly.com/v2/subscriptions/3f406bb050dd05ad0e8db14b22b449b4/notes" method="put"/>
</subscription>`
});
Loading

0 comments on commit 7e9e3b5

Please sign in to comment.