Skip to content

Commit 493515e

Browse files
author
Nicholas Thomson
committed
Merge conflicts
2 parents bdc8b93 + 106b0dd commit 493515e

File tree

5 files changed

+143
-82
lines changed

5 files changed

+143
-82
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ $ grunt serve
1818
```
1919
---
2020

21+
## Changing the API
22+
23+
You should be using the faker for local development, not staging data.
24+
25+
If you need to a make a change to the API, complete the following steps:
26+
1. Update webApiFaker to produce the type of output that you expect.
27+
1. In the same commit, update the JSON in the README to match what webApiFaker now returns
28+
1. Add a ticket to pivotal tracker assigned to the backend team and labeled with 'api'
29+
1. Include a link to the README (and a diff showing the changes) with the version of the API you need
30+
1. When the API team finishes the change and marks the ticket as 'delivered' determine if the output matches your expectations (check this manually and/or by running grunt serve:staging)
31+
1. If the output is as expected, accept the ticket
32+
1. If the output is incorrect, reject the ticket
33+
34+
Once the API has been successfully updated, any code that relies on the new data can be merged to master.
35+
2136
## Endpoint Schemas
2237

2338
### Companies
@@ -29,7 +44,7 @@ $ grunt serve
2944
"name": "company1",
3045
"permalink": "company-1",
3146
"category_id": 15,
32-
"total_funding": 1000.0,
47+
"total_funding": 1000,
3348
"latitude": 1.0,
3449
"longitude": 1.0,
3550
"investor_ids":
@@ -94,4 +109,4 @@ $ grunt serve
94109
]
95110
}
96111
]
97-
}
112+
}

app/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ <h2>Crunchbase search and visualization built using Angular.js and D3.</h2>
5858
<script src="vendor/ngInfiniteScroll/ng-infinite-scroll.js"></script>
5959
<script src="vendor/crossfilter/crossfilter.js"></script>
6060
<script src="vendor/less.js/dist/less-1.6.1.js"></script>
61+
<script src='vendor/Faker/Faker.js'></script>
6162
<!-- endbuild -->
6263

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

app/scripts/lib/webApiFaker.js

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

3-
(function (ng) {
4-
var injector = ng.injector(['configuration', 'ng']);
5-
var environment = injector.get('ENV');
3+
var normal_distribution = function() {
4+
return (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);
5+
};
6+
var exponential_distribution = function(min, max) {
7+
var increment = (max - min) / 6;
8+
var num;
9+
do {
10+
var u = Math.random();
11+
var t = (-1 * Math.log(u))/1;
12+
num = min + (t * increment);
13+
}
14+
while(num <= min || num >= max);
15+
return Math.floor(num);
16+
};
17+
var distributed_random = function(min, max) {
18+
var mean = (min + max)/2;
19+
var deviation = mean/3;
20+
var num;
21+
do {
22+
num = Math.floor((normal_distribution() * deviation) + mean);
23+
}
24+
while(num <= min || num >= max);
25+
return num;
26+
};
627

7-
var generateInvestors = function(investorCount) {
8-
var investorList = [];
928

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-
}
29+
(function (ng, fk) {
30+
var injector = ng.injector(['configuration', 'ng']);
31+
var environment = injector.get('ENV');
1832

19-
return investorList;
33+
/**
34+
* Generate a company object with random, usable attributes.
35+
*
36+
* @param {number} [id] Not required. Define an ID for the returned company.
37+
* @return {object} A company with randomly generated data values.
38+
*/
39+
var randomCompany = function(id) {
40+
var name = fk.Company.companyName();
41+
id = id || 0;
42+
return {
43+
id: id,
44+
name: name,
45+
permalink: name.toLowerCase(),
46+
category_id: 0,
47+
total_funding: distributed_random(1, 6e9), //Random between 1 and 6 billion
48+
latitude: 1.0,
49+
longitude: 1.0,
50+
investor_ids: [],
51+
funding_rounds: []
52+
};
2053
};
2154

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-
}
33-
34-
return categoryList;
55+
/**
56+
* Generate an investor object with random, usable attributes.
57+
*
58+
* @param {number} [id] Not required. Define an ID for the returned investor.
59+
* @return {object} An investor with randomly generated data values.
60+
*/
61+
var randomInvestor = function(id) {
62+
var type = Math.random() < 0.5 ? 'company' : 'person';
63+
var name = type === 'company' ? fk.Company.companyName() : fk.Name.findName();
64+
id = id || 0;
65+
return {
66+
id: id,
67+
name: name,
68+
investor_type: type,
69+
permalink: name.toLowerCase(),
70+
invested_company_ids: [],
71+
invested_category_ids: []
72+
};
3573
};
3674

37-
var generateCompanies = function(categories, investors, companyCount) {
38-
var getRandomInRange = function(from, to, fixed) {
39-
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
75+
/**
76+
* Generate a category object with random, usable attributes.
77+
*
78+
* @param {number} [id] Not required. Define an ID for the returned category.
79+
* @return {object} A category with randomly generated data values.
80+
*/
81+
var randomCategory = function(id) {
82+
id = id || 0;
83+
return {
84+
id: id,
85+
name: fk.random.bs_noun(),
86+
permalink: name.toLowerCase(),
87+
company_ids: [],
88+
investor_ids: []
4089
};
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);
72-
73-
if(category.investor_ids.indexOf(investor.id) === -1) {
74-
category.investor_ids.push(investor.id);
75-
}
90+
};
7691

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);
92+
/**
93+
* Generate a list of data based on a supplied function
94+
*
95+
* @param {number} [count] How many items should be returned in the data list
96+
* @param {function} [genFn] A function that when called returns an instance of a single item in the list
97+
* @return {array} A list of data generated from the supplied function
98+
*/
99+
var generateDataList = function(count, genFn) {
100+
var generated = [];
101+
for(var i = 0; i < count; i++){
102+
generated.push(genFn(i));
84103
}
104+
return generated;
105+
};
85106

86-
return companyList;
107+
/**
108+
* Link a generated list of data together
109+
*
110+
* @param {array} [companies] An unlinked list of companies
111+
* @param {array} [investors] An unlinked list of investors
112+
* @param {array} [categories] An unlinked list of categories
113+
*/
114+
var linkGeneratedLists = function(companies, investors, categories) {
115+
_.each(companies, function(company){
116+
var category = categories[exponential_distribution(0, categories.length)];
117+
company.category_id = category.id;
118+
category.company_ids.push(company.id);
119+
120+
_(exponential_distribution(1, 10)).times(function(){
121+
var investor = investors[exponential_distribution(0, investors.length)];
122+
company.investor_ids.push(investor.id);
123+
category.investor_ids.push(investor.id);
124+
investor.invested_company_ids.push(company.id);
125+
investor.invested_category_ids.push(company.category_id);
126+
});
127+
});
87128
};
88129

130+
/**
131+
* Initiate and respond with fake backend data instead of querying an actual API
132+
*/
89133
var setupStubbedBackend = function() {
90-
var investors = generateInvestors(20);
91-
var categories = generateCategories(10);
92-
var companies = generateCompanies(categories, investors, 500);
134+
var categories = generateDataList(42, randomCategory);
135+
var investors = generateDataList(9987, randomInvestor);
136+
var companies = generateDataList(16635, randomCompany);
137+
linkGeneratedLists(companies, investors, categories);
93138

94139
ng.module('crunchinatorApp')
95140
.config(['$provide', function($provide) {
96-
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
141+
$provide.decorator('$httpBackend', ng.mock.e2e.$httpBackendDecorator);
97142
}]).run(['$httpBackend', function($httpBackend) {
98143
$httpBackend.when('GET', '/companies.json').respond({ companies: companies });
99144
$httpBackend.when('GET', '/categories.json').respond({ categories: categories });
@@ -119,4 +164,4 @@
119164
}
120165

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

app/views/main.tpl.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<div class="container" ng-controller="CrunchinatorCtrl">
2-
<h4>Environment: {{environment}}</h4>
32
<div class="row">
43

54
<div class='col-sm-6 spacing-med'>

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"crossfilter": "~1.3.7",
1717
"ngInfiniteScroll": "*",
1818
"d3-cloud": "*",
19-
"less.js": "~1.6.1"
19+
"less.js": "~1.6.1",
20+
"Faker": "*"
2021
},
2122
"devDependencies": {
2223
"angular-mocks": "~1.2.0",

0 commit comments

Comments
 (0)