-
Notifications
You must be signed in to change notification settings - Fork 66
/
StoryData.re
executable file
·143 lines (130 loc) · 3.65 KB
/
StoryData.re
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
open Belt;
let apiBaseUrl = "https://serverless-api.hackernewsmobile.com";
let topStoriesUrl = page => {j|$apiBaseUrl/topstories-25-$page.json|j};
let storyUrl = id => {j|$apiBaseUrl/stories/$id.json|j};
type story = {
by: string,
descendants: int,
id: int,
score: int,
time: int,
title: string,
url: option(string),
};
type comment_deleted = {id: int};
type comment_present = {
by: string,
id: int,
kids: option(array(int)),
parent: int,
text: option(string),
time: int,
};
type comment =
| CommentPresent(comment_present)
| CommentDeleted(comment_deleted);
type comments_map = Map.Int.t(comment);
type story_with_comments = {
by: string,
descendants: int,
id: int,
kids: option(array(int)),
score: int,
time: int,
title: string,
url: option(string),
descendentIds: array(int),
comments: comments_map,
};
type topstories = array(story);
module Decode = {
let idsArray = (json): array(int) => Json.Decode.(json |> array(int));
let getCommentId = comment =>
switch (comment) {
| CommentDeleted(c) => c.id
| CommentPresent(c) => c.id
};
let comment = (json): comment => {
let deletedMaybe =
Json.Decode.(json |> optional(field("deleted", bool)));
let deleted =
switch (deletedMaybe) {
| Some(v) => v == true
| None => false
};
if (deleted) {
CommentDeleted(Json.Decode.{id: json |> field("id", int)});
} else {
CommentPresent(
Json.Decode.{
by: json |> field("by", string),
id: json |> field("id", int),
parent: json |> field("parent", int),
kids: json |> optional(field("kids", idsArray)),
text: json |> optional(field("text", string)),
time: json |> field("time", int),
},
);
};
};
let commentsArray = (json): comments_map =>
Json.Decode.array(comment, json)
->(Array.map(comment => (getCommentId(comment), comment)))
->Map.Int.fromArray;
let storyWithComments = (json): story_with_comments =>
Json.Decode.{
by: json |> field("by", string),
descendants: json |> field("descendants", int),
descendentIds: json |> field("descendentIds", idsArray),
comments: json |> field("comments", commentsArray),
id: json |> field("id", int),
kids: json |> optional(field("kids", idsArray)),
score: json |> field("score", int),
time: json |> field("time", int),
title: json |> field("title", string),
url: json |> optional(field("url", string)),
};
let story = (json): story =>
Json.Decode.{
by: json |> field("by", string),
descendants: json |> field("descendants", int),
id: json |> field("id", int),
score: json |> field("score", int),
time: json |> field("time", int),
title: json |> field("title", string),
url: json |> optional(field("url", string)),
};
let stories = (json): array(story) => Json.Decode.(json |> array(story));
};
let fetchTopStories = (page, callback) =>
Js.Promise.(
Fetch.fetch(topStoriesUrl(page))
|> then_(Fetch.Response.json)
|> then_(json =>
json
|> Decode.stories
|> (
stories => {
callback((page, stories));
resolve();
}
)
)
|> ignore
); /* TODO: error handling */
let fetchStoryWithComments = (id, callback) =>
Js.Promise.(
Fetch.fetch(storyUrl(id))
|> then_(Fetch.Response.json)
|> then_(json =>
json
|> Decode.storyWithComments
|> (
stories => {
callback(stories);
resolve();
}
)
)
|> ignore
); /* TODO: error handling */