Ejemplo n.º 1
0
// Create a new Object from Base64 JSON encoded data.
func NewFromBase64(context *context.Context, b64 string) (*Object, error) {
	jsonBytes, err := base64.URLEncoding.DecodeString(fixPadding(b64))
	if err != nil {
		return nil, fmt.Errorf(
			"Failed base64 decode of string \"%s\" with error: %s", b64, err)
	}
	var strSlices [][]interface{}
	err = json.Unmarshal(jsonBytes, &strSlices)
	if err != nil {
		return nil, fmt.Errorf(
			"Failed json unmarshal string %s with error %s", string(jsonBytes), err)
	}

	object := &Object{context: context}
	for _, row := range strSlices {
		if len(row) != 2 {
			return nil, fmt.Errorf("Got more than two elements in pair: %v", row)
		}
		if row[0] == nil {
			return nil, fmt.Errorf("First element in pair is null: %v", row)
		}
		key := fmt.Sprint(row[0])
		val := ""
		switch t := row[1].(type) {
		case nil:
			object.skipGenerate = append(object.skipGenerate, key)
			continue
		case float64:
			val = fmt.Sprint(uint64(t))
		default:
			val = fmt.Sprint(t)
		}
		object.AddPair(key, val)
	}

	if object.shouldGenerate("og:url") {
		url := context.AbsoluteURL("/rog/" + b64).String()
		object.AddPair("og:url", url)
	}

	err = object.generateDefaults()
	if err != nil {
		return nil, err
	}
	return object, nil
}
Ejemplo n.º 2
0
// Create a new Object from query string data.
func NewFromValues(context *context.Context, values url.Values) (*Object, error) {
	object := &Object{context: context}
	for key, values := range values {
		if strings.Contains(key, ":") {
			for _, value := range values {
				object.AddPair(key, value)
			}
		}
	}

	if object.shouldGenerate("og:url") {
		copiedValues := copyValues(values)
		copiedValues.Del("og:type")
		copiedValues.Del("og:title")
		url := url.URL{
			Scheme:   context.Scheme,
			Host:     context.Host,
			Path:     "/og/" + object.Type() + "/" + object.Title(),
			RawQuery: sortedEncode(copiedValues),
		}
		object.AddPair("og:url", url.String())
	}

	ogType := object.Type()
	isGlobalOGType := !strings.Contains(ogType, ":")
	isOwnedOGType := strings.HasPrefix(ogType, context.AppNamespace()+":")
	if object.shouldGenerate("fb:app_id") && (isGlobalOGType || isOwnedOGType) {
		object.AddPair("fb:app_id", strconv.FormatUint(context.AppID, 10))
	}

	err := object.generateDefaults()
	if err != nil {
		return nil, err
	}
	return object, nil
}
Ejemplo n.º 3
0
func redirectURI(c *context.Context) string {
	return c.AbsoluteURL(Path + resp).String()
}