Skip to content
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
52 changes: 52 additions & 0 deletions kadai4/execjosh/internal/jinja/jinja.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jinja

import (
"math/rand"
"sync"
"time"
)

var blessings = []string{
"大吉",
"吉",
"中吉",
"小吉",
"半吉",
"末吉",
"末小吉",
"平",
"凶",
"小凶",
"半凶",
"末凶",
"大凶",
}
var blessingCount = len(blessings)

// A Jinja that provides random omikuji
type Jinja struct {
lk sync.Mutex
rng *rand.Rand
}

// New creates and initializes a new Jinja
func New(seed int64) *Jinja {
return &Jinja{
rng: rand.New(rand.NewSource(seed)),
}
}

// GetBlessing returns an blessing based on the current time.
// For 1/1-1/3, it returns always "大吉".
// For all other dates, it returns a random blessing.
func (m *Jinja) GetBlessing(now time.Time) string {
if now.YearDay() <= 3 {
return "大吉"
}

m.lk.Lock()
n := m.rng.Intn(blessingCount)
m.lk.Unlock()

return blessings[n]
}
46 changes: 46 additions & 0 deletions kadai4/execjosh/internal/jinja/jinja_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package jinja_test

import (
"fmt"
"testing"
"time"

"github.com/gopherdojo/dojo1/kadai4/execjosh/internal/jinja"
)

func TestGetBlessing(t *testing.T) {
expectedValues := []struct {
now time.Time
seed int64
expected []string
}{
{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}},

{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}},

{time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}},
{time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}},

{time.Date(2018, time.May, 4, 0, 0, 0, 0, time.UTC), 1234567890, []string{"末小吉", "末吉", "末小吉", "末凶"}},
{time.Date(2018, time.May, 17, 0, 0, 0, 0, time.UTC), 123567890, []string{"平", "平", "半吉", "末吉"}},
}

for _, tc := range expectedValues {
tc := tc
j := jinja.New(tc.seed)
for _, expected := range tc.expected {
expected := expected
j := j
actual := j.GetBlessing(tc.now)
if expected != actual {
fmt.Printf("expected '%v' but got '%v'\n", expected, actual)
t.Fail()
}
}
}
}
6 changes: 6 additions & 0 deletions kadai4/execjosh/internal/jinja/omikuji.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package jinja

// An Omikuji holds a blessing
type Omikuji struct {
Blessing string `json:"blessing"`
}
21 changes: 21 additions & 0 deletions kadai4/execjosh/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"encoding/json"
"net/http"
"time"

"github.com/gopherdojo/dojo1/kadai4/execjosh/internal/jinja"
)

func main() {
j := jinja.New(time.Now().UnixNano())
handler := func(w http.ResponseWriter, r *http.Request) {
b := j.GetBlessing(time.Now())
mkj := &jinja.Omikuji{Blessing: b}
json.NewEncoder(w).Encode(mkj)
}

http.HandleFunc("/omikuji", handler)
http.ListenAndServe(":8080", nil)
}