-
Notifications
You must be signed in to change notification settings - Fork 65
/
formatters.clj
62 lines (51 loc) 路 1.7 KB
/
formatters.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns yetibot.commands.weather.formatters
(:require
[clojure.string :as str]))
(defn c-to-f [c] (-> (* c 9/5) (+ 32) float))
(defn km-to-mi [km] (-> (/ km 1.609) float))
(def imperial-units
{:temp (fn [v] (format "%.1f掳%s" (c-to-f v) "F"))
:speed (fn [v] (format "%.1f %s" (km-to-mi v) "mph"))})
(def metric-units
{:temp (fn [v] (format "%.1f掳%s" (float v) "C"))
:speed (fn [v] (format "%.1f %s" (float v) "km/h"))})
(defn- get-formatters-by-cc
[cc]
(let [cc (-> cc str/lower-case keyword)]
(condp = cc
:lbr imperial-units ;; Liberia
:mm imperial-units ;; Myanmar
:us imperial-units ;; The United States of America
;; THE ENTIRE REST OF THE WORLD
metric-units)))
(defn get-formatters
[unit cc]
(if (nil? unit)
(get-formatters-by-cc cc)
(if (= unit :i)
imperial-units
metric-units)))
(defn location-title
[{:keys [city_name state_code country_code]}]
(let [loc (if (re-matches #"\d+" state_code)
city_name
(str city_name ", " state_code))]
(format "%s (%s)" loc country_code)))
(defn description
[{fmt :temp} {temp :temp {:keys [icon code description]} :weather}]
(format "%s - %s"
(fmt temp)
(str/join (map str/capitalize (str/split description #"\b")))))
(defn feels-like
[{fmt :temp} {app_temp :app_temp}]
(format "Feels like %s" (fmt app_temp)))
(defn wind
[{fmt :speed} {:keys [wind_spd wind_cdir]}]
(format "Winds %s %s" (fmt wind_spd) wind_cdir))
(defn forecast-item
"Format a forecast item like: date: min - max"
[{fmt :temp} {:keys [min_temp max_temp valid_date]}]
(format "%s: %s - %s"
valid_date
(fmt min_temp)
(fmt max_temp)))