-
Couldn't load subscription status.
- Fork 0
Kadai4 matsumatsu20 #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84bd201
Impl omikuji without test
matsumatsu20 db06ebb
三が日の判断ロジックを切り出した
matsumatsu20 5d520f4
くじの種類の定義を連想配列から配列にした
matsumatsu20 73601a6
リファクタ
matsumatsu20 fba9c7c
Unitテストを書いた
matsumatsu20 2336ece
:lipstick:
matsumatsu20 9740de6
go imports
matsumatsu20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
kadai4/matsumatsu20/.gitignore
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Created by https://www.gitignore.io/api/go,intellij+all | ||
|
|
||
| ### Go ### | ||
| # Binaries for programs and plugins | ||
| *.exe | ||
| *.exe~ | ||
| *.dll | ||
| *.so | ||
| *.dylib | ||
| matsukuji | ||
|
|
||
| # Test binary, build with `go test -c` | ||
| *.test | ||
|
|
||
| # Output of the go coverage tool, specifically when used with LiteIDE | ||
| *.out | ||
|
|
||
| ### Go Patch ### | ||
| /vendor/ | ||
| /Godeps/ | ||
|
|
||
| ### Intellij+all ### | ||
| # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm | ||
| # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
|
||
| # User-specific stuff | ||
| .idea/**/workspace.xml | ||
| .idea/**/tasks.xml | ||
| .idea/**/usage.statistics.xml | ||
| .idea/**/dictionaries | ||
| .idea/**/shelf | ||
|
|
||
| # Sensitive or high-churn files | ||
| .idea/**/dataSources/ | ||
| .idea/**/dataSources.ids | ||
| .idea/**/dataSources.local.xml | ||
| .idea/**/sqlDataSources.xml | ||
| .idea/**/dynamic.xml | ||
| .idea/**/uiDesigner.xml | ||
| .idea/**/dbnavigator.xml | ||
|
|
||
| # Gradle | ||
| .idea/**/gradle.xml | ||
| .idea/**/libraries | ||
|
|
||
| # Gradle and Maven with auto-import | ||
| # When using Gradle or Maven with auto-import, you should exclude module files, | ||
| # since they will be recreated, and may cause churn. Uncomment if using | ||
| # auto-import. | ||
| # .idea/modules.xml | ||
| # .idea/*.iml | ||
| # .idea/modules | ||
|
|
||
| # CMake | ||
| cmake-build-*/ | ||
|
|
||
| # Mongo Explorer plugin | ||
| .idea/**/mongoSettings.xml | ||
|
|
||
| # File-based project format | ||
| *.iws | ||
|
|
||
| # IntelliJ | ||
| out/ | ||
|
|
||
| # mpeltonen/sbt-idea plugin | ||
| .idea_modules/ | ||
|
|
||
| # JIRA plugin | ||
| atlassian-ide-plugin.xml | ||
|
|
||
| # Cursive Clojure plugin | ||
| .idea/replstate.xml | ||
|
|
||
| # Crashlytics plugin (for Android Studio and IntelliJ) | ||
| com_crashlytics_export_strings.xml | ||
| crashlytics.properties | ||
| crashlytics-build.properties | ||
| fabric.properties | ||
|
|
||
| # Editor-based Rest Client | ||
| .idea/httpRequests | ||
|
|
||
| ### Intellij+all Patch ### | ||
| # Ignores the whole .idea folder and all .iml files | ||
| # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 | ||
| .idea/ | ||
|
|
||
| # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 | ||
| *.iml | ||
| modules.xml | ||
| .idea/misc.xml | ||
| *.ipr | ||
|
|
||
| # End of https://www.gitignore.io/api/go,intellij+all |
15 changes: 15 additions & 0 deletions
kadai4/matsumatsu20/Makefile
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Go パラメータ | ||
| GOCMD=go | ||
| GOBUILD=$(GOCMD) build | ||
| GOCLEAN=$(GOCMD) clean | ||
| GOTEST=$(GOCMD) test | ||
| BINARY_NAME=matsukuji | ||
| all: test build | ||
| build: | ||
| $(GOBUILD) -o $(BINARY_NAME) -v | ||
| test: | ||
| $(GOTEST) -v ./... | ||
| clean: | ||
| $(GOCLEAN) | ||
| rm -f $(BINARY_NAME) | ||
| rm -f $(BINARY_UNIX) |
16 changes: 16 additions & 0 deletions
kadai4/matsumatsu20/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # おみくじAPIを作ってみよう | ||
|
|
||
| ## 課題事項 | ||
| - [x] JSON形式でおみくじの結果を返す | ||
| - [x] 正月(1/1-1/3)だけ大吉にする | ||
| - [x] ハンドラのテストを書いてみる | ||
|
|
||
| ## Usage | ||
| ### build | ||
| ```bash | ||
| % make build | ||
| ``` | ||
| ### run | ||
| ```bash | ||
| % ./matsukuji | ||
| ``` |
22 changes: 22 additions & 0 deletions
kadai4/matsumatsu20/dateUtil/dateUtil.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package dateUtil | ||
|
|
||
| import ( | ||
| "log" | ||
| "time" | ||
| ) | ||
|
|
||
| var timeNow = time.Now() | ||
|
|
||
| func IsNewYearsHoliday() bool { | ||
| jst, err := time.LoadLocation("Asia/Tokyo") | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
|
|
||
| _, month, day := timeNow.In(jst).Date() | ||
| if month == time.January && (day == 1 || day == 2 || day == 3) { | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| } |
36 changes: 36 additions & 0 deletions
kadai4/matsumatsu20/dateUtil/dateUtil_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package dateUtil | ||
|
|
||
| import ( | ||
| "log" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestIsNewYearsHoliday(t *testing.T) { | ||
| jst, err := time.LoadLocation("Asia/Tokyo") | ||
| if err != nil { | ||
| log.Println(err) | ||
| } | ||
|
|
||
| cases := []struct { | ||
| year int | ||
| month time.Month | ||
| day int | ||
| expected bool | ||
| }{ | ||
| {2018, 12, 31, false}, | ||
| {2019, 1, 1, true}, | ||
| {2019, 1, 2, true}, | ||
| {2019, 1, 3, true}, | ||
| {2019, 1, 4, false}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| timeNow = time.Date(c.year, c.month, c.day, 0, 0, 0, 0, jst) | ||
| actual := IsNewYearsHoliday() | ||
|
|
||
| if actual != c.expected { | ||
| t.Errorf("%v is expected, but returned %v", c.expected, actual) | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
kadai4/matsumatsu20/main.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "log" | ||
| "net/http" | ||
|
|
||
| "github.com/gopherdojo/dojo3/kadai4/matsumatsu20/omikuji" | ||
| ) | ||
|
|
||
| var port = flag.String("p", "8080", "listen port") | ||
|
|
||
| func main() { | ||
| http.HandleFunc("/kuji", omikuji.Handler) | ||
|
|
||
| if err := http.ListenAndServe(":"+*port, nil); err != nil { | ||
| log.Fatal("ListenAndServe: ", err) | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
kadai4/matsumatsu20/omikuji/omikuji.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package omikuji | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
| "math/rand" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/gopherdojo/dojo3/kadai4/matsumatsu20/dateUtil" | ||
| ) | ||
|
|
||
| type response struct { | ||
| Status int `json:"status"` | ||
| Result string `json:"result"` | ||
| } | ||
|
|
||
| const ( | ||
| daikichi = "大吉" | ||
| kichi = "吉" | ||
| chukichi = "中吉" | ||
| shokichi = "小吉" | ||
| suekichi = "末吉" | ||
| kyo = "凶" | ||
| daikyo = "大凶" | ||
| ) | ||
|
|
||
| var ( | ||
| luck = []string{daikichi, kichi, chukichi, shokichi, suekichi, kyo, daikyo} | ||
| isNewYearsHolidayFunc = dateUtil.IsNewYearsHoliday | ||
| fetchKujiFunc = fetchKuji | ||
| ) | ||
|
|
||
| func Handler(w http.ResponseWriter, r *http.Request) { | ||
| var result string | ||
|
|
||
| if isNewYearsHolidayFunc() { | ||
| result = daikichi | ||
| } else { | ||
| rand.Seed(time.Now().UnixNano()) | ||
| result = fetchKujiFunc() | ||
| } | ||
|
|
||
| res := &response{Status: 200, Result: result} | ||
|
|
||
| encoder := json.NewEncoder(w) | ||
| if err := encoder.Encode(res); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func fetchKuji() string { | ||
| rand.Seed(time.Now().UnixNano()) | ||
| return luck[rand.Intn(len(luck))] | ||
| } |
56 changes: 56 additions & 0 deletions
kadai4/matsumatsu20/omikuji/omikuji_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package omikuji | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestHandler(t *testing.T) { | ||
| ts := httptest.NewServer(http.HandlerFunc(Handler)) | ||
| defer ts.Close() | ||
|
|
||
| cases := []struct { | ||
| luck string | ||
| isNewYearsHoliday bool | ||
| expected response | ||
| }{ | ||
| {daikichi, false, response{200, "大吉"}}, | ||
| {kyo, false, response{200, "凶"}}, | ||
| {shokichi, true, response{200, "大吉"}}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| fetchKujiFunc = func() string { | ||
| return c.luck | ||
| } | ||
| isNewYearsHolidayFunc = func() bool { | ||
| return c.isNewYearsHoliday | ||
| } | ||
|
|
||
| res, err := http.Get(ts.URL) | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| body, err := ioutil.ReadAll(res.Body) | ||
|
|
||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if res.StatusCode != 200 { | ||
| t.Errorf("expedted status %v, but returned %v", c.expected, 200) | ||
| } | ||
|
|
||
| var actual response | ||
| json.Unmarshal(body, &actual) | ||
|
|
||
| if actual != c.expected { | ||
| t.Errorf("expedted %v, but returned %v", c.expected, actual) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.