From 8b0be35c6ff96bca2762c7fe2830ce4106acb62e Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 4 Sep 2014 15:26:39 -0400 Subject: [PATCH] add function 'parse-url' which auto determines whether the url is absolute or relative and parses appropriately --- src/clj_hl7_fhir/core.clj | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/clj_hl7_fhir/core.clj b/src/clj_hl7_fhir/core.clj index 5574fd9..54b4a9e 100644 --- a/src/clj_hl7_fhir/core.clj +++ b/src/clj_hl7_fhir/core.clj @@ -239,6 +239,33 @@ :id (second no-version-url-parts)}) ))) +(defn absolute-url? + "returns true if the passed URL is an absolute URL, false if not. if the value + passed in is not a string (or an empty string) an exception is thrown." + [^String resource-url] + (if (and (string? resource-url) + (not (str/blank? resource-url))) + (boolean + (try + (url resource-url) + (catch Exception ex))) + (throw (new Exception "Invalid URL or non-string value.")))) + +(defn parse-url + "parses a FHIR resource URL returning a map containing each of the discrete + components of the URL (resource type, id, version number). if the optional + keywordize? arg is true, then returned resource type names will be turned into + a \"kebab case\" keyword (as opposed to a camelcase string which is the default). + if the URL cannot be parsed, returns nil. + + this function will automatically determine if the URL is relative or absolute + and will try to parse it appropriately. you should probably just use this + function all the time when parsing FHIR resource URLs." + [resource-url & [keywordize?]] + (if (absolute-url? resource-url) + (parse-absolute-url resource-url keywordize?) + (parse-relative-url resource-url keywordize?))) + (defn absolute->relative-url "turns an absolute FHIR resource URL into a relative one." [absolute-url] @@ -257,18 +284,6 @@ (url) (.toString)))) -(defn absolute-url? - "returns true if the passed URL is an absolute URL, false if not. if the value - passed in is not a string (or an empty string) an exception is thrown." - [^String resource-url] - (if (and (string? resource-url) - (not (str/blank? resource-url))) - (boolean - (try - (url resource-url) - (catch Exception ex))) - (throw (new Exception "Invalid URL or non-string value.")))) - (defn collect-resources "returns a sequence containing all of the resources contained in the given bundle. deleted resources listed in the bundle will not be included in the returned