Skip to content

Commit 2224119

Browse files
author
Nicholas Thomson
committed
Update and improve the api faker
1 parent 608d1fe commit 2224119

File tree

3 files changed

+112
-79
lines changed

3 files changed

+112
-79
lines changed

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<script src="vendor/leaflet.markerclusterer/dist/leaflet.markercluster.js"></script>
5555
<script src="vendor/ngInfiniteScroll/ng-infinite-scroll.js"></script>
5656
<script src="vendor/crossfilter/crossfilter.js"></script>
57+
<script src='vendor/Faker/Faker.js'></script>
5758
<!-- endbuild -->
5859

5960
<!-- build:js({.tmp,app}) scripts/scripts.js -->

app/scripts/lib/webApiFaker.js

Lines changed: 109 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,130 @@
11
'use strict';
22

3-
(function (ng) {
4-
var injector = ng.injector(['configuration', 'ng']);
5-
var environment = injector.get('ENV');
3+
var rnd_bmt = function() {
4+
var x = 0, y = 0, rds, c;
5+
do {
6+
x = Math.random()*2-1;
7+
y = Math.random()*2-1;
8+
rds = x*x + y*y;
9+
}
10+
while (rds === 0 || rds > 1);
11+
c = Math.sqrt(-2*Math.log(rds)/rds);
12+
return [x*c, y*c];
13+
};
614

7-
var generateInvestors = function(investorCount) {
8-
var investorList = [];
15+
var distributed_random = function(min, max) { return Math.floor(rnd_bmt()[0] * (max - min) + min); };
916

10-
for(var i = 0; i < investorCount; i++) {
11-
investorList.push({
12-
id: i,
13-
name: 'investor' + i,
14-
invested_company_ids: [],
15-
invested_category_ids: []
16-
});
17-
}
18-
19-
return investorList;
20-
};
2117

22-
var generateCategories = function(categoryCount) {
23-
var categoryList = [];
24-
25-
for(var i = 0; i < categoryCount; i++) {
26-
categoryList.push({
27-
id: i,
28-
name: 'category' + i,
29-
company_ids: [],
30-
investor_ids: []
31-
});
32-
}
18+
(function (ng, fk) {
19+
var injector = ng.injector(['configuration', 'ng']);
20+
var environment = injector.get('ENV');
3321

34-
return categoryList;
22+
/**
23+
* Generate a company object with random, usable attributes.
24+
*
25+
* @param {number} [id] Not required. Define an ID for the returned company.
26+
* @return {object} A company with randomly generated data values.
27+
*/
28+
var randomCompany = function(id) {
29+
var name = fk.Company.companyName();
30+
id = id || 0;
31+
return {
32+
id: id,
33+
name: name,
34+
permalink: name.toLowerCase(),
35+
category_id: 0,
36+
total_funding: distributed_random(1, 6e9), //Random between 1 and 6 billion
37+
latitude: 1.0,
38+
longitude: 1.0,
39+
investor_ids: [],
40+
funding_rounds: []
41+
};
3542
};
3643

37-
var generateCompanies = function(categories, investors, companyCount) {
38-
var getRandomInRange = function(from, to, fixed) {
39-
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
44+
/**
45+
* Generate an investor object with random, usable attributes.
46+
*
47+
* @param {number} [id] Not required. Define an ID for the returned investor.
48+
* @return {object} An investor with randomly generated data values.
49+
*/
50+
var randomInvestor = function(id) {
51+
var type = Math.random() < 0.5 ? 'company' : 'person';
52+
var name = type === 'company' ? fk.Company.companyName() : fk.Name.findName();
53+
id = id || 0;
54+
return {
55+
id: id,
56+
name: name,
57+
investor_type: type,
58+
permalink: name.toLowerCase(),
59+
invested_company_ids: [],
60+
invested_category_ids: []
4061
};
41-
var companyList = [];
42-
43-
for(var i = 0; i < companyCount; i++) {
44-
var company = {};
45-
var associationLimit = Math.floor(Math.random()*10);
46-
company.id = i;
47-
company.name = 'company' + i;
48-
company.zip_code = Math.floor(Math.random()*90000) + 10000;
49-
company.total_funding = Math.floor(Math.random()*9900000) + 100000;
50-
company.latitude = getRandomInRange(22, 49, 3);
51-
company.longitude = getRandomInRange(-124, -66, 3);
52-
var category = categories[Math.floor(Math.random()*categories.length)];
53-
company.category_code = category;
54-
55-
if(category.company_ids.indexOf(company.id) === -1) {
56-
category.company_ids.push(company.id);
57-
}
58-
59-
company.investor_ids = [];
60-
company.funding_rounds = [];
61-
62-
for(var j = 0; j < associationLimit; j++) {
63-
var fundingRound = {};
64-
fundingRound.id = j;
65-
fundingRound.raised_amount = '$1000';
66-
fundingRound.funded_on = '2013-01-01';
67-
68-
for(var k = 0; k < associationLimit; k++) {
69-
var investor = investors[Math.floor(Math.random()*investors.length)];
70-
investor.invested_company_ids.push(company.id);
71-
investor.invested_category_ids.push(category.id);
62+
};
7263

73-
if(category.investor_ids.indexOf(investor.id) === -1) {
74-
category.investor_ids.push(investor.id);
75-
}
64+
/**
65+
* Generate a category object with random, usable attributes.
66+
*
67+
* @param {number} [id] Not required. Define an ID for the returned category.
68+
* @return {object} A category with randomly generated data values.
69+
*/
70+
var randomCategory = function(id) {
71+
id = id || 0;
72+
return {
73+
id: id,
74+
name: fk.random.bs_buzz(),
75+
permalink: name.toLowerCase(),
76+
company_ids: [],
77+
investor_ids: []
78+
};
79+
};
7680

77-
if(company.investor_ids.indexOf(investor.id) === -1) {
78-
company.investor_ids.push(investor.id);
79-
}
80-
}
81-
company.funding_rounds.push(fundingRound);
82-
}
83-
companyList.push(company);
81+
/**
82+
* Generate a list of data based on a supplied function
83+
*
84+
* @param {number} [count] How many items should be returned in the data list
85+
* @param {function} [genFn] A function that when called returns an instance of a single item in the list
86+
* @return {array} A list of data generated from the supplied function
87+
*/
88+
var generateDataList = function(count, genFn) {
89+
var generated = [];
90+
for(var i = 0; i < count; i++){
91+
generated.push(genFn(i));
8492
}
93+
return generated;
94+
};
8595

86-
return companyList;
96+
/**
97+
* Link a generated list of data together
98+
*
99+
* @param {array} [companies] An unlinked list of companies
100+
* @param {array} [investors] An unlinked list of investors
101+
* @param {array} [categories] An unlinked list of categories
102+
*/
103+
var linkGeneratedLists = function(companies, investors, categories) {
104+
_.each(companies, function(company){
105+
var category = categories[Math.floor(Math.random()*categories.length)];
106+
company.category_id = category.id;
107+
category.company_ids.push(company.id);
108+
109+
_(Math.floor(Math.random() * (1) + 24)).times(function(){
110+
var investor = investors[Math.floor(Math.random()*investors.length)];
111+
company.investor_ids.push(investor.id);
112+
category.investor_ids.push(investor.id);
113+
investor.invested_company_ids.push(company.id);
114+
investor.invested_category_ids.push(company.category_id);
115+
});
116+
});
87117
};
88118

89119
var setupStubbedBackend = function() {
90-
var investors = generateInvestors(20);
91-
var categories = generateCategories(10);
92-
var companies = generateCompanies(categories, investors, 500);
120+
var categories = generateDataList(10, randomCategory);
121+
var investors = generateDataList(10000, randomInvestor);
122+
var companies = generateDataList(15000, randomCompany);
123+
linkGeneratedLists(companies, investors, categories);
93124

94125
ng.module('crunchinatorApp')
95126
.config(['$provide', function($provide) {
96-
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
127+
$provide.decorator('$httpBackend', ng.mock.e2e.$httpBackendDecorator);
97128
}]).run(['$httpBackend', function($httpBackend) {
98129
$httpBackend.when('GET', '/companies.json').respond({ companies: companies });
99130
$httpBackend.when('GET', '/categories.json').respond({ categories: categories });
@@ -119,4 +150,4 @@
119150
}
120151

121152
ng.module('crunchinatorApp.models').constant('API_BASE_URL', base_url);
122-
})(angular);
153+
})(angular, window.Faker);

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"leaflet.markerclusterer": "*",
1616
"crossfilter": "~1.3.7",
1717
"ngInfiniteScroll": "*",
18-
"d3-cloud": "*"
18+
"d3-cloud": "*",
19+
"Faker": "*"
1920
},
2021
"devDependencies": {
2122
"angular-mocks": "~1.2.0",

0 commit comments

Comments
 (0)