URI makes working with URIs a little more convenient, without duplicating the existing functionality provided by Java.
This library includes a reader literal that will affect the way Clojure writes
instances of java.net.URI
. If your do not want a URI to end up looking like
the example below, do not add this to your codebase.
{:user/name "Charlie Collie"
:user/website #invetica/uri "https://myspace.com/charlie.223"}
To make is easier to modify URIs we have a invetica.uri/parse
function that
will convert something URI-like into a persistent hash map.
(require '[invetica.uri :as uri])
(uri/parse "https://rich:S3creT@example.com:9999/foo?a=1&a=2")
;; => #:invetica.uri{:absolute? true
;; :host "example.com"
;; :password "S3creT"
;; :path "/foo"
;; :port 9999
;; :query-string "a=1&a=2"
;; :scheme "https"
;; :user "rich"
;; :user-info "rich:S3creT"}
To convert a URI map back into a URI you can use invetica.uri/unparse
.
(uri/unparse #:invetica.uri{:absolute? true
:host "example.com"
:password "S3creT"
:path "/foo"
:port 9999
:query-string "a=1&a=2"
:scheme "https"
:user "rich"
:user-info "rich:S3creT"})
;; => #invetica/uri "https://rich:S3creT@example.com:9999/foo?a=1&a=2"
This makes it’s easy to change any part of the URI using familiar Clojure
functions like assoc
, merge
, update
, etc. An example of changing the path
in a URI looks like this:
(-> "https://example.com/foo?bar=baz#quux"
uri/parse
(assoc ::uri/path "/WOOT")
uri/unparse)
;; => #invetica/uri "https://example.com/WOOT?bar=baz#quux"
Don’t forget to use the :invetica.uri
namespace! :path
would not work in the
above example.
See the example in dev/invetica/uri/example.clj
.
The following specs are included:
:invetica.uri/absolute-uri
:invetica.uri/absolute-uri-str
:invetica.uri/absolute?
:invetica.uri/authority
:invetica.uri/domain-part
:invetica.uri/fragment
:invetica.uri/host
:invetica.uri/hostname
:invetica.uri/ipv4
:invetica.uri/ipv6
:invetica.uri/octet
:invetica.uri/password
:invetica.uri/path
:invetica.uri/port
:invetica.uri/query-string
:invetica.uri/relative-uri
:invetica.uri/relative-uri-str
:invetica.uri/scheme
:invetica.uri/uri-map
:invetica.uri/user
:invetica.uri/user-info
The MIT License (MIT)
Copyright © 2017 Invetica Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.