Last active
November 12, 2020 08:34
-
-
Save nihonjinrxs/5be8e5d54a8c6b33251d21911089ed26 to your computer and use it in GitHub Desktop.
Who's in space?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const got = require('got'); | |
const SPACE_URI = 'http://api.open-notify.org/astros.json'; | |
async function getPeople() { | |
try { | |
const { body } = await got( | |
SPACE_URI, | |
{ headers: { Accept: 'application/json' } } | |
) | |
return JSON.parse(body); | |
} catch (e) { | |
console.error(`ERROR OCCURRED: ${e.message}`); | |
} | |
} | |
function printPeople(people) { | |
const N = getMaxNameLength(people); | |
printColumnTitles(N); | |
people.forEach((p) => printRecord(N, p)); | |
} | |
function printColumnTitles(nameLength) { | |
printRecord(nameLength, { name: 'Name', craft: 'Craft' }) | |
console.log('-'.repeat(nameLength + 1) + '|------'); | |
} | |
function printRecord(nameLength, record) { | |
const str = record.name.padEnd(nameLength, ' ') + | |
' | ' + record.craft; | |
console.log(str); | |
} | |
function getMaxNameLength(people) { | |
return people | |
.map((p) => p.name.length) | |
.reduce((maxVal, length) => maxVal > length ? maxVal : length, -1); | |
} | |
function main() { | |
getPeople().then(({ people }) => printPeople(people)); | |
} | |
if (require.main === module) { | |
main(); | |
} | |
module.exports = { | |
getPeople, | |
printPeople, | |
printColumnTitles, | |
printRecord, | |
getMaxNameLength, | |
main | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"strings" | |
) | |
const ( | |
spaceURI = "http://api.open-notify.org/astros.json" | |
) | |
type Person struct { | |
Name string `json:"name"` | |
Craft string `json:"craft"` | |
} | |
func getPeople() ([]*Person, error) { | |
resp, err := http.Get(spaceURI) | |
if err != nil { | |
fmt.Println("ERROR:", err) | |
} | |
defer resp.Body.Close() | |
type AstrosResponseBody struct { | |
Message string `json:"message"` | |
People []*Person `json:"people"` | |
Number int `json:"number"` | |
} | |
var body AstrosResponseBody | |
dec := json.NewDecoder(resp.Body) | |
err = dec.Decode(&body) | |
if err != nil { | |
fmt.Println("ERROR:", err) | |
return []*Person{}, err | |
} | |
return body.People, err | |
} | |
func printPeople(people []*Person) { | |
N := getMaxNameLength(people) | |
printRecord(Person{Name: "Name", Craft: "Craft"}, N) | |
fmt.Println(strings.Repeat("-", N+1) + "|" + strings.Repeat("-", 6)) | |
for _, person := range people { | |
printRecord(*person, N) | |
} | |
} | |
func printRecord(record Person, nameLength int) { | |
fmtStr := fmt.Sprintf("%%-%ds | %%s\n", nameLength) | |
fmt.Printf(fmtStr, record.Name, record.Craft) | |
} | |
func getMaxNameLength(people []*Person) int { | |
N := -1 | |
for _, person := range people { | |
nameLen := len((*person).Name) | |
if N < nameLen { | |
N = nameLen | |
} | |
} | |
return N | |
} | |
func main() { | |
people, err := getPeople() | |
if err != nil { | |
log.Fatal(err) | |
} | |
printPeople(people) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the exercise this afternoon, I thought I'd take a stab at this in Golang, so I've added that here too.