Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/fetchCurrentTemperature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ describe("fetchCurrentTemperature", () => {
assert(result.temperature_2m.every(x => typeof x === "number")); // Assert each element in that time is a number
});
});

it("Promise rejects when given wrong latitude and longitude numbers", () => {
return expect(fetchCurrentTemperature({lat:100, lon: 200})).rejects.toThrow();
});

it("returns the right array length", () => {
const promise = fetchCurrentTemperature({ lat: 0, lon: 0 });

return promise.then(result => {
assert (result.time.length === result.temperature_2m.length); //check array length
});
});

});
42 changes: 33 additions & 9 deletions src/fetchGeoCoord.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import assert from "assert";
import { fetchGeoCoord } from "./fetchGeoCoord.js";


describe("fetchGeoCoord", () => {
it("follows type specification", () => {
const promise = fetchGeoCoord("University of Massachusetts Amherst");

return promise.then(result => {
assert(typeof result === "object"); // Assert the result is an object
assert(typeof result.lon === "number"); // Assert that the lon value is a number
assert(typeof result.lat === "number"); // Assert that the lat value is a number
assert(Object.keys(result).length === 2); // Assert there are only two keys in the object
});
it("follows type specification", () => {
const promise = fetchGeoCoord("University of Massachusetts Amherst");

return promise.then(result => {
assert(typeof result === "object"); // Assert the result is an object
assert(typeof result.lon === "number"); // Assert that the lon value is a number
assert(typeof result.lat === "number"); // Assert that the lat value is a number
assert(Object.keys(result).length === 2); // Assert there are only two keys in the object
});
});

it("throws an error for an invalid location", () => {
const promise = fetchGeoCoord("XYZ");
return promise.catch(error => {assert(error === ("No results found for query."))})
})

it("gives the correct coordinates for University of Massachusetts Amherst", () => {
const promise = fetchGeoCoord("University of Massachusetts Amherst");

return promise.then(result => {
assert(result.lat === 42.3869382);
assert(result.lon === -72.52991477067445);
})
})

it("gives the correct coordinates for Amherst College", () => {
const promise = fetchGeoCoord("Amherst College");

return promise.then(result => {
assert(result.lat === 42.3703768);
assert(result.lon === -72.5160691823344);
})
})
});
105 changes: 93 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,97 @@
import { fetchUMassWeather, fetchUCalWeather, fetchUniversityWeather, AverageTemperatureResults } from "./universityWeather.js";
import fetch from "../include/fetch.js";
import path from "path";

fetchUMassWeather().then(results => console.log(results)).catch(error => console.error(error));
fetchUCalWeather().then(results => console.log(results)).catch(error => console.log(error));
// TODO - Now its your turn to make the working example! :)
/*
authorInfo takes in an author name and gets the data from the link.
It uses this information to get the author id.
Using this author id we can then access all the author specific information.
Also using the author id we can get the works of the author
This is what authorWorks does.
*/

const randomCollegeSearch = (universityQuery: string) : Promise<AverageTemperatureResults> => {
return fetchUniversityWeather(universityQuery).then(res => {
console.log(`\n${universityQuery} search:`);
return res;
});
interface docs{
key: string;
type: string;
name: string;
alternateNames: string[];
birthDate: string;
topWork: string;
workCount: string;
topSubjects: string;
version: string;
}

randomCollegeSearch("Boston College").then(console.log);
randomCollegeSearch("Smith College").then(console.log);
randomCollegeSearch("Harvard University").then(console.log);
randomCollegeSearch("test").then(console.log).catch(error => console.error("Expected", error));
interface author{
numFound: number;
start: number;
numFoundExact: boolean;
docs: docs[]
}

interface Book {
type: {
key: string;
};
title: string;
authors: {
type: {
key: string;
};
author: {
key: string;
};
}[];
covers: number[];
key: string;
latest_revision: number;
revision: number;
created: {
type: string;
value: string;
};
last_modified: {
type: string;
value: string;
};
}


export function authorInfo(name:string){
const authorName = name.replace(". ", " ");
const baseURL = "https://220.maxkuechen.com/fetch/noCache/?url=https://openlibrary.org";
const apiURL = `${baseURL}/search/authors.json?q=${encodeURIComponent(authorName)}`;

fetch(apiURL)
.then(result => result.ok ? result.json() : Promise.reject(new Error(`Error in response: ${result.statusText}`)))
.then ((response: author) => {
return response.numFoundExact && Array.isArray(response.docs) && response.docs.length>0
? Promise.resolve(response.docs[0].key)
: Promise.reject(new Error("No results found for the given author."));
})
.then((Id:string) => {console.log ("Author's ID: " ,Id) ; return fetch (`https://220.maxkuechen.com/fetch/noCache/?url=https://openlibrary.org/authors/${encodeURIComponent(Id)}.json`)})
.then (res => res.ok ? res.json() : Promise.reject(new Error(`Error in response: ${res.statusText}`)))
.then (info =>console.log("Author's information: ", info.bio))
.catch(error => console.log("Invalid", error));
}

export function authorWorks(authorID:string, numWorks: number) {
const apiURL = `https://220.maxkuechen.com/fetch/noCache/?url=https://openlibrary.org/authors/${encodeURIComponent(authorID)}/works.json?limit=${encodeURIComponent(numWorks)}`;
console.log(apiURL);

fetch(apiURL)
.then(result => result.ok ? result.json() : Promise.reject(new Error("Error in response: ${result.statusText}")))
.then(response => {
console.log("Author Works:");
response.entries.forEach ((bk: Book) => console.log(bk.title) );
})
.catch(error => console.log("Invalid") + error);
}

authorInfo("J K Rowling");
authorWorks("OL23919A", 10);





51 changes: 49 additions & 2 deletions src/universityWeather.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import assert from "assert";
import { fetchUCalWeather, fetchUMassWeather } from "./universityWeather.js";
import { fetchUCalWeather, fetchUMassWeather, fetchUniversityWeather } from "./universityWeather.js";

// 1000ms
const SECOND = 1000;
const SECOND = 5000;
// 30 second timeout
jest.setTimeout(30 * SECOND);


describe("fetchUniversityWeather", () => {
it("returns an error when entered an empty query", () => {
return expect(fetchUniversityWeather("No results found for query.")).rejects.toThrow();
});

it("works 1 university", () => {
const promise = fetchUniversityWeather("University of Connecticut");

return promise.then(result => {
expect(result.totalAverage).toEqual(result['University of Connecticut']);
});
});

it("works with 2 universities", () => {
const promise = fetchUniversityWeather("Aachen");

return promise.then(result => {
const x1 = result['Rheinisch Westfälische Technische Hochschule Aachen'];
const x2 = result['Fachhochschule Aachen'];
expect(result.totalAverage).toEqual((x1+x2)/2);
});
})
})



describe("fetchUCalWeather", () => {
it("follows type specification", () => {
const promise = fetchUCalWeather();
Expand All @@ -20,6 +47,13 @@ describe("fetchUCalWeather", () => {
it(`should fetch weather data for UMass universities`, () => {
return fetchUMassWeather().then(result => {
expect(result).toHaveProperty(`totalAverage`);
[
'University of Massachusetts Boston',
'University of Massachusetts at Dartmouth',
'University of Massachusetts at Lowell',
'University of Massachusetts at Amherst'
].forEach(name => expect(result).toHaveProperty(name));

});
});
});
Expand All @@ -38,6 +72,19 @@ describe("fetchUMassWeather", () => {
it(`should fetch weather data for UCal Universities`, () => {
return fetchUCalWeather().then(result => {
expect(result).toHaveProperty(`totalAverage`);
[
'University of California, Merced',
'University of California, Irvine',
'University of California, Berkeley',
'University of California, Davis',
'University of California, Los Angeles',
'University of California, Office of the President',
'University of California, Santa Cruz',
'University of California, Santa Barbara',
'University of California, San Diego',
'University of California, San Francisco',
'University of California, Riverside'
].forEach(name => expect(result).toHaveProperty(name));
});
});
});
30 changes: 18 additions & 12 deletions src/universityWeather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,39 @@ export function fetchUniversityWeather(
if (universityNames.length === 0) throw new Error("No results found for query.");
const transformedNames = transformName ? universityNames.map(transformName) : universityNames;
const geoCoordPromises = transformedNames.map(name => fetchGeoCoord(name));
return Promise.all(geoCoordPromises).then(geoCoords => {
return Promise.all(geoCoordPromises)
.then(geoCoords => {
const temperaturePromises = geoCoords.map((coord, index) => fetchCurrentTemperature(coord).then(temperature => {
const averageTemp = temperature.temperature_2m.reduce((a, b) => a + b, 0) / temperature.temperature_2m.length;
return {
name: universityNames[index],
averageTemp
};
}));
return Promise.all(temperaturePromises).then(temperatures => {
let totalAverage = 0;
const results: AverageTemperatureResults = { totalAverage: 0 };
temperatures.forEach(temp => {
totalAverage += temp.averageTemp;
results[temp.name] = temp.averageTemp;
});
results.totalAverage = totalAverage / temperatures.length;
return results;
return Promise.all(temperaturePromises)
})
.then(temperatures => {
let totalAverage = 0;
const results: AverageTemperatureResults = { totalAverage: 0 };
temperatures.forEach(temp => {
totalAverage += temp.averageTemp;
results[temp.name] = temp.averageTemp;
});
results.totalAverage = totalAverage / temperatures.length;
return results;

});
})
}

function transformName (name: string): string {
return name.replace(/ at|,|-/g, ' ');
}

export function fetchUMassWeather(): Promise<AverageTemperatureResults> {
const transformName = (name: string): string => name.replace(` at `, ` `);
return fetchUniversityWeather(`University of Massachusetts`, transformName);
}

export function fetchUCalWeather(): Promise<AverageTemperatureResults> {
return fetchUniversityWeather(`University of California`);
return fetchUniversityWeather(`University of California`, transformName);
}