public ILinkObject ResolveTemplated(Func <UriTemplate, string> hrefResolver)
        {
            if (!Templated)
            {
                throw new InvalidOperationException("Cannot resolve a non-Templated link");
            }

            var template = new UriTemplate(Template);
            var href     = hrefResolver(template);

            var link = new LinkObject
            {
                Rel         = Rel,
                Templated   = false,
                Deprecation = Deprecation,
                HrefLang    = HrefLang,
                Name        = Name,
                Profile     = Profile,
                Title       = Title,
                Type        = Type
            };

            link.SetHref(href);

            return(link);
        }
Exemple #2
0
        private static LinkObject ParseLinkObject(JObject outer, string rel)
        {
            var link = new LinkObject {
                Rel = rel
            };
            string href = null;

            foreach (var inner in outer.Properties())
            {
                var value = inner.Value.ToString();

                if (string.IsNullOrEmpty(value))
                {
                    continue;                     // nothing to assign, just leave the default value ...
                }
                var attribute = inner.Name.ToLowerInvariant();

                switch (attribute)
                {
                case "href":
                    href = value;
                    break;

                case "templated":
                    link.Templated = value.Equals("true", StringComparison.OrdinalIgnoreCase);
                    break;

                case "type":
                    link.Type = value;
                    break;

                case "deprecation":
                    link.SetDeprecation(value);
                    break;

                case "name":
                    link.Name = value;
                    break;

                case "profile":
                    link.SetProfile(value);
                    break;

                case "title":
                    link.Title = value;
                    break;

                case "hreflang":
                    link.HrefLang = value;
                    break;

                default:
                    throw new NotSupportedException("Unsupported link attribute encountered: " + attribute);
                }
            }

            if (link.Templated)
            {
                link.Template = href;
            }
            else
            {
                link.SetHref(href);
            }

            return(link);
        }