Skip to content
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

Kadai4 ramenjuniti #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions kadai4/ramenjuniti/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BIN := gokuji

.PHONY: test
test:
go test -cover -v ./...

.PHONY: build
build: test
go build -o $(BIN)

.PHONY: clean
clean:
rm $(BIN)
go clean
23 changes: 23 additions & 0 deletions kadai4/ramenjuniti/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# gokuji

## Description

おみくじ API です。1/1 - 1/3 は大吉だけが出ます。

## Usage

```bash
./gokuji -p {ポート番号(デフォルトは8080)}
```

## Build

```bash
make build
```

## Test

```bash
make test
```
23 changes: 23 additions & 0 deletions kadai4/ramenjuniti/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"flag"
"fmt"
"net/http"
"time"

"github.com/gopherdojo/dojo5/kadai4/ramenjuniti/omikuji"
)

var port int

func init() {
flag.IntVar(&port, "p", 8080, "port number")
}

func main() {
flag.Parse()
o := omikuji.New(time.Now())
http.HandleFunc("/", o.Handler)
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
55 changes: 55 additions & 0 deletions kadai4/ramenjuniti/omikuji/omikuji.go
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"
)

// Omikuji contains time
type Omikuji struct {
time time.Time
}

// Result contains type of omikuji result
type Result struct {
Type string `json:"type"`
}

var types = []string{
"大吉",
"吉",
"中吉",
"小吉",
"末吉",
"凶",
"大凶",
}

// New returns Omikuji instance
func New(t time.Time) *Omikuji {
rand.Seed(t.UnixNano())
return &Omikuji{time: t}
}

// Handler returns omikuji result
func (o *Omikuji) Handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

result := draw(o.time)
if err := json.NewEncoder(w).Encode(result); err != nil {
log.Println("Error:", err)
}
}

func draw(t time.Time) *Result {
yd := t.YearDay()

if yd == 1 || yd == 2 || yd == 3 {
return &Result{Type: types[0]}
}

return &Result{Type: types[rand.Intn(len(types))]}
}
77 changes: 77 additions & 0 deletions kadai4/ramenjuniti/omikuji/omikuji_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package omikuji

import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)

var cases = []struct {
name string
time time.Time
}{
{
name: "case1",
time: time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "case2",
time: time.Date(2019, 1, 1, 23, 59, 59, 0, time.UTC),
},
{
name: "case3",
time: time.Date(2019, 1, 2, 0, 0, 0, 0, time.UTC),
},
{
name: "case4",
time: time.Date(2019, 1, 2, 23, 59, 59, 0, time.UTC),
},
{
name: "case5",
time: time.Date(2019, 1, 3, 0, 0, 0, 0, time.UTC),
},
{
name: "case6",
time: time.Date(2019, 1, 3, 23, 59, 59, 0, time.UTC),
},
}

func getResult(time time.Time, t *testing.T) *Result {
t.Helper()
o := New(time)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
o.Handler(w, r)
rw := w.Result()
defer rw.Body.Close()

if rw.StatusCode != http.StatusOK {
t.Fatal("unexpected status code")
}

b, err := ioutil.ReadAll(rw.Body)
if err != nil {
t.Fatal("unexpected error")
}

re := &Result{}
if err := json.Unmarshal(b, &re); err != nil {
t.Fatal("failed json unmarshal")
}

return re
}

func TestOmikuji(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
re := getResult(c.time, t)
if re.Type != "大吉" {
t.Errorf("got %v, want %v", re.Type, "大吉")
}
})
}
}