Skip to content

Instantly share code, notes, and snippets.

@pdamoc
Created October 29, 2015 11:46
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 pdamoc/3fdcec14db038d4260cb to your computer and use it in GitHub Desktop.
Save pdamoc/3fdcec14db038d4260cb to your computer and use it in GitHub Desktop.
A strategy to decouple the requests from the rest o the app in order to facilitate mock results.
import Html exposing (..)
import Html.Attributes exposing (style, id, type', src)
import Html.Events exposing (onClick)
import Task exposing (Task, succeed)
import StartApp
import Effects exposing (Never)
import Json.Decode exposing ( (:=), string )
import Http exposing (send, Request, empty, defaultSettings, fromJson )
import Task exposing (andThen)
debug = False
corsGet : Request
corsGet =
{ verb = "GET"
, headers =
[ ("Origin", "http://elm-lang.org")
, ("Access-Control-Request-Method", "GET")
, ("X-Mashape-Key", "aZPQfgrZSAmshIcZ0vt29ougt630p1Lv5w9jsnvTUZ68uvSW90")
, ("Access-Control-Request-Headers", "X-Custom-Header")
]
, url = "https://andruxnet-random-famous-quotes.p.mashape.com/cat=movies"
, body = empty
}
dataService : String -> Task x () -- converts requests received in the query mailbox to messages to results mailbox
dataService req =
if req == ""
then
succeed () -- It prevents the execution of the default request in the querry signal
else
if debug
then
Signal.send results.address (Ok "Do or not do, there is no try!")
else
( fromJson ("quote" := string) (send defaultSettings corsGet)
|> Task.mapError (\e -> toString e)
|> Task.toResult) `andThen` (Signal.send results.address)
query = Signal.mailbox ""
results = Signal.mailbox (Ok "")
port requests : Signal (Task x ())
port requests = Signal.map dataService query.signal
type alias Model =
{ output : String
, serviceAddress : Signal.Address String}
init : (Model, Effects.Effects Action)
init = ({ output = "start", serviceAddress = query.address}, Effects.none)
messageToServiceEffect : Signal.Address String -> String -> Effects.Effects Action
messageToServiceEffect address req=
Signal.send address req
|> Task.map NoOp
|> Effects.task
type Action = NewResult (Result String String) | RequestValue | NoOp ()
update : Action -> Model -> (Model, Effects.Effects Action)
update action model =
case action of
NewResult result ->
case result of
Ok value -> ({ model | output <- "It worked! I received: \"" ++ value++"\""}, Effects.none)
Err error -> ({ model | output <- "Error: " ++ error}, Effects.none)
RequestValue -> (model, messageToServiceEffect model.serviceAddress "Get Shorty!")
NoOp () -> (model, Effects.none)
view address model =
div []
[ button [onClick address RequestValue ] [text "Get Quote"]
, p [] [text model.output]
]
app =
StartApp.start
{ init = init
, update = update
, view = view
, inputs = [Signal.map NewResult results.signal]
}
main = app.html
port tasks : Signal (Task Never ())
port tasks = app.tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment