Created
April 18, 2016 21:21
-
-
Save chuck0523/bea188843f647431f01b477252aed05c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getRandomGif : String -> Effects Action | |
getRandomGif topic = | |
Http.get decodeUrl (randomUrl topic) | |
|> Task.toMaybe | |
|> Task.map NewGif | |
|> Effects.task | |
-- getRandomGifはGETメソッドをもつHTTPリクエストを発行している。 | |
-- randomUrl関数はtopicを引数にJSONを返す。 | |
-- decodeUrl関数は受け取ったJSONをデコードする。 | |
-- それぞれの関数は以下に定義されている。 | |
randomUrl : String -> String | |
randomUrl topic = | |
Http.url "http://api.giphy.com/v1/gifs/random" | |
[ ("api_key", "dc6zaTOxFJmzC") | |
, ("tag", topic) | |
] | |
-- topicが与えられて、giphyAPIのためのURLを生成する。 | |
decodeUrl : Json.Decoder String | |
decodeUrl = | |
Json.at ["data", "image_url"] Json.string | |
-- 巨大なJSONから、json.data.image_urlを取得する。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment