An open API service indexing awesome lists of open source software.

https://github.com/invetica/uri

Specs, generators, and common utilities for URIs.
https://github.com/invetica/uri

clojure generative-testing spec specification uri

Last synced: 2 months ago
JSON representation

Specs, generators, and common utilities for URIs.

Awesome Lists containing this project

README

          

#+STARTUP: showall

*URI* makes working with URIs a little more convenient, without duplicating the
existing functionality provided by Java.

#+BEGIN_HTML





#+END_HTML

* Contents :TOC:
- [[#warning-data-readers-included][Warning! Data readers included]]
- [[#usage][Usage]]
- [[#manipulation][Manipulation]]
- [[#specs][Specs]]
- [[#license][License]]

* Warning! Data readers included
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.

#+begin_src clojure
{:user/name "Charlie Collie"
:user/website #invetica/uri "https://myspace.com/charlie.223"}
#+end_src

* Usage
** Manipulation
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.

#+begin_src clojure
(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"}
#+end_src

To convert a URI map back into a URI you can use ~invetica.uri/unparse~.

#+begin_src clojure
(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"
#+end_src

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:

#+begin_src clojure
(-> "https://example.com/foo?bar=baz#quux"
uri/parse
(assoc ::uri/path "/WOOT")
uri/unparse)
;; => #invetica/uri "https://example.com/WOOT?bar=baz#quux"
#+end_src

Don't forget to use the ~:invetica.uri~ namespace! ~:path~ would not work in the
above example.

** Specs
See the example in ~dev/invetica/uri/example.clj~.

The following specs are included:

#+begin_src clojure :exports none
(->> (clojure.spec.alpha/registry)
keys
(filter keyword?)
(filter #(= "invetica.uri" (namespace %)))
sort)
#+end_src

- ~: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~

* License
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.