Ejemplo n.º 1
0
func JsonEncoder(next func(map[string]string) interface{}) func(http.ResponseWriter, *http.Request) {
	return func(writer http.ResponseWriter, request *http.Request) {
		writer.Header().Set("Content-Type", "application/json")

		result := next(Variables(request))
		bytes, error := json.Marshal(result)

		if error != nil {
			log.Warn("encountered error marshelling object for json: %v", error)
		}

		fmt.Fprint(writer, string(bytes))
	}
}
Ejemplo n.º 2
0
func RegisterObjectClick(variables map[string]string) interface{} {
	objectId := variables["object_id"]
	userId := variables["X-Clikr-User"]

	sceneObject, ok := SceneObjects[objectId]
	if !ok {
		log.Warn("attempt to register object click with invalid object id: '%v'", objectId)
		return []*SceneObject{}
	}

	log.Debug("user '%v' register click on object '%v:%v'", userId, sceneObject.Name, objectId)

	// mark the object as liked for this user, will strength bond between other
	// liked objects.
	LikeObject(userId, sceneObject)

	bonds, ok := Bonds[sceneObject]
	if !ok {
		log.Debug("no bonds available for object: '%v'", objectId)
		return []*SceneObject{}
	}

	maxSize := 3 // maximum number of recommendations to return
	size := int(math.Min(float64(len(bonds)), float64(maxSize)))
	out := make([]*SceneObject, size, size)

	for i, b := range bonds {
		log.Debug("recommending '%v' with bond strength '%v'", b.With.Name, b.Strength)
		out[i] = b.With
		if i == (maxSize - 1) {
			break
		}
	}

	return out
}