Add `Uri::toRelative(base: Uri): Uri` method
- Dominant language
- Scala
- Stars
- 1.4k
- Forks
- 584
- Avg merge
- 14h 33m
- Merged PRs (30d)
- 24
Description
**Issue by [bblfish](https://github.com/bblfish)**
_Friday Jul 04, 2014 at 09:08 GMT_
_Originally opened as https://github.com/akka/akka/issues/15495_
---
I reported this as [Spray issue 818](https://github.com/spray/spray/issues/818) as the move to akka was underway. So I'll add a reference here.
Currently one can make a Uri relative to the root.
``` scala
**
* Converts this URI into a relative URI by keeping the path, query and fragment, but dropping the scheme and authority.
*/
def toRelative =
Uri(path = if (path.isEmpty) Uri.Path./ else path, query = query, fragment = fragment)
```
It would be useful to have the same functionality as [java.net.URI#relativize(URI)](http://docs.oracle.com/javase/6/docs/api/java/net/URI.html#relativize%28java.net.URI%29) to allow easy relative URI creation.
``` scala
scala> import java.net.URI
import java.net.URI
scala> val card = new URI("http://bblfish.net/people/henry/card#me")
card: java.net.URI = http://bblfish.net/people/henry/card#me
scala> val base1 = new URI("http://bblfish.net/people/")
base1: java.net.URI = http://bblfish.net/people/
scala> base1.relativize(card)
res0: java.net.URI = henry/card#me
scala> new URI("http://bblfish.net/people/henry/").relativize(card)
res1: java.net.URI = card#me
scala> new URI("http://bblfish.net/people/henry/card").relativize(card)
res2: java.net.URI = #me
```
It is often very useful to serialise to relative uris when a base is known. So when one does a GET on
a URI such as http://bblfish.net/people/henry/card it is much easier to read the result when it says
``` Turtle
<> a foaf:PersonalProfileDocument;
foaf:primaryTopic .
```
rather than writing out all the Uris in full. Because a relative URI such as "" ( or <> and in the above ) allows a human to immediatly understand that the document is speaking of itself. A full URI requires a human to do a full comparison.
So we need this a lot in serialisations. Having this feature would allow banana-rdf for example to [move totally to akka-http](https://github.com/w3c/banana-rdf/issues/88), which would also make it easier then to write code that can compile with scala-js.
As a matter of interest I wonder if it would be possible to make type distinctions between full and relative Uris. (Full Uris being a subclass of relative Uris perhaps). This would help make a large class of errors visible to the compiler, as explained in [issue #41 of banana-rdf](https://github.com/w3c/banana-rdf/issues/41). Perhaps that should be for a different bug report?
Contributor guide
Research direction
Start with the existing Uri.toRelative method and compare its behavior with the java.net.URI#relativize examples in the issue. Add Uri::toRelative(base: Uri): Uri so the shown base and card combinations produce the corresponding relative URIs, then verify the behavior with focused tests covering those examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100