-
Notifications
You must be signed in to change notification settings - Fork 1
/
gliberapay.gleam
165 lines (149 loc) · 4.4 KB
/
gliberapay.gleam
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import gleam/dict
import gleam/float
import gleam/http/request.{type Request}
import gleam/int
import gleam/list
import gleam/option.{type Option, None, Some}
import gleam/result
import gleam/set
import gleam/string
import gsv
const patrons_csv_headers = [
"pledge_date", "patron_id", "patron_username", "patron_public_name",
"donation_currency", "weekly_amount", "patron_avatar_url",
]
pub type Patron {
Patron(
pledge_date: Date,
patron_id: Int,
patron_username: String,
/// The name the patron has chosen to display publicly.
///
/// If the patron has not set a public name then this will be `None` and you
/// may want to use `patron_username` instead.
///
patron_public_name: Option(String),
donation_currency: String,
/// The amount the patron has pledged to donate per week.
///
/// If the patron has set their amount to be private then this will be `None`.
///
weekly_amount: Option(Float),
patron_avatar_url: String,
)
}
pub type Error {
InvalidCsvSyntax(String)
MissingCsvHeaderRow
MissingCsvHeaders(missing: List(String))
InvalidValue(detail: String)
}
pub type Date {
Date(year: Int, month: Int, day: Int)
}
/// Construct a HTTP request to download the public Liberapay patrons CSV for
/// the given recipient.
///
/// Once you have a response for the request you can parse the body with the
/// `parse_patrons_csv` function.
///
pub fn download_patrons_csv_request(
recipient recipient: String,
) -> Request(String) {
request.new()
|> request.set_host("liberapay.com")
|> request.set_path("/" <> recipient <> "/patrons/public.csv")
}
/// Parse a Liberapay patrons CSV, as can be downloaded from the Liberapay, e.g.
/// <https://liberapay.com/gleam/patrons/public.csv>
///
/// If you want to download this in Gleam see the `download_patrons_csv` function.
///
pub fn parse_patrons_csv(csv: String) -> Result(List(Patron), Error) {
csv
|> gsv.to_lists_or_error
|> result.map_error(InvalidCsvSyntax)
|> result.try(extract_csv_patrons)
}
fn extract_csv_patrons(csv: List(List(String))) -> Result(List(Patron), Error) {
use #(headers, rows) <- result.try(pop_headers(csv))
let rows =
list.map(rows, fn(row) {
list.zip(headers, row)
|> dict.from_list
})
use row <- list.try_map(rows)
let get = fn(k) {
case dict.get(row, k) {
Ok(v) -> Ok(v)
_ -> Error(InvalidValue("missing value for " <> k))
}
}
use date <- result.try(get("pledge_date"))
use id <- result.try(get("patron_id"))
use username <- result.try(get("patron_username"))
use name <- result.try(get("patron_public_name"))
use currency <- result.try(get("donation_currency"))
use amount <- result.try(get("weekly_amount"))
use avatar <- result.try(get("patron_avatar_url"))
use date <- result.try(parse_date(date))
use id <- result.try(parse_int(id))
use amount <- result.try(parse_amount(amount))
Ok(Patron(
pledge_date: date,
patron_id: id,
patron_username: username,
patron_public_name: optional_string(name),
donation_currency: currency,
weekly_amount: amount,
patron_avatar_url: avatar,
))
}
fn optional_string(s: String) -> Option(String) {
case s {
"" -> None
_ -> Some(s)
}
}
fn parse_int(int: String) -> Result(Int, Error) {
case int.parse(int) {
Ok(i) -> Ok(i)
_ -> Error(InvalidValue("invalid int: " <> int))
}
}
fn parse_amount(amount: String) -> Result(Option(Float), Error) {
case amount {
"" | "private" -> Ok(None)
_ ->
case float.parse(amount) {
Ok(f) -> Ok(Some(f))
_ -> Error(InvalidValue("invalid float: " <> amount))
}
}
}
fn parse_date(date: String) -> Result(Date, Error) {
use numbers <- result.try(
string.split(date, "-")
|> list.try_map(int.parse)
|> result.map_error(fn(_) { InvalidValue("invalid date: " <> date) }),
)
case numbers {
[year, month, day] -> Ok(Date(year, month, day))
_ -> Error(InvalidValue("invalid date: " <> date))
}
}
fn pop_headers(
csv: List(List(String)),
) -> Result(#(List(String), List(List(String))), Error) {
use #(headers, rest) <- result.try(case csv {
[first, ..rest] -> Ok(#(first, rest))
[] -> Error(MissingCsvHeaderRow)
})
let headers_set = set.from_list(headers)
let missing =
list.filter(patrons_csv_headers, fn(h) { !set.contains(headers_set, h) })
case missing {
[] -> Ok(#(headers, rest))
_ -> Error(MissingCsvHeaders(missing))
}
}