Exemplo n.º 1
0
// NewContext generates a new context with randomly populated content
func NewContext() Context {
	ctx := Context{}
	smokingChoices := []randutil.Choice{
		{2, "Smoker"},
		{3, "Non-smoker"},
		{1, "Ex-smoker"}}
	sc, _ := randutil.WeightedChoice(smokingChoices)
	ctx.Smoker = sc.Item.(string)

	alcoholChoices := []randutil.Choice{
		{2, "Occasional"},
		{1, "Heavy"},
		{1, "None"}}
	ac, _ := randutil.WeightedChoice(alcoholChoices)
	ctx.Alcohol = ac.Item.(string)

	cholesterolChoices := []randutil.Choice{
		{3, "Optimal"},
		{1, "Near Optimal"},
		{2, "Borderline"},
		{1, "High"},
		{2, "Very High"}}
	cc, _ := randutil.WeightedChoice(cholesterolChoices)
	ctx.Cholesterol = cc.Item.(string)

	hc, _ := randutil.ChoiceString([]string{"Normal", "Pre-hypertension", "Hypertension"})
	ctx.Hypertention = hc

	dc, _ := randutil.ChoiceString([]string{"Normal", "Pre-diabetes", "Diabetes"})
	ctx.Diabetes = dc
	return ctx
}
Exemplo n.º 2
0
// Generate returns a new value for a token according to the definition.
func (t URLToken) Generate() (r string) {
	if t.Choices != "" {
		r, _ = randutil.ChoiceString(strings.Split(t.Choices, "|"))
	}
	switch t.Pattern {
	case "alpha":
		r, _ = randutil.StringRange(t.Min, t.Max, randutil.Alphabet)
	case "alphanum":
		r, _ = randutil.AlphaStringRange(t.Min, t.Max)
	case "num":
		rInt, _ := randutil.IntRange(t.Min, t.Max)
		r = strconv.FormatInt(int64(rInt), 10)
	}
	return
}
Exemplo n.º 3
0
// Decide receives a JSON payload containing several strings, and returns a JSON
// message containing one of said strings, selected at random.
func Decide(w http.ResponseWriter, req *http.Request) {
	//
	// Parse Payload
	//
	if req.ContentLength <= 0 {
		msg := "Content-Length must be greater than 0."
		http.Error(w, msg, http.StatusLengthRequired)
		return
	}
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	var dreq DecisionRequest
	err = json.Unmarshal(body, &dreq)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	if dreq.Quandary == "" {
		msg := "Must supply a quandary"
		http.Error(w, msg, http.StatusBadRequest)
		return
	}
	//
	// Discard empty choices
	//
	validChoices := []string{}
	for _, choice := range dreq.Choices {
		if choice.Text != "" {
			validChoices = append(validChoices, choice.Text)
		}
	}
	if len(validChoices) < 2 {
		msg := fmt.Sprintln("Must provide at least 2 choices, but you provided", len(validChoices))
		http.Error(w, msg, http.StatusBadRequest)
		return
	}
	//
	// Make the decision
	//
	winner, err := randutil.ChoiceString(validChoices)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	//
	// Save to MongoDB
	//
	c := mongo.C("quandaries")
	ip := req.Header.Get("X-Forwarded-For")
	if ip != "" {
		ip = strings.Split(ip, ",")[0]
	} else {
		ip = strings.Split(req.RemoteAddr, ":")[0]
	}
	d := Decision{
		Quandary:  dreq.Quandary,
		Choices:   validChoices,
		Winner:    winner,
		Ip:        ip,
		Timestamp: time.Now(),
		UserAgent: req.UserAgent(),
	}
	err = c.Insert(&d)
	if err != nil {
		msg := "MongoDB Error: " + err.Error()
		http.Error(w, msg, http.StatusInternalServerError)
		return
	}
	//
	// Save to Neo4j
	//

	//
	// Generate response
	//
	dres := DecisionResponse{winner}
	blob, err := json.Marshal(dres)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	req.Header.Add("content-type", "application/json")
	w.Write(blob)
}