Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Last active September 2, 2015 11:25
Show Gist options
  • Save amitaibu/30c08802be18a74f39d0 to your computer and use it in GitHub Desktop.
Save amitaibu/30c08802be18a74f39d0 to your computer and use it in GitHub Desktop.
import Graphics.Element exposing (show)
import Json.Decode as Json exposing ((:=))
import Http
import Task exposing (Task, andThen, onError)
import TaskTutorial exposing (print)
type alias Id = Int
type alias Article =
{ id: Id
, title: String
}
type alias Model =
{ articles: List Article
}
articleInit : Article
articleInit = Article 0 ""
model : Model
model = Model []
decoder : Json.Decoder (List Article)
decoder =
Json.at ["data"]
<| Json.list
<| Json.object2 Article
("id" := Json.int)
("title" := Json.string)
url : String
url = "http://live-todo-restful.pantheon.io/api/v1.0/todos1"
get : Task Http.Error (List Article)
get =
Http.get decoder url
safeGet : Task x (Result Http.Error (List Article))
safeGet =
Task.toResult get
port runner : Task x ()
port runner =
let
report : Result Http.Error (List Article) -> Task x ()
report result =
case result of
Ok articles -> Signal.send contentMailbox.address articles
Err msg -> Signal.send contentMailbox.address []
in
safeGet `andThen` report
main =
Signal.map show contentMailbox.signal
contentMailbox : Signal.Mailbox (List Article)
contentMailbox =
Signal.mailbox []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment