Commit 63c2e0d

mo khan <mo@mokhan.ca>
2016-12-24 20:02:15
fix api resolution.
1 parent 9430256
app/boot/__tests__/wire-up-components-into_spec.js
@@ -1,6 +1,7 @@
-import WireUpComponentsInto from '../wire-up-components-into';
-import Registry from '../../infrastructure/registry';
 import * as commands from '../../services/commands';
+import Api from '../../infrastructure/api';
+import Registry from '../../infrastructure/registry';
+import WireUpComponentsInto from '../wire-up-components-into';
 
 describe("WireUpComponentsInto", () => {
   let subject = null;
@@ -12,11 +13,18 @@ describe("WireUpComponentsInto", () => {
   });
 
   describe("#run", () => {
-    it ("registers each command", function() {
+    beforeEach(() => {
       subject.run()
+    });
+
+    it ("registers each command", function() {
+      results = registry.resolveAll('subscriber');
+      expect(results.length).toEqual(3);
+    });
 
-      results = registry.resolveAll('subscriber')
-      expect(results.length).toEqual(3)
+    it ("can build the api", function() {
+      result = registry.resolve('api');
+      expect(result).toBeInstanceOf(Api);
     });
   });
 })
app/boot/wire-up-components-into.js
@@ -2,7 +2,7 @@ import * as commands from '../services/commands';
 import * as queries from '../services/queries';
 import Api from '../infrastructure/api';
 import ApplicationStorage from '../infrastructure/application-storage';
-import Config from 'react-native-config';
+import Configuration from '../infrastructure/configuration';
 import EventAggregator from '../infrastructure/event-aggregator';
 import Registry from '../infrastructure/registry';
 import Router from '../infrastructure/router'
@@ -20,21 +20,25 @@ export default class WireUpComponentsInto {
       });
     }).asSingleton();
     this.registry.register('applicationStorage', ApplicationStorage).asSingleton();
-    let host = Config.API_HOST;
-    this.registry.register('sessionsApi', (container) => new Api(host, '/sessions', container.resolve('applicationStorage'))).asSingleton();
-    this.registry.register('workoutsApi', (container) => new Api(host, '/workouts', container.resolve('applicationStorage'))).asSingleton();
+    this.registry.register('configuration', () => {
+      return new Configuration('development');
+    }).asSingleton();
+    this.registry.register('api', Api).asSingleton();
+
     this.registerSubscribers(commands);
     this.registerSubscribers(queries);
+
+    this.registry.resolveAll("subscriber").forEach((subscriber) => {
+      console.log(`subscribing: ${subscriber}`);
+      subscriber.subscribeTo(this.registry.resolve('eventAggregator'));
+    });
     return this.registry;
   }
 
   registerSubscribers(subscribers) {
     for (let subscriber in subscribers) {
-      //console.log(`registering: ${subscriber}`);
+      console.log(`registering: ${subscriber}`);
       this.registry.register('subscriber', subscribers[subscriber]).asSingleton();
     }
-    this.registry.resolveAll("subscriber").forEach((subscriber) => {
-      subscriber.subscribeTo(this.registry.resolve('eventAggregator'));
-    });
   }
 }
app/infrastructure/__tests__/configuration_spec.js
@@ -0,0 +1,15 @@
+import Configuration from '../configuration';
+
+describe("Configuration", ()=> {
+  let subject = null;
+
+  beforeEach(()=> {
+    subject = new Configuration('development');
+  });
+
+  it ("loads the specified configuration", function() {
+    let apiHost='http://192.168.128.6:3000'
+
+    expect(subject.value_for("API_HOST")).toEqual(apiHost)
+  });
+});
app/infrastructure/api.js
@@ -1,13 +1,14 @@
 export default class Api {
-  constructor(host, url, applicationStorage) {
-    this.url = `${host}/api${url}`
+  constructor(configuration, applicationStorage) {
+    this.configuration = configuration;
     this.storage = applicationStorage;
   }
 
-  get(success) {
+  get(relativeUrl, success) {
+    let url = this.buildUrlFor(relativeUrl);
     this.defaultHeaders((headers) => {
-      console.log(`GET ${this.url}`);
-      fetch(this.url, { method: 'GET', headers: headers })
+      console.log(`GET ${url}`);
+      fetch(url, { method: 'GET', headers: headers })
         .then((response) => response.json())
         .then((json) => success(json))
         .catch((error) => console.error(error))
@@ -15,10 +16,11 @@ export default class Api {
     });
   }
 
-  post(body, success) {
+  post(relativeUrl, body, success) {
+    let url = this.buildUrlFor(relativeUrl);
     const jsonBody = JSON.stringify(body);
-    console.log(`POST ${this.url}`);
-    fetch(this.url, {
+    console.log(`POST ${url}`);
+    fetch(url, {
       method: "POST",
       headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
       body: jsonBody
@@ -42,5 +44,11 @@ export default class Api {
       .catch((error) => console.error(error))
       .done();
   }
+
+  buildUrlFor(url) {
+    //let host = this.configuration.value_for('API_HOST');
+    let host = 'https://localhost:3000';
+    return `${host}/api${url}`
+  }
 }
 
app/infrastructure/configuration.js
@@ -0,0 +1,11 @@
+import Config from 'react-native-config';
+
+export default class Configuration {
+  constructor(environment) {
+    this.environment = environment;
+  }
+
+  value_for(key) {
+    return Config[key];
+  }
+}
app/infrastructure/registry.js
@@ -12,9 +12,9 @@ export class Resolver {
     }
   }
 
-  parseConstructor(item) {
-    let code = item.toString();
-    let regex = /function ([a-zA-Z]*)\((.*)\)\{/;
+  parseConstructor(func) {
+    let code = func.toString();
+    let regex = /function ([a-zA-Z]*)\((.*)\) *\{/;
     return code.match(regex);
   }
 
@@ -66,7 +66,13 @@ export default class Registry {
   }
 
   resolve(key) {
-    return this.registrations[key][0].create(this);
+    try {
+      return this.registrations[key][0].create(this);
+    } catch(error) {
+      console.error(`ERROR: Could Not Resolve ${key}`);
+      console.error(error);
+      throw error;
+    }
   }
 
   resolveAll(key) {
app/services/commands/login-command.js
@@ -1,9 +1,9 @@
 import * as events from '../events';
 
 export default class LoginCommand {
-  constructor(eventAggregator, sessionsApi, applicationStorage) {
+  constructor(eventAggregator, api, applicationStorage) {
     this.eventAggregator = eventAggregator;
-    this.api = sessionsApi;
+    this.api = api;
     this.applicationStorage = applicationStorage;
   }
 
@@ -13,7 +13,7 @@ export default class LoginCommand {
 
   notify(event) {
     let body = { username: event.username, password: event.password };
-    this.api.post(body, this.onResponse.bind(this));
+    this.api.post('/sessions', body, this.onResponse.bind(this));
   }
 
   onResponse(json) {
app/services/queries/fetch-workouts.js
@@ -1,9 +1,9 @@
 import * as events from '../events';
 
 export default class FetchWorkouts {
-  constructor(eventAggregator, workoutsApi) {
+  constructor(eventAggregator, api) {
     this.eventAggregator = eventAggregator;
-    this.api = workoutsApi;
+    this.api = api;
   }
 
   subscribeTo(eventAggregator) {
@@ -11,7 +11,7 @@ export default class FetchWorkouts {
   }
 
   notify(event) {
-    this.api.get(this.onResponse.bind(this));
+    this.api.get('/workouts', this.onResponse.bind(this));
   }
 
   onResponse(json) {