From 635c55460d176a71d2a5c5eeba6bdb6775cef0ee Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sat, 25 Jun 2016 08:27:41 -0700 Subject: [PATCH] Swap part7 and part8 --- part7/ElmHub.elm | 67 +++++++++++++++++++------------------ part7/Main.elm | 26 +++++++++++--- part7/README.md | 14 -------- {part8 => part7}/github.js | 0 part7/index.html | 23 +++++++++++-- part7/test/Auth.elm | 7 ---- part7/test/TestRunner.elm | 15 --------- part7/test/Tests.elm | 55 ------------------------------ part7/test/elm-package.json | 20 ----------- {part7 => part8}/.gitignore | 0 part8/ElmHub.elm | 67 ++++++++++++++++++------------------- part8/Main.elm | 26 +++----------- part8/README.md | 6 ++++ part8/index.html | 23 ++----------- part8/test/.gitignore | 1 - part8/test/Tests.elm | 18 +++++----- 16 files changed, 130 insertions(+), 238 deletions(-) rename {part8 => part7}/github.js (100%) delete mode 100644 part7/test/Auth.elm delete mode 100644 part7/test/TestRunner.elm delete mode 100644 part7/test/Tests.elm delete mode 100644 part7/test/elm-package.json rename {part7 => part8}/.gitignore (100%) delete mode 100644 part8/test/.gitignore diff --git a/part7/ElmHub.elm b/part7/ElmHub.elm index 819af7e..ef71143 100644 --- a/part7/ElmHub.elm +++ b/part7/ElmHub.elm @@ -1,26 +1,23 @@ module ElmHub exposing (..) -import Auth import Html exposing (..) import Html.Attributes exposing (class, target, href, property, defaultValue) import Html.Events exposing (..) -import Http +import Auth import Task exposing (Task) import Json.Decode exposing (Decoder) import Json.Decode.Pipeline exposing (..) +import Json.Encode -searchFeed : String -> Cmd Msg -searchFeed query = - let - url = - "https://api.github.com/search/repositories?access_token=" - ++ Auth.token - ++ "&q=" - ++ query - ++ "+language:elm&sort=stars&order=desc" - in - Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url) +getQueryUrl : String -> String +getQueryUrl query = + -- See https://developer.github.com/v3/search/#example for how to customize! + "https://api.github.com/search/repositories?access_token=" + ++ Auth.token + ++ "&q=" + ++ query + ++ "+language:elm&sort=stars&order=desc" responseDecoder : Decoder (List SearchResult) @@ -101,34 +98,26 @@ type Msg = Search | SetQuery String | DeleteById ResultId - | HandleSearchResponse (List SearchResult) - | HandleSearchError Http.Error + | SetResults (List SearchResult) + | SetErrorMessage (Maybe String) + | DoNothing -update : Msg -> Model -> ( Model, Cmd Msg ) -update msg model = +update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg ) +update searchFeed msg model = case msg of Search -> - ( model, searchFeed model.query ) - - HandleSearchResponse results -> - ( { model | results = results }, Cmd.none ) - - HandleSearchError error -> - let - errorMessage = - case error of - Http.UnexpectedPayload message -> - Just message - - _ -> - Nothing - in - ( { model | errorMessage = errorMessage }, Cmd.none ) + ( model, searchFeed (getQueryUrl model.query) ) SetQuery query -> ( { model | query = query }, Cmd.none ) + SetResults results -> + ( { model | results = results }, Cmd.none ) + + SetErrorMessage errorMessage -> + ( { model | errorMessage = errorMessage }, Cmd.none ) + DeleteById idToHide -> let newResults = @@ -139,3 +128,15 @@ update msg model = { model | results = newResults } in ( newModel, Cmd.none ) + + DoNothing -> + ( model, Cmd.none ) + + +decodeGithubResponse : Json.Encode.Value -> Msg +decodeGithubResponse value = + -- TODO use Json.Decode.DecodeValue to decode the response into an Action. + -- + -- Hint: look at ElmHub.elm, specifically the definition of Action and + -- the deefinition of responseDecoder + SetErrorMessage (Just "TODO decode the response!") diff --git a/part7/Main.elm b/part7/Main.elm index c60133a..a736ba5 100644 --- a/part7/Main.elm +++ b/part7/Main.elm @@ -1,13 +1,31 @@ -module Main exposing (..) +port module Main exposing (..) import ElmHub exposing (..) +import Html.App +import Json.Decode main : Program Never main = Html.App.program { view = view - , update = update - , init = ( initialModel, searchFeed initialModel.query ) - , inputs = [] + , update = update githubSearch + , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) ) + , subscriptions = \_ -> githubResponse decodeResponse } + + +decodeResponse : Json.Decode.Value -> Msg +decodeResponse json = + case Json.Decode.decodeValue responseDecoder json of + Err err -> + SetErrorMessage (Just err) + + Ok results -> + SetResults results + + +port githubSearch : String -> Cmd msg + + +port githubResponse : (Json.Decode.Value -> msg) -> Sub msg diff --git a/part7/README.md b/part7/README.md index 6ef1372..d52699c 100644 --- a/part7/README.md +++ b/part7/README.md @@ -18,17 +18,3 @@ to fail; in that case, just run `elm-package install` again.) ```bash elm-live Main.elm --open --output=elm.js ``` - -## Running Tests - -```bash -cd test -elm-package install -elm test TestRunner.elm -``` - -## References - -* [Using Elm packages](https://github.com/elm-lang/elm-package/blob/master/README.md#basic-usage) -* [elm-test documentation](http://package.elm-lang.org/packages/deadfoxygrandpa/elm-test/3.1.1/) -* [`(<|)` documentation](http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Basics#<|) diff --git a/part8/github.js b/part7/github.js similarity index 100% rename from part8/github.js rename to part7/github.js diff --git a/part7/index.html b/part7/index.html index 93fc00c..f717a9a 100644 --- a/part7/index.html +++ b/part7/index.html @@ -4,17 +4,36 @@ ElmHub + - +
diff --git a/part7/test/Auth.elm b/part7/test/Auth.elm deleted file mode 100644 index c59f16a..0000000 --- a/part7/test/Auth.elm +++ /dev/null @@ -1,7 +0,0 @@ -module Auth exposing (token) - - -token : String -token = - -- Tests don't need a real token. - "" diff --git a/part7/test/TestRunner.elm b/part7/test/TestRunner.elm deleted file mode 100644 index 0baa6f2..0000000 --- a/part7/test/TestRunner.elm +++ /dev/null @@ -1,15 +0,0 @@ -module Main where - -import Signal exposing (Signal) - -import ElmTest exposing (consoleRunner) -import Console exposing (IO, run) -import Task - -import Tests - -console : IO () -console = consoleRunner Tests.all - -port runner : Signal (Task.Task x ()) -port runner = run console diff --git a/part7/test/Tests.elm b/part7/test/Tests.elm deleted file mode 100644 index ad81e51..0000000 --- a/part7/test/Tests.elm +++ /dev/null @@ -1,55 +0,0 @@ -port module Main exposing (..) - -import Test exposing (..) -import Expect exposing (Expectation) -import ElmHub exposing (responseDecoder) -import Json.Decode exposing (decodeString, Value) -import Test.Runner.Node as Runner - - -main : Program Never -main = - describe "Decoding responses from GitHub" - [ test "they can decode empty responses" - <| \() -> - let - emptyResponse = - """{ "items": [] }""" - in - Expect.equal (Ok []) - (decodeString responseDecoder emptyResponse) - , test "they can decode responses with results in them" - <| \() -> - let - response = - """{ "items": [ - /* TODO: dummy JSON goes here */ - ] }""" - in - Expect.equal - (Ok - [ { id = 5, name = "foo", stars = 42 } - , { id = 3, name = "bar", stars = 77 } - ] - ) - (decodeString responseDecoder response) - , test "they result in an error for invalid JSON" - <| \() -> - let - response = - """{ "pizza": [] }""" - - isErrorResult result = - -- TODO return True if the given Result is an Err of some sort, - -- and False if it is an Ok of some sort. - -- - -- Result docs: http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result - False - in - Expect.true "Expected decoding an invalid response to return an Err." - (isErrorResult (decodeString responseDecoder response)) - ] - |> Runner.run emit - - -port emit : ( String, Value ) -> Cmd msg diff --git a/part7/test/elm-package.json b/part7/test/elm-package.json deleted file mode 100644 index 8525bcc..0000000 --- a/part7/test/elm-package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "version": "1.0.0", - "summary": "Like GitHub, but for Elm stuff.", - "repository": "https://github.com/rtfeldman/elm-workshop.git", - "license": "BSD-3-Clause", - "source-directories": [ - ".", - ".." - ], - "exposed-modules": [], - "dependencies": { - "project-fuzzball/test": "2.0.1 <= v < 3.0.0", - "project-fuzzball/node": "1.0.2 <= v < 2.0.0", - "NoRedInk/elm-decode-pipeline": "1.1.2 <= v < 2.0.0", - "elm-lang/core": "4.0.1 <= v < 5.0.0", - "elm-lang/html": "1.0.0 <= v < 2.0.0", - "evancz/elm-http": "3.0.1 <= v < 4.0.0" - }, - "elm-version": "0.17.0 <= v < 0.18.0" -} diff --git a/part7/.gitignore b/part8/.gitignore similarity index 100% rename from part7/.gitignore rename to part8/.gitignore diff --git a/part8/ElmHub.elm b/part8/ElmHub.elm index ef71143..819af7e 100644 --- a/part8/ElmHub.elm +++ b/part8/ElmHub.elm @@ -1,23 +1,26 @@ module ElmHub exposing (..) +import Auth import Html exposing (..) import Html.Attributes exposing (class, target, href, property, defaultValue) import Html.Events exposing (..) -import Auth +import Http import Task exposing (Task) import Json.Decode exposing (Decoder) import Json.Decode.Pipeline exposing (..) -import Json.Encode -getQueryUrl : String -> String -getQueryUrl query = - -- See https://developer.github.com/v3/search/#example for how to customize! - "https://api.github.com/search/repositories?access_token=" - ++ Auth.token - ++ "&q=" - ++ query - ++ "+language:elm&sort=stars&order=desc" +searchFeed : String -> Cmd Msg +searchFeed query = + let + url = + "https://api.github.com/search/repositories?access_token=" + ++ Auth.token + ++ "&q=" + ++ query + ++ "+language:elm&sort=stars&order=desc" + in + Task.perform HandleSearchError HandleSearchResponse (Http.get responseDecoder url) responseDecoder : Decoder (List SearchResult) @@ -98,26 +101,34 @@ type Msg = Search | SetQuery String | DeleteById ResultId - | SetResults (List SearchResult) - | SetErrorMessage (Maybe String) - | DoNothing + | HandleSearchResponse (List SearchResult) + | HandleSearchError Http.Error -update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg ) -update searchFeed msg model = +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = case msg of Search -> - ( model, searchFeed (getQueryUrl model.query) ) + ( model, searchFeed model.query ) + + HandleSearchResponse results -> + ( { model | results = results }, Cmd.none ) + + HandleSearchError error -> + let + errorMessage = + case error of + Http.UnexpectedPayload message -> + Just message + + _ -> + Nothing + in + ( { model | errorMessage = errorMessage }, Cmd.none ) SetQuery query -> ( { model | query = query }, Cmd.none ) - SetResults results -> - ( { model | results = results }, Cmd.none ) - - SetErrorMessage errorMessage -> - ( { model | errorMessage = errorMessage }, Cmd.none ) - DeleteById idToHide -> let newResults = @@ -128,15 +139,3 @@ update searchFeed msg model = { model | results = newResults } in ( newModel, Cmd.none ) - - DoNothing -> - ( model, Cmd.none ) - - -decodeGithubResponse : Json.Encode.Value -> Msg -decodeGithubResponse value = - -- TODO use Json.Decode.DecodeValue to decode the response into an Action. - -- - -- Hint: look at ElmHub.elm, specifically the definition of Action and - -- the deefinition of responseDecoder - SetErrorMessage (Just "TODO decode the response!") diff --git a/part8/Main.elm b/part8/Main.elm index 718fc54..c60133a 100644 --- a/part8/Main.elm +++ b/part8/Main.elm @@ -1,31 +1,13 @@ -port module Main exposing (..) +module Main exposing (..) import ElmHub exposing (..) -import Html.App -import Json.Decode exposing (Value) main : Program Never main = Html.App.program { view = view - , update = update githubSearch - , init = ( initialModel, githubSearch (getQueryUrl initialModel.query) ) - , subscriptions = \_ -> githubResponse decodeResponse + , update = update + , init = ( initialModel, searchFeed initialModel.query ) + , inputs = [] } - - -decodeResponse : Json.Decode.Value -> Msg -decodeResponse json = - case Json.Decode.decodeValue responseDecoder json of - Err err -> - SetErrorMessage (Just err) - - Ok results -> - SetResults results - - -port githubSearch : String -> Cmd msg - - -port githubResponse : (Json.Decode.Value -> msg) -> Sub msg diff --git a/part8/README.md b/part8/README.md index 3f95fac..f24f3da 100644 --- a/part8/README.md +++ b/part8/README.md @@ -26,3 +26,9 @@ cd test elm-package install elm test TestRunner.elm ``` + +## References + +* [Using Elm packages](https://github.com/elm-lang/elm-package/blob/master/README.md#basic-usage) +* [elm-test documentation](http://package.elm-lang.org/packages/deadfoxygrandpa/elm-test/3.1.1/) +* [`(<|)` documentation](http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Basics#<|) diff --git a/part8/index.html b/part8/index.html index f717a9a..93fc00c 100644 --- a/part8/index.html +++ b/part8/index.html @@ -4,36 +4,17 @@ ElmHub - + -
diff --git a/part8/test/.gitignore b/part8/test/.gitignore deleted file mode 100644 index d99f930..0000000 --- a/part8/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!Auth.elm diff --git a/part8/test/Tests.elm b/part8/test/Tests.elm index 03b8291..ad81e51 100644 --- a/part8/test/Tests.elm +++ b/part8/test/Tests.elm @@ -23,9 +23,8 @@ main = let response = """{ "items": [ - { "id": 5, "full_name": "foo", "stargazers_count": 42 }, - { "id": 3, "full_name": "bar", "stargazers_count": 77 } - ] }""" + /* TODO: dummy JSON goes here */ + ] }""" in Expect.equal (Ok @@ -41,14 +40,13 @@ main = """{ "pizza": [] }""" isErrorResult result = - case result of - Ok _ -> - False - - Err _ -> - True + -- TODO return True if the given Result is an Err of some sort, + -- and False if it is an Ok of some sort. + -- + -- Result docs: http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Result + False in - Expect.equal True + Expect.true "Expected decoding an invalid response to return an Err." (isErrorResult (decodeString responseDecoder response)) ] |> Runner.run emit