Skip to content

Commit a23f182

Browse files
committed
Updated to match curriculum
1 parent 4de3042 commit a23f182

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

src/global_growth/core.clj

+41-34
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,64 @@
22
(:require [clj-http.client :as client]
33
[cheshire.core :as json]))
44

5-
(defn TODO [msg]
6-
(throw (Exception. msg)))
5+
;;;; MODULE 4
76

8-
(defn parse-json [str]
9-
(json/parse-string str true))
10-
11-
;; WORLD BANK API CALLS
127
(defn get-api
138
"Returns map representing API response."
149
[path params]
15-
(let [base-path (str "http://api.worldbank.org" path)
10+
(let [url (str "http://api.worldbank.org" path)
1611
query-params (merge params {:format "json" :per_page 10000})
17-
response (parse-json (:body (client/get base-path {:query-params query-params})))
12+
response (json/parse-string
13+
(:body (client/get url {:query-params query-params})) true)
1814
metadata (first response)
1915
results (second response)]
2016
{:metadata metadata
2117
:results results}))
2218

23-
(defn get-values
24-
"Returns a relation of two keys from API response."
25-
[path query-params key1 key2]
26-
(let [response (get-api path query-params)]
27-
(for [item (:results response)]
28-
[(key1 item) (key2 item)])))
19+
(defn get-country-and-value
20+
[response]
21+
(for [item (:results response)]
22+
(vector (get-in item [:country :value]) (get item :value))))
2923

3024
(defn remove-aggregate-countries
3125
"Remove all countries that aren't actually countries, but are aggregates."
3226
[countries]
3327
(remove (fn [country]
3428
(= (get-in country [:region :value]) "Aggregates")) countries))
3529

36-
(def indicators
37-
(delay (get-values "/topics/16/indicators" {} :name :id)))
38-
3930
;; Get set of country ids so we can filter out aggregate values.
40-
(def country-ids
31+
(def countries
4132
(delay
4233
(let [countries (remove-aggregate-countries (:results (get-api "/countries" {})))]
43-
(set (map :iso2Code countries)))))
34+
(set (map :name countries)))))
35+
36+
(defn get-indicator-values
37+
"Returns indicator values for a specified year for all countries."
38+
[indicator-code year]
39+
(let [response (get-api (str "/countries/all/indicators/" indicator-code)
40+
{:date (str year)})
41+
values (get-country-and-value response)]
42+
(for [[country value] values
43+
:when (and (not (nil? value))
44+
(contains? @countries country))]
45+
[country (read-string value)])))
46+
47+
;;;; MODULE 5
48+
49+
(defn take-top-values
50+
[indicator-values num-values]
51+
(take num-values
52+
(sort-by second > indicator-values)))
53+
54+
(defn get-values
55+
"Returns a relation of two keys from API response."
56+
[path query-params key1 key2]
57+
(let [response (get-api path query-params)]
58+
(for [item (:results response)]
59+
[(key1 item) (key2 item)])))
60+
61+
(def indicators
62+
(delay (get-values "/topics/16/indicators" {} :name :id)))
4463

4564
(defn get-indicators []
4665
"Gets vector of indicators.
@@ -50,21 +69,9 @@
5069
/indicators: All Indicators (about 8800)"
5170
@indicators)
5271

53-
(defn get-indicator-values
54-
"Returns indicator values for a specified year for all countries."
55-
[indicator year list-size]
56-
(let [values (get-values (str "/countries/all/indicators/" indicator)
57-
{:date (str year)}
58-
:country :value)]
59-
(take list-size
60-
(sort-by second >
61-
(for [[country value] values
62-
:when (and (not (nil? value))
63-
(contains? @country-ids (:id country)))]
64-
[(:value country) (read-string value)])))))
65-
6672
(defn -main
6773
[& args]
68-
(let [indicator-values (get-indicator-values "EN.POP.DNST" 2010 10)]
69-
(doseq [value indicator-values]
74+
(let [indicator-values (get-indicator-values "EN.POP.DNST" 2010)
75+
top-10-values (take-top-values indicator-values 10)]
76+
(doseq [value top-10-values]
7077
(println (str (first value) " " (second value))))))

src/global_growth/web.clj

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
(ns global-growth.web
2-
(:require [global-growth.core :as global-growth]
2+
(:require [global-growth.core :as api]
33
[compojure.core :refer [defroutes GET]]
44
[compojure.handler :refer [site]]
55
[hiccup.core :as hiccup]
66
[hiccup.page :as page]
77
[hiccup.form :as form]))
88

9-
; TODO: handle paging: right now just getting a large value
10-
; for 'per_page' parameter.
11-
; Maybe that is fine for simplicity. But that could be a
12-
; problem if we get things with large result sets. Not
13-
; getting large results as of now.
14-
159
;; WEB APP
1610

1711
(defn layout
@@ -52,12 +46,14 @@
5246

5347
(defn view-indicators
5448
[indicator1 indicator2 year]
55-
(let [inds1 (global-growth/get-indicator-values indicator1 year)
56-
inds2 (global-growth/get-indicator-values indicator2 year)
49+
(let [inds1 (api/take-top-values
50+
(api/get-indicator-values indicator1 year) 10)
51+
inds2 (api/take-top-values
52+
(api/get-indicator-values indicator2 year) 10)
5753
indicator-map (into {}
5854
(map (fn [indicator]
5955
[(second indicator) (first indicator)])
60-
(global-growth/get-indicators)))]
56+
(api/get-indicators)))]
6157
(layout "Sorted Indicators"
6258
[:h1 "Sorted Indicators"]
6359
[:div.row
@@ -83,12 +79,12 @@
8379
(form/label "indicator1" "Indicator 1: ")
8480
(form/drop-down {:class "form-control"}
8581
"indicator1"
86-
(global-growth/get-indicators))]
82+
(api/get-indicators))]
8783
[:div.form-group.col-md-5
8884
(form/label "indicator2" "Indicator 2: ")
8985
(form/drop-down {:class "form-control"}
9086
"indicator2"
91-
(global-growth/get-indicators))]
87+
(api/get-indicators))]
9288
[:div.form-group.col-md-2
9389
(form/label "year" "Year: ")
9490
(form/drop-down {:class "form-control"}

0 commit comments

Comments
 (0)