Last active
December 17, 2015 16:58
-
-
Save laszlopandy/5642251 to your computer and use it in GitHub Desktop.
Gist creator using GitHub's Http API from Elm
This file contains hidden or 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
import Json | |
import Dict | |
import Http | |
import Graphics.Input as Input | |
jsonObject list = Json.Object (Dict.fromList list) | |
sendAfter sig = Http.send <| foldp (\b _ -> b) (Http.get "") sig | |
asObject obj = | |
case obj of | |
Object dict -> Just dict | |
otherwise -> Nothing | |
asString jsonString = | |
case jsonString of | |
String s -> Just s | |
otherwise -> Nothing | |
g <<= m = case m of | |
Nothing -> Nothing | |
Just x -> g x | |
createGistJson fileContent = Json.toString " " <| jsonObject [ | |
("description", Json.String "test"), | |
("public", Json.Boolean True), | |
("files", jsonObject [ | |
("Test.elm", jsonObject | |
[("content", Json.String fileContent)]) | |
]) | |
] | |
gistUrl = "https://api.github.com/gists" | |
(textField, inputContent) = Input.field "Enter Gist content here" | |
(sendButton, sendClicked) = Input.button "Create a new Gist" | |
createGistRequest = (\x -> Http.post gistUrl (createGistJson x)) <~ inputContent | |
scene b tf info = flow down [tf, b, info] | |
sendGistRequest = sendAfter <| sampleOn sendClicked createGistRequest | |
getGistUrlFromJson str = | |
let maybeUrl = asString <<= (Dict.lookup "html_url") <<= asObject <<= (Json.fromString str) | |
in maybe "" id maybeUrl | |
mapStatus response = | |
case response of | |
Success str -> plainText ("Created a Gist: " ++ (getGistUrlFromJson str)) | |
Waiting -> plainText "Press send to create a Gist" | |
Failure code str -> plainText <| "It failed. Please try again (" ++ str ++ ")" | |
currentStatus = mapStatus <~ sendGistRequest | |
main = scene sendButton <~ textField ~ currentStatus | |
--main = asText (getGistUrlFromJson "{ \"html_url\": \"testurl\"}") |
This file contains hidden or 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
Example: github's JSON response | |
{ | |
"url": "https://api.github.com/gists/5642558", | |
"forks_url": "https://api.github.com/gists/5642558/forks", | |
"commits_url": "https://api.github.com/gists/5642558/commits", | |
"id": "5642558", | |
"git_pull_url": "https://gist.github.com/5642558.git", | |
"git_push_url": "https://gist.github.com/5642558.git", | |
"html_url": "https://gist.github.com/5642558", | |
"files": { | |
"Test.elm": { | |
"filename": "Test.elm", | |
"type": "text/plain", | |
"language": "Elm", | |
"raw_url": "https://gist.github.com/raw/5642558/e588a89280298b674f6ad86639a14bf2b86c42a9/Test.elm", | |
"size": 2, | |
"content": "ff" | |
} | |
}, | |
"public": true, | |
"created_at": "2013-05-24T10:09:53Z", | |
"updated_at": "2013-05-24T10:09:53Z", | |
"description": "test", | |
"comments": 0, | |
"user": null, | |
"comments_url": "https://api.github.com/gists/5642558/comments", | |
"forks": [ | |
], | |
"history": [ | |
{ | |
"user": null, | |
"version": "9aeb30cbe5d0e66cf1b016f1a3af25f19a509f17", | |
"committed_at": "2013-05-24T10:09:53Z", | |
"change_status": { | |
"total": 1, | |
"additions": 1, | |
"deletions": 0 | |
}, | |
"url": "https://api.github.com/gists/5642558/9aeb30cbe5d0e66cf1b016f1a3af25f19a509f17" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment