angular.module( 'countryCapitalsAppModel', [ 'geonamesLibrary' ] )

// getCapitalInfo( countryObject )
// extend a country with additional information about the capital
.factory( 'countryCapitalsModel', [ 'geonames', 'SEARCH_CODE_CAPITAL',
                           function( geonames,   SEARCH_CODE_CAPITAL ) {

	return ({
		listCountries: function() {
			return geonames.listCountries();
		},
		getCountry: function( countryCode ) {
			return geonames.getCountry( countryCode ).then(function( country ) {
				// prep for extending capital info
				if ( typeof country === 'object' ) {
					country.capital = { name: country.capital };
				}
				return country;
			});
		},
		extendCountryInfoWithCapital: function( country ) {
			return geonames.search( country.capital.name, SEARCH_CODE_CAPITAL ).then(function( data ) {
				// parse lat/long as floats
				data.geonames[ 0 ].lat = parseFloat( data.geonames[ 0 ].lat );
				data.geonames[ 0 ].lng = parseFloat( data.geonames[ 0 ].lng );
				angular.extend( country.capital, data.geonames[ 0 ]);
			});
		},
		getCountryNeighbours: function( country ) {
			return geonames.listNeighbours( country );
		}
	});
}])
;

@@ -232,16 +232,22 @@
 APITAL',
+ '$q',
 %0A       
@@ -307,16 +307,22 @@
 _CAPITAL
+,   $q
  ) %7B%0A%0A%09r
@@ -588,16 +588,35 @@
 'object'
+ && country.capital
  ) %7B%0A%09%09%09
@@ -700,16 +700,16 @@
 );%0A%09%09%7D,%0A
-
 %09%09extend
@@ -746,32 +746,98 @@
 on( country ) %7B%0A
+%09%09%09if ( ! country.capital ) %7B%0A%09%09%09%09return $q.defer().promise;%0A%09%09%09%7D%0A
 %09%09%09return geonam

handle missing capital (fixes #3)