Skip to content

Instantly share code, notes, and snippets.

@szabba
Created May 24, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szabba/53b6ff489a2de0b902efa91742a56564 to your computer and use it in GitHub Desktop.
Save szabba/53b6ff489a2de0b902efa91742a56564 to your computer and use it in GitHub Desktop.
import Html exposing (Html)
import Html.App as App
import Http
import Task exposing (Task)
main : Program Never
main =
App.program
{ init = init
, update = update
, view = view
, subscriptions = always Sub.none
}
-- MODEL
type alias Model =
String
init : (Model, Cmd Msg)
init =
( ""
, { url = "http://elm-lang.org/try" }
|> sendRequest
|> Task.perform (always Nothing) identity
)
-- MODEL
type alias Msg =
Maybe String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( msg |> Maybe.withDefault model
, Cmd.none
)
-- VIEW
view : Model -> Html Msg
view =
Html.text
-- HELPERS
sendRequest : { url : String } -> Task Http.RawError (Maybe String)
sendRequest { url }=
{ verb = "GET"
, headers = []
, url = url
, body = Http.empty
}
|> Http.send Http.defaultSettings
|> Task.map extractBodyText
extractBodyText : Http.Response -> Maybe String
extractBodyText response =
case response.value of
Http.Text t ->
Just t
Http.Blob _ ->
Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment