Пример #1
0
// Join takes a slice of strings (a metric "path") and returns a valid Statsd
// metric name. It cleans all parts by slug-ifying them.
func Join(parts ...string) (name string) {
	clean := make([]string, len(parts))
	for i := 0; i < len(parts); i++ {
		clean[i] = slug.Clean(parts[i])
	}
	return strings.Join(clean, ".")
}
Пример #2
0
func NewQueue(name string) *Queue {
	return &Queue{
		ID:        bson.NewObjectId(),
		Name:      name,
		Slug:      slug.Clean(name),
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		tableName: "queue",
	}
}
Пример #3
0
// todo fix slug generation with the slugifiable.coffee implementation
func Slugify(message *ChannelMessage) (*ChannelMessage, error) {

	if message.TypeConstant != ChannelMessage_TYPE_POST {
		return message, nil
	}

	// we want hypen between our words
	slug.Replacement = '-'
	res := NewChannelMessage()

	suggestedSlug := slug.Clean(message.Body)
	if len(suggestedSlug) > 80 {
		suggestedSlug = suggestedSlug[:79]
	}

	query := map[string]interface{}{
		"slug": suggestedSlug,
	}

	rand.Seed(time.Now().UnixNano())

	for tryCount := 0; tryCount < 10; tryCount++ {
		if err := res.One(bongo.NewQS(query)); err != nil {
			// if we got error from db, it means it couldnt find the
			// data, so we can return here
			if err != bongo.RecordNotFound {
				return nil, err
			}
			message.Slug = suggestedSlug
			return message, nil
		}
		// iterate with the new slug
		// this is not the best strategy to generate slug
		// but also not the worst
		suggestedSlug = suggestedSlug + "-" + strconv.Itoa(rand.Intn(1000000000))
		query["slug"] = suggestedSlug
	}

	return nil, fmt.Errorf("couldnt generate unique slug:%s", message.Slug)
}