Skip to content

Commit

Permalink
Complete refactoring for Version 2, basic structure in place
Browse files Browse the repository at this point in the history
  • Loading branch information
tadman committed Nov 7, 2016
1 parent 3696f33 commit aa3c504
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 226 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_js:
- "6"
- "5"
- "4"
env:
global:
secure: "yKi8fgJrq3E69I4XaiYi7gc39x+rQR7Nnhx4kTtlB1bwGvPeAHr4giSY2gGacIUXwHRU8lz1nIxAETn02cZMkwevA4kosB4yVKGnn2KAILnr6wvJyXPVKkkV29ls4QpYRf4yW++7hbEKsiN3BZsr4OczHf/dKi+rzzWTbuAen1A="
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2011-2016 Scott Tadman, Jon Lim, PostageApp
Copyright (c) 2011-2016 Scott Tadman, PostageApp

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
14 changes: 14 additions & 0 deletions bin/postageapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node

// == Imports ===============================================================

const PostageApp = require('../lib/postageapp');

// == Main ==================================================================

var program = require('commander');

var postageApp = new PostageApp();

console.log(postageApp.version);
console.log(postageApp.config());
1 change: 0 additions & 1 deletion index.js

This file was deleted.

36 changes: 36 additions & 0 deletions lib/api_call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// == Imports ===============================================================

const Promise = require('bluebird').Promise;
const uuid = require('uuid');

// == Exported Class ========================================================

class ApiCall {
constructor(method, args, uid, apiKey) {
this.method = method;
this.uid = uid || uuid.v4();
this.arguments = args || { };
this.apiKey = apiKey;
}

toJSON() {
return {
api_key: this.apiKey,
uid: this.uid,
method: this.method,
arguments: this.arguments
};
}

serialize(pretty) {
if (pretty) {
return JSON.stringify(this, null, 2);
}

return JSON.stringify(this);
}
}

// == Exports ===============================================================

module.exports = ApiCall;
73 changes: 73 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// == Imports ================================================================

const fs = require('fs');
const path = require('path');

// == Local Variables ========================================================

const defaults = {
host: 'api.postageapp.com',
port: 443,
secure: true,
apiKey: undefined
};

// == Initialization ========================================================

if (process.env.POSTAGEAPP_HOST) {
defaults.host = process.env.POSTAGEAPP_HOST;
}

if (process.env.POSTAGEAPP_PORT) {
defaults.port = process.env.POSTAGEAPP_PORT;
}

if (process.env.POSTAGEAPP_SECURE) {
defaults.secure = !!process.env.POSTAGEAPP_SECURE;
}

if (process.env.POSTAGEAPP_API_KEY) {
defaults.apiKey = process.env.POSTAGEAPP_API_KEY;
}

// == Exported Class ========================================================

class Config {
constructor(options) {
if (!options) {
options = process.env.POSTAGEAPP_CONFIG;
}

this.merge(defaults);

switch (typeof(options)) {
case 'string':
var configPath = path.resolve(process.cwd(), options);

if (fs.existsSync(configPath)) {8
this.merge(require(configPath));
}
else if (options.match(/^\w+$/)) {
this.apiKey = options;
}

break;
case 'object':
this.merge(options);
break;
case 'undefined':
// Work with defaults.
break;
}
}

merge(options) {
Object.keys(options).forEach((k) => {
this[k] = options[k];
});
}
}

// == Exports ===============================================================

module.exports = Config;
Loading

0 comments on commit aa3c504

Please sign in to comment.