1 /**
  2  * @fileOverview Account Resource class definition
  3  */
  4 
  5 var Resource = require('./resource')
  6   ;
  7 
  8 /**
  9  * Represents an account in the spire api.
 10  *
 11  * @class Account resource
 12  *
 13  * @constructor
 14  * @extends Resource
 15  * @param spire {object} Spire object
 16  * @param data {object} Account data from the spire api
 17  */
 18 function Account(spire, data) {
 19   /**
 20    * Reference to spire object.
 21    */
 22   this.spire = spire;
 23 
 24   /**
 25    * Actual data from the spire.io api.
 26    */
 27   this.data = data;
 28 
 29   this.resourceName = 'account';
 30 }
 31 
 32 Account.prototype = new Resource();
 33 
 34 module.exports = Account;
 35 
 36 /**
 37  * Gets the account secret
 38  */
 39 Account.prototype.secret = function () {
 40   return this.data.secret;
 41 };
 42 
 43 /**
 44  * Resets the account
 45  *
 46  * Note that this passes a session to the callback, and not an account.
 47  * This is because many of the session urls will have changed.
 48  *
 49  * @param {function (err, session)} cb Callback
 50  */
 51 Account.prototype.reset = function (cb) {
 52   var account = this;
 53   this.request('reset', function (err, sessionData) {
 54     if (err) return cb(err);
 55     account.data = sessionData.resources.account;
 56     cb(null, sessionData);
 57   });
 58 };
 59 
 60 /**
 61  * Updates the billing plan for the account.
 62  *
 63  * @param {object} info New billing plan data
 64  * @param {function (err, account)} cb Callback
 65  */
 66 Account.prototype.updateBillingSubscription = function (info, cb) {
 67   var account = this;
 68   this.request('update_billing_subscription', info, function (err, data) {
 69     if (err) return cb(err);
 70     account.data = data;
 71     cb(null, account);
 72   });
 73 };
 74 
 75 /**
 76  * Requests
 77  * These define API calls and have no side effects.  They can be run by calling
 78  *     this.request(<request name>);
 79  */
 80 
 81 /**
 82  * Reset your account
 83  * @name reset
 84  * @ignore
 85  */
 86 Resource.defineRequest(Account.prototype, 'reset', function () {
 87   return {
 88     method: 'post',
 89     url: this.url(),
 90     headers: {
 91       'Accept': this.mediaType(),
 92       'Authorization': this.authorization('reset')
 93     }
 94   };
 95 });
 96 
 97 /**
 98  * Updates the billing subscription with the given data.
 99  * @name update_billing_subscription
100  * @ignore
101  */
102 Resource.defineRequest(Account.prototype, 'update_billing_subscription', function (info) {
103   var billing = this.data.billing;
104   return {
105     method: 'put',
106     url: billing.url,
107     content: info,
108     headers: {
109       'Accept': this.mediaType(),
110       'Content-Type': this.mediaType(),
111       'Authorization': this.authorization('update', billing)
112     }
113   };
114 });
115 
116 /**
117  * Gets the billing invoices for the account.
118  * @name billing_invoices
119  * @ignore
120  */
121 Resource.defineRequest(Account.prototype, 'billing_invoices', function () {
122   var invoices = this.data.billing.invoices;
123   return {
124     method: 'get',
125     url: invoices.url,
126     headers: {
127       'Accept': "application/json",
128       'Authorization': "Capability " + invoices.capability
129     }
130   };
131 });
132 
133 /**
134  * Gets the upcoming billing invoices for the account.
135  * @name billing_invoices_upcoming
136  * @ignore
137  */
138 Resource.defineRequest(Account.prototype, 'billing_invoices_upcoming', function () {
139   var upcoming = this.data.billing.invoices.upcoming;
140   return {
141     method: 'get',
142     url: upcoming,
143     headers: {
144       'Accept': "application/json",
145       'Authorization': "Capability " + upcoming.capability
146     }
147   };
148 });
149