and obtain an API key
box coldbox create module name=cfYNAB
cd modules_app/cfYNAB
box install hyper
# .env
# YNAB API credentials
YNAB_URL=https://api.youneedabudget.com/v1/
# See https://api.youneedabudget.com/#personal-access-tokens
YNAB_ACCESS_TOKEN=XYZ_FIXME
# .gitignore
# Ignore all SECRET stuff
.env
box install commandbox-dotenv
or
var envFile = expandPath( ".env" );
if ( fileExists( envFile ) ){
var props = CreateObject( "java", "java.util.Properties" ).init();
props.load( CreateObject( "java", "java.io.FileInputStream" ).init( envFile ) );
var availableProps = props.propertyNames();
while( availableProps.hasNext() ){
var propName = availableProps.next();
javaSystem.setProperty( propName, props.getProperty( propName ) );
}
}
store as environment variables
// ModuleConfig.cfc
function configure(){
settings = {
api_url: getSystemSetting( "YNAB_URL", "https://api.youneedabudget.com/v1/" ),
// See https://api.youneedabudget.com/#personal-access-tokens
personal_access_token: getSystemSetting( "YNAB_ACCESS_TOKEN" )
};
}
via ModuleConfig.cfc
// ModuleConfig.cfc
function configure(){
// ...
binder.map( "YNABClient" )
.to( "hyper.models.HyperBuilder" )
.asSingleton();
}
// ModuleConfig.cfc
function configure() {
// ...
binder.map( "YNABClient" )
.to( "hyper.models.HyperBuilder" )
.asSingleton()
.initWith(
baseUrl = settings.api_url,
headers = {
"Authorization" = "Bearer #settings.personal_access_token#"
}
);
}
/**
* I model all YNAB transaction requests
*/
component {
property name="YNAB" inject="YNABClient";
// ...
}
Prefer:
Avoid:
// models/Budgets.cfc
/**
* I handle YNAB API requests for the /budgets endpoint.
*
* @url https://api.youneedabudget.com/v1#/Budgets/getBudgets
*/
component {
property name="YNAB" inject="YNABClient";
/**
* Constructor
*/
public component function init(){
return this;
}
}
function getBudgets(){
return YNABClient.get( "/budgets" );
}
function getBudgets( boolean include_accounts ){
var queryParams = {};
if ( !isNull( arguments.include_accounts ) ){
queryParams[ "include_accounts" ] = arguments.include_accounts;
}
return YNABClient.get( "/budgets", queryParams );
}
function getBudgets(){
return YNABClient.get( "/budgets", arguments );
}
/**
* Retrieve YNAB budgets from YNAB API
*
* @include_accounts true to return array of budget accounts with each budget entry
*
* @see https://api.youneedabudget.com/v1#/Budgets/getBudgets
*/
function getBudgets( boolean include_accounts ){
var queryParams = {};
if ( !isNull( arguments.include_accounts ) ){
queryParams[ "include_accounts" ] = arguments.include_accounts;
}
return YNABClient.get( "/budgets", queryParams );
}
// handlers/YNAB.cfc
/**
* Manage YNAB connection
*/
component {
property name="Budgets" inject="Budgets@cfYNAB";
/**
* Show YNAB budgets
*/
function index( event, rc, prc ){
prc.data = budgets.getBudgets();
event.setView( "account/myBudget" );
}
}
The HyperResponse makes it easy to