-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiphy.elm
51 lines (41 loc) · 1.35 KB
/
Giphy.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
module Giphy
( getRandomGif
, getTranslateGif
) where
import Json.Decode as Json
import Http exposing (..)
import Task exposing (..)
(=>) = (,)
type alias ErrorMessage = String
getRandomGif : String -> Task ErrorMessage String
getRandomGif topic =
Http.get decodeRandomUrl (randomUrl topic) `onError` errorToErrorMessage
getTranslateGif : String -> Task ErrorMessage String
getTranslateGif phrase =
Http.get decodeTranslateUrl (translateUrl phrase) `onError` errorToErrorMessage
errorToErrorMessage : Error -> Task ErrorMessage a
errorToErrorMessage error =
Task.fail
<| case error of
Timeout -> "Connection timed out"
NetworkError -> "Network error"
UnexpectedPayload str -> "UnexpectedPayload: " ++ str
BadResponse int str -> "Bad response: " ++ (toString int) ++ ", " ++ str
randomUrl : String -> String
randomUrl topic =
Http.url "http://api.giphy.com/v1/gifs/random"
[ "api_key" => "dc6zaTOxFJmzC"
, "tag" => topic
]
decodeRandomUrl : Json.Decoder String
decodeRandomUrl =
Json.at ["data", "image_url"] Json.string
translateUrl : String -> String
translateUrl phrase =
Http.url "http://api.giphy.com/v1/gifs/translate"
[ "api_key" => "dc6zaTOxFJmzC"
, "s" => phrase
]
decodeTranslateUrl : Json.Decoder String
decodeTranslateUrl =
Json.at ["data", "images", "fixed_height", "url"] Json.string