This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FetchCore.elm
180 lines (136 loc) · 4.7 KB
/
FetchCore.elm
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module Xkcd.FetchCore exposing (currentXkcdInfoUrl, fetchCurrentXkcdResolver, fetchRelevantIdsResolver, fetchXkcdResolver, latestXkcdIdsFromCurrentId, relevantXkcdUrl, xkcdInfoUrl)
{-| Core functionality for Fetch.
-}
import Http
import Json.Decode as Decode
import Task exposing (Task)
import Url exposing (Url)
import Xkcd exposing (..)
import Xkcd.FetchError exposing (..)
-- fetchXkcd
xkcdInfoUrl : XkcdId -> Url
xkcdInfoUrl id =
{ protocol = Url.Https
, host = "xkcd.com"
, port_ = Nothing
, path = "/" ++ String.fromInt id ++ "/info.0.json"
, query = Nothing
, fragment = Nothing
}
fetchXkcdResolver : Int -> Http.Response String -> Result FetchXkcdError Xkcd
fetchXkcdResolver id response =
let
genericError =
Err ("Error fetching xkcd with id " ++ String.fromInt id ++ ".")
in
resultFromResponse response
|> Result.mapError
(\error ->
case error of
(BadStatus { statusCode } _) as original ->
if statusCode == 404 then
Unreleased id
else
Network original
networkError ->
Network networkError
)
|> Result.andThen (\{ body } -> parseXkcd body)
-- fetchCurrentXkcd
currentXkcdInfoUrl : Url
currentXkcdInfoUrl =
{ protocol = Url.Https
, host = "xkcd.com"
, port_ = Nothing
, path = "/info.0.json"
, query = Nothing
, fragment = Nothing
}
fetchCurrentXkcdResolver : Http.Response String -> Result FetchXkcdError Xkcd
fetchCurrentXkcdResolver response =
resultFromResponse response
|> Result.mapError Network
|> Result.andThen (\{ body } -> parseXkcd body)
parseXkcd : String -> Result FetchXkcdError Xkcd
parseXkcd raw =
Decode.decodeString
decodeXkcd
raw
|> Result.mapError Invalid
-- fetchLatestXkcdIds
latestXkcdIdsFromCurrentId : { amount : Int, offset : Int } -> XkcdId -> List XkcdId
latestXkcdIdsFromCurrentId { amount, offset } currentId =
let
-- `List.range` includes the upper bound, therefore we need one less.
sanitizedAmount =
max -1 (amount - 1)
sanitizedOffset =
max 0 offset
sanitizedCurrentId =
max 1 currentId
maxId =
max 1 (sanitizedCurrentId - sanitizedOffset)
minId =
max 1 (maxId - sanitizedAmount)
in
List.range minId maxId
|> List.reverse
-- fetchRelevantXkcdIds
relevantXkcdUrl : String -> Url
relevantXkcdUrl query =
{ protocol = Url.Https
, host = "relevantxkcd.appspot.com"
, port_ = Nothing
, path = "/process"
, query = Just ("action=xkcd&query=" ++ query)
, fragment = Nothing
}
fetchRelevantIdsResolver : Http.Response String -> Result FetchRelevantXkcdError (List XkcdId)
fetchRelevantIdsResolver response =
resultFromResponse response
|> Result.mapError Network
|> Result.andThen (\{ body } -> parseRelevantXkcdResponse body)
parseRelevantXkcdResponse : String -> Result FetchRelevantXkcdError (List XkcdId)
parseRelevantXkcdResponse body =
let
dropFromEnd amount list =
List.take (List.length list - amount) list
sanitizeLines lines =
let
amount =
List.length lines
in
if amount >= 3 then
-- The first two entries are statistics.
List.drop 2 lines
-- The last line is a newline.
|> dropFromEnd 1
|> Ok
else
Err (Invalid <| "Expected 3 lines, but got only " ++ String.fromInt amount ++ ".")
in
String.lines body
|> sanitizeLines
|> Result.andThen
(\lines ->
List.map parseLine lines
|> List.foldl
(\result ->
Result.andThen
(\previousIds ->
Result.map (\id -> previousIds ++ [ id ]) result
)
)
(Ok [])
)
parseLine : String -> Result FetchRelevantXkcdError XkcdId
parseLine line =
case String.words line of
idString :: urlString :: [] ->
case String.toInt idString of
Just id ->
Ok id
_ ->
Err (Invalid "Malformed line. Could not convert id.")
malformed ->
Err (Invalid <| "Malformed line. Expected 2 fields, got " ++ (List.length malformed |> String.fromInt) ++ ".")