<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Tomasz Pluskiewicz]]></title>
  <link href="http://t-code.pl/atom.xml" rel="self"/>
  <link href="http://t-code.pl/"/>
  <updated>2021-03-28T12:28:09+00:00</updated>
  <id>http://t-code.pl/</id>
  <author>
    <name><![CDATA[Tomasz Pluskiewicz]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Hydra and SHACL - part 2 - IRI Templates]]></title>
    <link href="http://t-code.pl/blog/2020/12/hydra-shacl-templates/"/>
    <updated>2020-12-28T00:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2020/12/hydra-shacl-templates</id>
    <content type="html"><![CDATA[<p>In the <a href="http://t-code.pl/blog/2020/12/hydra-shacl-interoperability">previous post</a> I presented the simplest functionality of loading remote form contents by having <a href="https://www.w3.org/TR/shacl/">SHACL</a> property shape reference a <a href="http://www.hydra-cg.com/spec/latest/core/">Hydra Core</a> collection.</p>

<p>In the second part I will extend that example to create a form with multiple connected dropdowns, where each one is only populated when other(s) have been selected, which is a common scenario seen in (web) applications.</p>

<!--more-->

<h2 id="tldr-can-i-see-it-working">TL;DR; can I see it working?</h2>

<p>The screenshot below links to Shaperone Playground which implements the ideas described in the subsequent paragraphs.</p>

<p><a href="https://forms.hypermedia.app/playground?resource=%7B%0A++%22%40context%22%3A+%7B%0A++++%22rdf%22%3A+%22http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%22%2C%0A++++%22rdfs%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%22%2C%0A++++%22xsd%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%22%2C%0A++++%22schema%22%3A+%22http%3A%2F%2Fschema.org%2F%22%2C%0A++++%22foaf%22%3A+%22http%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%22%2C%0A++++%22vcard%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2006%2Fvcard%2Fns%23%22%0A++%7D%2C%0A++%22%40id%22%3A+%22http%3A%2F%2Fexample.com%2Fjohn-doe%22%2C%0A++%22%40type%22%3A+%22schema%3APerson%22%2C%0A++%22schema%3AaddressCountry%22%3A+%7B%0A++++%22%40id%22%3A+%22http%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ39%22%0A++%7D%0A%7D&amp;selectedResource=http%3A%2F%2Fexample.com%2Fjohn-doe&amp;shapes=%40prefix+sh%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fshacl%23%3E+.%0A%40prefix+schema%3A+%3Chttp%3A%2F%2Fschema.org%2F%3E+.%0A%40prefix+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E+.%0A%40prefix+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E+.%0A%40prefix+dash%3A+%3Chttp%3A%2F%2Fdatashapes.org%2Fdash%23%3E+.%0A%40prefix+hydra%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fhydra%2Fcore%23%3E+.%0A%40prefix+wdt%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E+.%0A%40prefix+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E+.%0A%0A%40prefix+ex%3A+%3Chttp%3A%2F%2Fexample.com%2F%3E+.%0A%0Aschema%3APerson%0A++a+rdfs%3AClass%2C+sh%3ANodeShape+%3B%0A++rdfs%3Alabel+%22Register%22+%3B%0A++sh%3Aproperty+ex%3ANameProperty+%2C+%0A%09%09++++++ex%3ACountryProperty+%2C+%0A%09%09%09++ex%3AStateProperty+%2C%0A++++++++++++++ex%3ASubStateProperty+%3B%0A.%0A%0Aex%3ANameProperty%0A++sh%3Apath+schema%3Aname+%3B%0A++sh%3Aname+%22Name%22+%3B%0A++sh%3Adatatype+xsd%3Astring+%3B%0A++dash%3AsingleLine+true+%3B%0A++sh%3AmaxCount+1+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3Aorder+10+%3B%0A.%0A%0Aex%3ACountryProperty%0A++sh%3Aname+%22Country%22+%3B%0A++sh%3Aclass+wd%3AQ6256+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++sh%3Apath+schema%3AaddressCountry+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++hydra%3Acollection+%3Chttps%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3Dprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcountry%2520.%250A%2520%2520%253Fcountry%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%250A%2520%2520%253Fcountry%2520wdt%253AP31%2520wd%253AQ6256%2520%253B%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%3E+%3B%0A++sh%3Aorder+20+%3B%0A.%0A%0A%0Aex%3AStateProperty%0A++sh%3Aname+%22State%22+%3B%0A++sh%3Apath+schema%3AaddressRegion+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++hydra%3Asearch+%5B%0A++++hydra%3Atemplate+%22https%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3DPREFIX%2520wd%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fentity%252F%253E%250APREFIX%2520wdt%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fprop%252Fdirect%252F%253E%250Aprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fdivision%2520.%250A%2520%2520%253Fdivision%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%2520%2520%250A%2520%2520%253C%7BCOUNTRY%7D%253E%2520wdt%253AP150%2520%253Fdivision%2520.%250A%2520%2520%253Fdivision%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%22+%3B%0A++++hydra%3Amapping+%5B%0A++++++hydra%3Aproperty+schema%3AaddressCountry+%3B%0A++++++hydra%3Avariable+%22COUNTRY%22+%3B%0A++++++hydra%3Arequired+true+%3B%0A++++%5D+%3B%0A++%5D+%3B%0A++sh%3Aorder+30+%3B%0A.%0A%0A%0Aex%3ASubStateProperty%0A++sh%3Aname+%22City%22+%3B%0A++sh%3Apath+schema%3AaddressLocality+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++hydra%3Asearch+%5B%0A++++hydra%3Atemplate+%22https%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3DPREFIX%2520wd%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fentity%252F%253E%250APREFIX%2520wdt%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fprop%252Fdirect%252F%253E%250Aprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcity%2520.%250A%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%2520%250A%2520%2520%257B%250A%2520%2520%2520%2520SELECT%2520%253Fcity%2520%253Flabel%2520WHERE%2520%257B%250A%2520%2520%2520%2520%2520%2520%253C%7BSTATE%7D%253E%2520wdt%253AP150%252B%2520%253Fcity%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520wdt%253AP31%2520%253FcityType%2520.%250A%2520%2520%2520%2520%2520%2520%253FcityType%2520wdt%253AP279%2520wd%253AQ515%2520.%250A%2520%2520%2520%2520%257D%250A%2520%2520%257D%250A%2520%2520UNION%250A%2520%2520%257B%250A%2520%2520%2520%2520SELECT%2520%253Fcity%2520%253Flabel%2520WHERE%2520%257B%2520%2520%2520%2520%250A%2520%2520%2520%2520%2520%2520%253C%7BSTATE%7D%253E%2520wdt%253AP150%252B%2520%253Fcity%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520wdt%253AP31%2520wd%253AQ515%2520.%250A%2520%2520%2520%2520%257D%250A%2520%2520%257D%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%22+%3B%0A++++hydra%3Amapping+%5B%0A++++++hydra%3Aproperty+schema%3AaddressRegion+%3B%0A++++++hydra%3Avariable+%22STATE%22+%3B%0A++++++hydra%3Arequired+true+%3B%0A++++%5D+%3B%0A++%5D+%3B%0A++sh%3Aorder+40+%3B%0A."><img src="http://t-code.pl/images/shaperone/hydra-search.png" alt="shaperone playground" /></a></p>

<h2 id="filtering-collections-with-hydra">Filtering collections with Hydra</h2>

<p>In addition to <code>hydra:collection</code>, the Hydra Core vocabulary comes with another general-purpose property <code>hydra:search</code>. Unlike most predicates which would link to another resource, identified by a concrete URI, its objects are instances of URI Templates, defined by <a href="https://tools.ietf.org/html/rfc6570">RFC6570</a>.</p>

<p>For example, let’s have a “State collection” resource which returns country’s first-level administrative division. It would come with a search template so that clients can construct filtered URIs:</p>

<rdf-snippet formats="text/turtle,application/n-quads" prefixes="schema,hydra">
<script type="application/ld+json">
{
  "@context": {
    "@vocab": "http://www.w3.org/ns/hydra/core#",
    "schema": "http://schema.org/",
    "property": { "@type": "@id" }
  },
  "@id": "/states",
  "@type": "Collection",
  "search": {
    "@type": "IriTemplate",
    "template": "/states?country={country}",
    "mapping": [{
      "variable": "country",
      "property": "schema:addressCountry",
      "required": true
    }]
  }
}
</script>
</rdf-snippet>

<p>The client must provide template values to a Hydra library which will return a URI fit for dereferencing. This is called <code>expansion</code> by the RFC6570. A Hydra client will take a graph node with values being attached to that node using the <code>hydra:property</code> as defined by the template and match those property/object pairs to the template variables.</p>

<p>Here’s an example of such a template variable model, where JSON-LD <code>@context</code> has been constructed from the <code>hydra:mapping</code>, although the JSON keys may be irrelevant for the expansion if the implementation only relies on the actual graph data.</p>

<rdf-snippet formats="text/turtle,application/n-quads" prefixes="schema,hydra">
<script type="application/ld+json">
{
  "@context": {
    "schema": "http://schema.org/",
    "country": "schema:addressCountry"
  },
  "country": { "@id": "http://www.wikidata.org/entity/Q27" }
}
</script>
</rdf-snippet>

<p>Combine this with the template above to get</p>

<blockquote>
  <p><code>/states?country=http%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ27</code></p>
</blockquote>

<p>Read more about Hydra’s template <a href="http://www.hydra-cg.com/spec/latest/core/#templated-links">here</a></p>

<h2 id="connecting-form-fields">Connecting form fields</h2>

<p>The idea is simple:</p>

<ol>
  <li>A SHACL Shape describes a graph structure</li>
  <li>A form can be generated for agents (usu. humans) to create an instance of such a graph</li>
  <li>Use the created graph to expand a template</li>
</ol>

<p>Now, a form in such a scenario could simply be used to filter a collection for display, but I propose to short-circuit it back into the form itself so that the filtered collection, when dereferenced, provides values for other fields.</p>

<rdf-snippet formats="application/ld+json,application/n-quads" prefixes="schema,hydra,sh,dash">
<script type="text/turtle">
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix schema: <http://schema.org/> .
@prefix dash: <http://datashapes.org/dash#> .
@prefix hydra: <http://www.w3.org/ns/hydra/core#> .

@prefix ex: <http://example.com/> .

schema:Person
  a rdfs:Class, sh:NodeShape ;
  rdfs:label "Register" ;
  sh:property ex:CountryProperty , ex:StateProperty .

ex:CountryProperty
  sh:name "Country" ;
  dash:editor dash:InstancesSelectEditor ;
  sh:path schema:addressCountry ;
  hydra:collection </countries> .

ex:StateProperty
  sh:name "State" ;
  sh:path ex:state ;
  dash:editor dash:InstancesSelectEditor ;
  hydra:search [
    hydra:template "/states?country={country}" ;
    hydra:mapping [
      hydra:property schema:addressCountry ;
      hydra:variable "country" ;
      hydra:required true ;
    ] ;
  ] .
</script>
</rdf-snippet>

<p>The <code>Person</code> shape above has two properties. The first will generate a dropdown with a selection of countries as described in <a href="http://t-code.pl/blog/2020/12/hydra-shacl-interoperability">the first Hydra+SHACL post</a>. The second, while it’s also going to render a dropdown, will not be populated until a country is selected (<code>hydra:required true</code>).</p>

<p>The glue here is matching property shared between <code>sh:path</code> of the upstream field and <code>hydra:property</code> of the downstream’s search template. In other words, when the form’s graph node receives the value for the <code>schema:addressCountry</code> predicate, the “states” will be loaded.</p>

<h2 id="less-apis-more-web-standards">Less APIs, more Web Standards!</h2>

<p>Again this time, the <a href="https://forms.hypermedia.app/playground?resource=%7B%0A++%22%40context%22%3A+%7B%0A++++%22rdf%22%3A+%22http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%22%2C%0A++++%22rdfs%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%22%2C%0A++++%22xsd%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%22%2C%0A++++%22schema%22%3A+%22http%3A%2F%2Fschema.org%2F%22%2C%0A++++%22foaf%22%3A+%22http%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%22%2C%0A++++%22vcard%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2006%2Fvcard%2Fns%23%22%0A++%7D%2C%0A++%22%40id%22%3A+%22http%3A%2F%2Fexample.com%2Fjohn-doe%22%2C%0A++%22%40type%22%3A+%22schema%3APerson%22%2C%0A++%22schema%3AaddressCountry%22%3A+%7B%0A++++%22%40id%22%3A+%22http%3A%2F%2Fwww.wikidata.org%2Fentity%2FQ39%22%0A++%7D%0A%7D&amp;selectedResource=http%3A%2F%2Fexample.com%2Fjohn-doe&amp;shapes=%40prefix+sh%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fshacl%23%3E+.%0A%40prefix+schema%3A+%3Chttp%3A%2F%2Fschema.org%2F%3E+.%0A%40prefix+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E+.%0A%40prefix+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E+.%0A%40prefix+dash%3A+%3Chttp%3A%2F%2Fdatashapes.org%2Fdash%23%3E+.%0A%40prefix+hydra%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fhydra%2Fcore%23%3E+.%0A%40prefix+wdt%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E+.%0A%40prefix+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E+.%0A%0A%40prefix+ex%3A+%3Chttp%3A%2F%2Fexample.com%2F%3E+.%0A%0Aschema%3APerson%0A++a+rdfs%3AClass%2C+sh%3ANodeShape+%3B%0A++rdfs%3Alabel+%22Register%22+%3B%0A++sh%3Aproperty+ex%3ANameProperty+%2C+%0A%09%09++++++ex%3ACountryProperty+%2C+%0A%09%09%09++ex%3AStateProperty+%2C%0A++++++++++++++ex%3ASubStateProperty+%3B%0A.%0A%0Aex%3ANameProperty%0A++sh%3Apath+schema%3Aname+%3B%0A++sh%3Aname+%22Name%22+%3B%0A++sh%3Adatatype+xsd%3Astring+%3B%0A++dash%3AsingleLine+true+%3B%0A++sh%3AmaxCount+1+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3Aorder+10+%3B%0A.%0A%0Aex%3ACountryProperty%0A++sh%3Aname+%22Country%22+%3B%0A++sh%3Aclass+wd%3AQ6256+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++sh%3Apath+schema%3AaddressCountry+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++hydra%3Acollection+%3Chttps%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3Dprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcountry%2520.%250A%2520%2520%253Fcountry%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%250A%2520%2520%253Fcountry%2520wdt%253AP31%2520wd%253AQ6256%2520%253B%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%3E+%3B%0A++sh%3Aorder+20+%3B%0A.%0A%0A%0Aex%3AStateProperty%0A++sh%3Aname+%22State%22+%3B%0A++sh%3Apath+schema%3AaddressRegion+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++hydra%3Asearch+%5B%0A++++hydra%3Atemplate+%22https%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3DPREFIX%2520wd%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fentity%252F%253E%250APREFIX%2520wdt%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fprop%252Fdirect%252F%253E%250Aprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fdivision%2520.%250A%2520%2520%253Fdivision%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%2520%2520%250A%2520%2520%253C%7BCOUNTRY%7D%253E%2520wdt%253AP150%2520%253Fdivision%2520.%250A%2520%2520%253Fdivision%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%22+%3B%0A++++hydra%3Amapping+%5B%0A++++++hydra%3Aproperty+schema%3AaddressCountry+%3B%0A++++++hydra%3Avariable+%22COUNTRY%22+%3B%0A++++++hydra%3Arequired+true+%3B%0A++++%5D+%3B%0A++%5D+%3B%0A++sh%3Aorder+30+%3B%0A.%0A%0A%0Aex%3ASubStateProperty%0A++sh%3Aname+%22City%22+%3B%0A++sh%3Apath+schema%3AaddressLocality+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++hydra%3Asearch+%5B%0A++++hydra%3Atemplate+%22https%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3DPREFIX%2520wd%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fentity%252F%253E%250APREFIX%2520wdt%253A%2520%253Chttp%253A%252F%252Fwww.wikidata.org%252Fprop%252Fdirect%252F%253E%250Aprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcity%2520.%250A%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%2520%250A%2520%2520%257B%250A%2520%2520%2520%2520SELECT%2520%253Fcity%2520%253Flabel%2520WHERE%2520%257B%250A%2520%2520%2520%2520%2520%2520%253C%7BSTATE%7D%253E%2520wdt%253AP150%252B%2520%253Fcity%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520wdt%253AP31%2520%253FcityType%2520.%250A%2520%2520%2520%2520%2520%2520%253FcityType%2520wdt%253AP279%2520wd%253AQ515%2520.%250A%2520%2520%2520%2520%257D%250A%2520%2520%257D%250A%2520%2520UNION%250A%2520%2520%257B%250A%2520%2520%2520%2520SELECT%2520%253Fcity%2520%253Flabel%2520WHERE%2520%257B%2520%2520%2520%2520%250A%2520%2520%2520%2520%2520%2520%253C%7BSTATE%7D%253E%2520wdt%253AP150%252B%2520%253Fcity%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%2520%2520%2520%2520%2520%2520%253Fcity%2520wdt%253AP31%2520wd%253AQ515%2520.%250A%2520%2520%2520%2520%257D%250A%2520%2520%257D%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%22+%3B%0A++++hydra%3Amapping+%5B%0A++++++hydra%3Aproperty+schema%3AaddressRegion+%3B%0A++++++hydra%3Avariable+%22STATE%22+%3B%0A++++++hydra%3Arequired+true+%3B%0A++++%5D+%3B%0A++%5D+%3B%0A++sh%3Aorder+40+%3B%0A.">playground</a> example does not “talk” to an actual API but instead runs SPARQL queries encoded into query string parameters of Wikidata’s query endpoint. The trick is to replace a URI of the variable with a URI Template placeholder. <strong>Just gotta make sure that the braces are not percent-encoded</strong>.</p>

<p>The query to load states is simple:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
</pre></td><td class="code"><pre><code class="sparql"><span class="line"><span class="k">PREFIX</span><span class="w"> </span><span class="nn">wd</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.wikidata.org/entity/&gt;</span><span class="w"></span>
</span><span class="line"><span class="k">PREFIX</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.wikidata.org/prop/direct/&gt;</span><span class="w"></span>
</span><span class="line"><span class="k">prefix</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.w3.org/ns/hydra/core#&gt;</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="k">CONSTRUCT</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="k">a</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">Collection</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">member</span><span class="w"> </span><span class="nv">?division</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?division</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="k">BIND</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nl">&lt;urn:contry:collection&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nv">?col</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="w">  </span><span class="nl">&lt;{COUNTRY}&gt;</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="nt">P150</span><span class="w"> </span><span class="nv">?division</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?division</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="k">filter</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nf">lang</span><span class="p">(</span><span class="nv">?label</span><span class="p">)</span><span class="w"> </span><span class="k">IN</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="s">&#39;en&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;de&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;fr&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;pl&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;es&#39;</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"></span>
</span></code></pre></td></tr></table></div></figure>

<p>Loading cities is slightly more complicated, accounting for deeper graphs where a state is the root and also various types of cities recognised by Wikidata.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
</pre></td><td class="code"><pre><code class="sparql"><span class="line"><span class="k">PREFIX</span><span class="w"> </span><span class="nn">wd</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.wikidata.org/entity/&gt;</span><span class="w"></span>
</span><span class="line"><span class="k">PREFIX</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.wikidata.org/prop/direct/&gt;</span><span class="w"></span>
</span><span class="line"><span class="k">prefix</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.w3.org/ns/hydra/core#&gt;</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="k">CONSTRUCT</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="k">a</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">Collection</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">member</span><span class="w"> </span><span class="nv">?city</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?city</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="k">BIND</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nl">&lt;urn:contry:collection&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nv">?col</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="w">  </span><span class="nl">&lt;STATE&gt;</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="nt">P150</span><span class="o">*</span><span class="w"> </span><span class="nv">?city</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?city</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?city</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="nt">P31</span><span class="w"> </span><span class="nv">?cityType</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?cityType</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="nt">P279</span><span class="w"> </span><span class="nn">wd</span><span class="p">:</span><span class="nt">Q515</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="w">  </span><span class="k">filter</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nf">lang</span><span class="p">(</span><span class="nv">?label</span><span class="p">)</span><span class="w"> </span><span class="k">IN</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="s">&#39;en&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;de&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;fr&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;pl&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;es&#39;</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"></span>
</span></code></pre></td></tr></table></div></figure>

<p>Tried as I might, the cities query does not work for every country. United States, Germany and Poland are fine. On the other hand, for Colombia and Australia it finds no cities at all. Queries for Australian cities are also surprisingly slow…</p>

<p>It is not important for the example, but I would be curious to learn from a Wikidata expert how it can be improved.</p>

<script src="http://t-code.pl/components/rdf-snippet.js"></script>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hydra and SHACL - a perfect couple - part 1]]></title>
    <link href="http://t-code.pl/blog/2020/12/hydra-shacl-interoperability/"/>
    <updated>2020-12-20T00:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2020/12/hydra-shacl-interoperability</id>
    <content type="html"><![CDATA[<p><a href="http://www.hydra-cg.com/spec/latest/core/">Hydra Core</a> is a community-driven specification for describing hypermedia APIs in a machine readable form so that client applications can discover the resources at runtime. On its own, however, it is not expressible enough to describe any arbitrary resource representation.
<a href="https://www.w3.org/TR/shacl/">SHACL</a>, or Shapes Constraint Language, on the other hand is a beautifully extensible schema-like language which offers great power and flexibility in describing graph data structures.
Combined, they provide a complete solution for building hypermedia applications driven by RDF.</p>

<!--more-->

<h2 id="tldr-i-want-some-action">TL;DR; I want some action!</h2>

<p>Click the image to open Shaperone Playground, which demonstrates a working example of a form generated from a SHACL shape which dynamically loads Wikidata resources using SPARQL.</p>

<p>At the bottom of this post you will see how to configure <a href="https://github.com/hypermedia-app/shaperone">shaperone</a> this way.</p>

<p><a href="https://forms.hypermedia.app/playground?resource=%7B%0A++%22%40context%22%3A+%7B%0A++++%22rdf%22%3A+%22http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%22%2C%0A++++%22rdfs%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%22%2C%0A++++%22xsd%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%22%2C%0A++++%22schema%22%3A+%22http%3A%2F%2Fschema.org%2F%22%2C%0A++++%22foaf%22%3A+%22http%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%22%2C%0A++++%22vcard%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2006%2Fvcard%2Fns%23%22%2C%0A++++%22dcat%22%3A+%22http%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%22%0A++%7D%2C%0A++%22%40id%22%3A+%22http%3A%2F%2Fexample.com%2Fjohn-doe%22%2C%0A++%22%40type%22%3A+%22schema%3APerson%22%2C%0A++%22schema%3Aname%22%3A+%22John+Doe%22%0A%7D&amp;selectedResource=http%3A%2F%2Fexample.com%2Fjohn-doe&amp;shapes=%40prefix+sh%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fshacl%23%3E+.%0A%40prefix+schema%3A+%3Chttp%3A%2F%2Fschema.org%2F%3E+.%0A%40prefix+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E+.%0A%40prefix+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E+.%0A%40prefix+dash%3A+%3Chttp%3A%2F%2Fdatashapes.org%2Fdash%23%3E+.%0A%40prefix+hydra%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fhydra%2Fcore%23%3E+.%0A%40prefix+wdt%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E+.%0A%40prefix+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E+.%0A%0A%40prefix+ex%3A+%3Chttp%3A%2F%2Fexample.com%2F%3E+.%0A%0Aschema%3APerson%0A++a+rdfs%3AClass%2C+sh%3ANodeShape+%3B%0A++rdfs%3Alabel+%22Register%22+%3B%0A++sh%3Aproperty+ex%3ANameProperty+%2C+ex%3ACountryProperty+%3B%0A.%0A%0Aex%3ANameProperty%0A++sh%3Apath+schema%3Aname+%3B%0A++sh%3Aname+%22Name%22+%3B%0A++sh%3Adatatype+xsd%3Astring+%3B%0A++dash%3AsingleLine+true+%3B%0A++sh%3AmaxCount+1+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3Aorder+10+%3B%0A.%0A%0Aex%3ACountryProperty%0A++a+sh%3APropertyShape+%3B%0A++sh%3Aname+%22Country%22+%3B%0A++sh%3Aclass+wd%3AQ6256+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++sh%3Apath+wdt%3AP27+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++hydra%3Acollection+%3Chttps%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3Dprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcountry%2520.%250A%2520%2520%253Fcountry%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%250A%2520%2520%253Fcountry%2520wdt%253AP31%2520wd%253AQ6256%2520%253B%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%3E+%3B%0A++sh%3Aorder+20+%3B%0A.%0A&amp;disableEditorChoice=true&amp;components=vaadin"><img src="http://t-code.pl/images/shaperone/hydra-collection.png" alt="shaperone playground" /></a></p>

<h2 id="hydra-http-request-descriptions">Hydra HTTP request descriptions</h2>

<p>The Hydra vocabulary defines a term <code>hydra:Operation</code> which represents a HTTP request which a server advertises as being supported by specific resources, either by a specific instance or entire class of resources.</p>

<p>For the sake of this blog post, let’s consider a hypothetical API which describes a registration request:</p>

<rdf-snippet formats="application/ld+json,application/n-quads" prefixes="schema,hydra">
   <script type="text/turtle">
base <http://example.app/api#>
prefix schema: <http://schema.org/>
prefix hydra:  <http://www.w3.org/ns/hydra/core#>
prefix rdfs:   <http://www.w3.org/2000/01/rdf-schema#>

<UsersCollection>
  a hydra:Class ;
  rdfs:subClassOf hydra:Collection ;
  hydra:supportedOperation <RegisterUser> .

<RegisterUser>
  a hydra:Operation , schema:CreateAction ;
  hydra:method "POST" ;
  hydra:expects <User> ;
.

<User> a hydra:Class .
   </script>
</rdf-snippet>

<p>The above snippet, excerpt from the API’s <a href="http://www.hydra-cg.com/spec/latest/core/#documenting-a-web-api">Documentation resource</a>, declares that the clients will come across a collection of users (<code>rdf:type &lt;UserCollection&gt;</code>) against which a POST request will be possible to create a new resource. That operation will require a representation of the <code>&lt;User&gt;</code> class.</p>

<p>While Hydra Core vocabulary does have a basic set of terms which can describe the user class, it may not be enough to cater for rich client-server interactions as well as a UI building block. Neither will be RDFS, and OWL, although quite powerful, is a little complex and seriously lacks tooling support and widespread recognition.</p>

<p>Enter, SHACL.</p>

<h2 id="using-shacl-to-describe-api-payloads">Using SHACL to describe API payloads</h2>

<p>SHACL is another RDF vocabulary, which describes data graphs by constraining properties and values of precisely targeted nodes in an RDF graph. It could be used to complement the API Documentation graph above by providing the required shape of instances of the <code>&lt;User&gt;</code> class. This is easiest done by turning it into an <a href="https://www.w3.org/TR/shacl/#implicit-targetClass">implicitly targeted</a> <a href="https://www.w3.org/TR/shacl/#node-shapes"><code>sh:NodeShape</code></a>.</p>

<p>In this example let’s require users to provide exactly one name (using <code>schema:name</code>) and exactly one country of citizenship (using said Wikidata property <a href="https://www.wikidata.org/wiki/Property:P27">P27</a>)</p>

<rdf-snippet formats="application/ld+json,application/n-quads" prefixes="schema,hydra,sh,dash">
   <script type="text/turtle">
prefix hydra:  <http://www.w3.org/ns/hydra/core#>
prefix rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
prefix sh:     <http://www.w3.org/ns/shacl#>
prefix schema: <http://schema.org/>
prefix dash:   <http://datashapes.org/dash#>
prefix wdt:    <http://www.wikidata.org/prop/direct/>
prefix wd:     <http://www.wikidata.org/entity/>
prefix xsd:    <http://www.w3.org/2001/XMLSchema#>

<User>
  a hydra:Class , rdfs:Class , sh:NodeShape ;
  sh:property [
    a sh:PropertyShape ;
    sh:path schema:name ;
    sh:name "Name" ;
    sh:minCount 1 ;
    sh:maxCount 1 ;
    sh:datatype xsd:string ;
    sh:minLength 3 ;
    sh:order 10 ;
    dash:singleLine true ;
  ] , [
    a sh:PropertyShape ;
    sh:name "Country" ;
    sh:class wd:Q6256 ;
    sh:path wdt:P27 ;
    sh:minCount 1 ;
    sh:maxCount 1 ;
    sh:order 20 ;
    dash:editor dash:InstancesSelectEditor ;
  ] ;
.
   </script>
</rdf-snippet>

<p>Hopefully this is quite self-explanatory so far.</p>

<ol>
  <li>The objects of <code>sh:property</code> require that any instance of <code>&lt;User&gt;</code> have exactly one of each property, declared using <code>sh:path</code>. That is achieved using <code>sh:minCount</code> and <code>sh:maxCount</code></li>
  <li>Name must be at least 3 characters long string</li>
  <li>Country must be an instance of Wikidata Country class <code>wd:Q6256</code></li>
  <li>Exactly one country is allowed</li>
  <li><code>sh:order</code> is a UI hint for organising inputs in a form</li>
  <li><code>dash:singleLine</code> is a form builder hint which ensures that the text field does not allow line breaks (ie. no <code>&lt;textarea&gt;</code>)</li>
  <li><code>dash:editor</code> instructs the form builder to create an input component with a selection of instances of the desired RDF type</li>
</ol>

<p>SHACL is quite wonderful in that shapes are useful for many purposes. Check the <a href="https://www.w3.org/TR/shacl-ucr/">SHACL Use Cases and Requirements</a> note for a host of examples. In the presented scenario, a rich client can use to dynamically produce a form to have users input the data, and the server will run validations to check that requests payloads satisfy the SHACL constraints.</p>

<p>There is one piece missing however: <strong>where do the Country instances come from?</strong> 🤨</p>

<h2 id="circling-back-to-hydra">Circling back to Hydra</h2>

<p>Out of the box, a SHACL processor would assume that any instances would be part the <a href="https://www.w3.org/TR/shacl/#data-graph">Data Graph</a>. While this works for validation inside of TopBraid it is not feasible to build a browser application that way. For example, at the time of writing there are 171 instances of Country in Wikidata. Combined with a multitude of labels in various languages that is total of over 40 thousand triples. It’s hardly a good idea to push that proactively to the client up front.</p>

<p>Instead, I propose to connect the Shape back with the API using Hydra Core term <code>hydra:collection</code>. It is defined modestly:</p>

<blockquote>
  <p>Collections somehow related to this resource.</p>
</blockquote>

<p>It also does not have and <code>rdfs:range</code> or <code>rdfs:domain</code> making it a good candidate for linking a property shape directly with its data source:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="diff"><span class="line">prefix hydra: &lt;http://www.w3.org/ns/hydra/core#&gt;
</span><span class="line">prefix sh: &lt;http://www.w3.org/ns/shacl#&gt;
</span><span class="line">prefix wdt: &lt;http://www.wikidata.org/prop/direct/&gt;
</span><span class="line">prefix wd: &lt;http://www.wikidata.org/entity/&gt;
</span><span class="line">
</span><span class="line">&lt;User&gt;
</span><span class="line">  sh:property [
</span><span class="line">    a sh:PropertyShape ;
</span><span class="line">    sh:class wd:Q6256 ;
</span><span class="line">    sh:path wdt:P27 ;
</span><span class="line"><span class="gi">+   hydra:collection &lt;https://example.app/countries&gt; ;</span>
</span><span class="line">  ] ;
</span><span class="line">.
</span></code></pre></td></tr></table></div></figure>

<p>By adding this property a UI component can load the countries by dereferencing a <code>hydra:Collection</code> whose representation would look somewhat like this:</p>

<rdf-snippet formats="application/ld+json,application/n-quads" prefixes="hydra">
   <script type="text/turtle">
prefix hydra: <http://www.w3.org/ns/hydra/core#>
prefix wd:    <http://www.wikidata.org/entity/>
prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#>

<https://example.app/countries>
  a hydra:Collection ;
  hydra:member wd:Q36 , wd:Q183 , wd:Q145 ;
  hydra:totalItems 171 ;
.

wd:Q36 rdfs:label "Poland"@en , "Polska"@pl , "Polen"@de .
wd:Q183 rdfs:label "Germany"@en , "Niemcy"@pl , "Deutschland"@de .
wd:Q145 rdfs:label "United Kingdom"@en , "Zjednoczone Królestwo"@pl , "Vereinigtes Königreich"@de .
   </script>
</rdf-snippet>

<h2 id="apis-are-dead-long-live-linked-data-apis">APIs are dead; Long live (Linked Data) APIs!</h2>

<p><img src="https://hydra.t-code.pl/img/linked_data.jpg" alt="linked data mug" /></p>

<p>So far the subject was APIs, but the web is more than just servers returning data, even if that data is RDF. You see, the hypothetical registration form above actually references a third party dataset, which is Wikidata. All of this data is already on the web and use standard formats. By using a simple SPARQL query the countries can be <a href="https://w.wiki/rsz">fetched directly from their source</a>; without even adding the <code>/countries</code> resource to your API. Heck, the client appication would not need a dedicated API at all!</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
</pre></td><td class="code"><pre><code class="sparql"><span class="line"><span class="c"># wd: and wdt: are implicitly added by wikidata&#39;s SPARQL endpoint</span><span class="w"></span>
</span><span class="line"><span class="k">prefix</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="w"> </span><span class="nl">&lt;http://www.w3.org/ns/hydra/core#&gt;</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="k">CONSTRUCT</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="k">a</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">Collection</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?col</span><span class="w"> </span><span class="nn">hydra</span><span class="p">:</span><span class="nt">member</span><span class="w"> </span><span class="nv">?country</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?country</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"> </span><span class="p">.</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="k">BIND</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nl">&lt;https://example.app/countries&gt;</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nv">?col</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="w">  </span><span class="c"># wdt:P31 - &quot;instance of&quot;</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="c"># wd:Q6256 - &quot;country&quot;</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="nv">?country</span><span class="w"> </span><span class="nn">wdt</span><span class="p">:</span><span class="nt">P31</span><span class="w"> </span><span class="nn">wd</span><span class="p">:</span><span class="nt">Q6256</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="nn">rdfs</span><span class="p">:</span><span class="nt">label</span><span class="w"> </span><span class="nv">?label</span><span class="w"></span>
</span><span class="line">
</span><span class="line"><span class="w">  </span><span class="c"># only request labels in a handful of languages</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="c"># to dramatically reduce response size</span><span class="w"></span>
</span><span class="line"><span class="w">  </span><span class="k">FILTER</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="nf">lang</span><span class="p">(</span><span class="nv">?label</span><span class="p">)</span><span class="w"> </span><span class="k">IN</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="s">&#39;en&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;de&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;fr&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;pl&#39;</span><span class="p">,</span><span class="w"> </span><span class="s">&#39;es&#39;</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</span><span class="line"><span class="p">}</span><span class="w"></span>
</span></code></pre></td></tr></table></div></figure>

<p>This query can be directly encoded in a URL to GET the countries and populate a dropdown component. You can see that in the <a href="https://forms.hypermedia.app/playground?resource=%7B%0A++%22%40context%22%3A+%7B%0A++++%22rdf%22%3A+%22http%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-rdf-syntax-ns%23%22%2C%0A++++%22rdfs%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%22%2C%0A++++%22xsd%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%22%2C%0A++++%22schema%22%3A+%22http%3A%2F%2Fschema.org%2F%22%2C%0A++++%22foaf%22%3A+%22http%3A%2F%2Fxmlns.com%2Ffoaf%2F0.1%2F%22%2C%0A++++%22vcard%22%3A+%22http%3A%2F%2Fwww.w3.org%2F2006%2Fvcard%2Fns%23%22%2C%0A++++%22dcat%22%3A+%22http%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%22%0A++%7D%2C%0A++%22%40id%22%3A+%22http%3A%2F%2Fexample.com%2Fjohn-doe%22%2C%0A++%22%40type%22%3A+%22schema%3APerson%22%2C%0A++%22schema%3Aname%22%3A+%22John+Doe%22%0A%7D&amp;selectedResource=http%3A%2F%2Fexample.com%2Fjohn-doe&amp;shapes=%40prefix+sh%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fshacl%23%3E+.%0A%40prefix+schema%3A+%3Chttp%3A%2F%2Fschema.org%2F%3E+.%0A%40prefix+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E+.%0A%40prefix+xsd%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%23%3E+.%0A%40prefix+dash%3A+%3Chttp%3A%2F%2Fdatashapes.org%2Fdash%23%3E+.%0A%40prefix+hydra%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fhydra%2Fcore%23%3E+.%0A%40prefix+wdt%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E+.%0A%40prefix+wd%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E+.%0A%0A%40prefix+ex%3A+%3Chttp%3A%2F%2Fexample.com%2F%3E+.%0A%0Aschema%3APerson%0A++a+rdfs%3AClass%2C+sh%3ANodeShape+%3B%0A++rdfs%3Alabel+%22Register%22+%3B%0A++sh%3Aproperty+ex%3ANameProperty+%2C+ex%3ACountryProperty+%3B%0A.%0A%0Aex%3ANameProperty%0A++sh%3Apath+schema%3Aname+%3B%0A++sh%3Aname+%22Name%22+%3B%0A++sh%3Adatatype+xsd%3Astring+%3B%0A++dash%3AsingleLine+true+%3B%0A++sh%3AmaxCount+1+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3Aorder+10+%3B%0A.%0A%0Aex%3ACountryProperty%0A++a+sh%3APropertyShape+%3B%0A++sh%3Aname+%22Country%22+%3B%0A++sh%3Aclass+wd%3AQ6256+%3B%0A++dash%3Aeditor+dash%3AInstancesSelectEditor+%3B%0A++sh%3Apath+wdt%3AP27+%3B%0A++sh%3AminCount+1+%3B%0A++sh%3AmaxCount+1+%3B%0A++hydra%3Acollection+%3Chttps%3A%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3Dprefix%2520hydra%253A%2520%253Chttp%253A%252F%252Fwww.w3.org%252Fns%252Fhydra%252Fcore%2523%253E%250A%250ACONSTRUCT%2520%257B%250A%2520%2520%253Fcol%2520a%2520hydra%253ACollection%2520.%250A%2520%2520%253Fcol%2520hydra%253Amember%2520%253Fcountry%2520.%250A%2520%2520%253Fcountry%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%257D%2520WHERE%2520%257B%250A%2520%2520BIND%2520%2528%2520%253Curn%253Acontry%253Acollection%253E%2520as%2520%253Fcol%2520%2529%250A%250A%2520%2520%253Fcountry%2520wdt%253AP31%2520wd%253AQ6256%2520%253B%2520rdfs%253Alabel%2520%253Flabel%2520.%250A%250A%2520%2520filter%2520%2528%2520lang%2528%253Flabel%2529%2520IN%2520%2528%2520%2527en%2527%252C%2520%2527de%2527%252C%2520%2527fr%2527%252C%2520%2527pl%2527%252C%2520%2527es%2527%2520%2529%2520%2529%250A%257D%3E+%3B%0A++sh%3Aorder+20+%3B%0A.%0A&amp;disableEditorChoice=true&amp;components=vaadin">playground</a>, mentioned in the beginning.</p>

<p>All possible thanks to web standards 🤘</p>

<h2 id="implementation-notes">Implementation notes</h2>

<p>Shaperone makes building a Hydra-aware form like this easy:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="ts"><span class="line"><span class="kr">import</span> <span class="o">*</span> <span class="kr">as</span> <span class="nx">components</span> <span class="nx">from</span> <span class="s1">&#39;@hydrofoil/shaperone-wc/NativeComponents&#39;</span>
</span><span class="line"><span class="c1">// OR import * as components from &#39;@hydrofoil/shaperone-wc-material/components&#39;</span>
</span><span class="line"><span class="c1">// OR import * as components from &#39;@hydrofoil/shaperone-wc-vaadin/components&#39;</span>
</span><span class="line"><span class="c1">// OR roll your own rendering components</span>
</span><span class="line"><span class="kr">import</span> <span class="o">*</span> <span class="kr">as</span> <span class="nx">configure</span> <span class="nx">from</span> <span class="s1">&#39;@hydrofoil/shaperone-wc/configure&#39;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">instancesSelector</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;@hydrofoil/shaperone-hydra/components&#39;</span>
</span><span class="line">
</span><span class="line"><span class="c1">// register UI component which will do the rendering</span>
</span><span class="line"><span class="nx">configure</span><span class="p">.</span><span class="nx">components</span><span class="p">.</span><span class="nx">pushComponents</span><span class="p">(</span><span class="nx">components</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="c1">// add Hydra extension to dash:InstancesSelectEditor</span>
</span><span class="line"><span class="nx">configure</span><span class="p">.</span><span class="nx">editors</span><span class="p">.</span><span class="nx">decorate</span><span class="p">(</span><span class="nx">instancesSelector</span><span class="p">.</span><span class="nx">matcher</span><span class="p">)</span>
</span><span class="line"><span class="nx">configure</span><span class="p">.</span><span class="nx">components</span><span class="p">.</span><span class="nx">decorate</span><span class="p">(</span><span class="nx">instancesSelector</span><span class="p">.</span><span class="nx">decorator</span><span class="p">())</span>
</span></code></pre></td></tr></table></div></figure>

<p>The <code>@hydrofoil/shaperone-hydra</code> package extends the default behaviour to have <code>hydra:collection</code> dereferenced rather than looking for the instance data locally.</p>

<h2 id="next-steps">Next steps</h2>

<p>In future posts I will present how to:</p>

<ol>
  <li>use Hydra descriptions to find collections without <code>hydra:collection</code> directly</li>
  <li><code>hydra:search</code> URI Templates can be used to:
    <ul>
      <li>create forms with dependent fields, so that users first select a country which is then used to narrow down a selection of country’s secondary administrative division and so on <strong><a href="http://t-code.pl/blog/2020/12/hydra-shacl-templates">POST</a></strong></li>
      <li>improve performance by filtering resources on the data source</li>
    </ul>
  </li>
</ol>

<script src="http://t-code.pl/components/rdf-snippet.js"></script>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why RDF is struggling - the case of R2RML]]></title>
    <link href="http://t-code.pl/blog/2020/07/rdf-struggling-case-of-r2rml/"/>
    <updated>2020-07-24T00:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2020/07/rdf-struggling-case-of-r2rml</id>
    <content type="html"><![CDATA[<p>In 2012 I started my .NET implementation of <a href="https://www.w3.org/TR/r2rml/">R2RML</a> and <a href="https://www.w3.org/TR/rdb-direct-mapping/">RDB to RDF Direct mapping</a> which I called <a href="https://github.com/r2rml4net/r2rml4net">r2rml4net</a>. It never reached the maturity it should have but now, 8 years later, I have little choice but to polish it and use it for converting my database to triples. A task I had originally intended but never really completed.</p>

<p>Why is it significant? Because all those years later the environment around R2RML as a standard is almost as broken, incomplete and sad as it was when I started. Let’s explore that as an example of what is wrong with RDF in general.</p>

<!--more-->

<h2 id="update-july-31st-2020">Update July 31st, 2020</h2>

<p>It has been brought to my attention that Morph is in fact actiavely maintained. I’ve updated it’s details and evaluation.</p>

<h2 id="intro-what-is-r2rml">Intro. What is R2RML?</h2>

<p><strong>R2RML</strong> and <strong>Direct Mapping</strong> are two complementary W3C recommendation (specifications) which define language and algorithm respectively which are used to transform relation databases into RDF graphs. The first is a full blown, but not overly complicated RDF vocabulary which lets designers hand-craft the way in which relational tables are converted into RDF. Individual columns are either directly converted into values (taking their respective database types into consideration) or used within simple templates to produce compound values as literals, blank node and literal alike.</p>

<p>Direct Mapping is a simpler approach, often using R2RML internally as the mapping model, which creates an automatic mapping from any given relational database into triples. The specification defines way in which tables, rows and values are meant to map into triples. It can be either executed standalone and then the resulting RDF would be refined, or an R2RML document can be produced so that it can be fine-tune before the actual transformation happens.</p>

<p>Complementary to these two specs there are a two sets of test cases which can be exercised by implementors claiming compatibility and advertised at a central <a href="https://www.w3.org/TR/rdb2rdf-implementations/">RDB2RDF implementation report</a> page hosted by W3C.</p>

<p>Related to R2RML, there is also a newer specification <a href="https://rml.io">RML.io</a> which extends it into supporting also other sources like XML and CSV.</p>

<h2 id="why-is-it-important">Why is it important?</h2>

<p>I had an interesting twitter exchange recently where I tried to present arguments why applying RDF selectively, without really using it in every layer of the application architecture is problematic.</p>

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">You need to look at the big picture, entire stack of a single or multiple applications<br /><br />Polyglot persistence becomes a burden if you convert JSONs and relational data into RDF all the time<br /><br />If RDF is not your programming model then you&#39;re in for pain<br /><br />And no, JSON-LD is snake oil</p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/1283701660552962048?ref_src=twsrc%5Etfw">July 16, 2020</a></blockquote>

<p>In that case JSON-LD got the bashing but the bottom like here is that when building an application using RDF technologies it is worth using it in all software components. From the user interface all the way to the database. This is the only way which prevents constant tension between graph and non-graph models, such as the mentioned issue where JSON-LD hides the graphy nature of data. It is a similar problem which haunted software where relation data model is mapped into object complex models. For that I recommend the classic blog post by Jeff Atwood titled <a href="https://blog.codinghorror.com/object-relational-mapping-is-the-vietnam-of-computer-science/">Object-Relational Mapping is the Vietnam of Computer Science</a></p>

<p>R2RML should be an important tool in the toolkit of any <em>Semantic Web development team</em> as it aims to provide an effective way for migrating existing datasets stored in SQL silos into RDF. This can be done by performing a one-time conversion as mentioned above but an alternative approach some take is running the mapping on-demand, for example by translating SPARQL queries into SQL without ever persisting the converted triples.</p>

<hr />

<p>You could think that surely, over the years we should have grown a vibrant ecosystem around this cornerstone piece of technology. Well, think again…</p>

<h2 id="my-humble-requirements">My humble requirements</h2>

<p>For my use case I have simple requirements. I need to perform a fairly simple mapping of a handful of tables into quads. That is, I want to partition the dataset into named graphs, mostly in a graph-per-entity fashion. Pretty standard as R2RML goes.</p>

<p>My database is Azure SQL so MS SQL has to be supported.</p>

<p>I expect also ease of use. Preferably a standalone CLI, easily installed and usable on CI.</p>

<h2 id="r2rml-implementations-in-the-wild">R2RML implementations in the wild</h2>

<p>The first logical place to look for R2RML software should be the <a href="https://www.w3.org/TR/rdb2rdf-implementations/">Implementation Report</a>. It lists 8 implementations, 4 out of which implement both R2RML and Direct mapping:</p>

<ol>
  <li><a href="https://metacpan.org/release/RDF-RDB2RDF">RDF-RDB2RDF</a> (both)</li>
  <li><a href="https://github.com/semantalytics/xsparql">XSPARQL</a> (both)</li>
  <li><a href="http://www.capsenta.com/">ultrawrap</a> (both)</li>
  <li><a href="https://github.com/antidot/db2triples">db2triples</a> (both)</li>
  <li><a href="http://d2rq.org/">D2RQ</a> (Direct Mapping)</li>
  <li><a href="http://swobjects.svn.sourceforge.net/">SWObjects dm-materialize</a> (Direct Mapping)</li>
  <li><a href="http://virtuoso.openlinksw.com/">OpenLink Virtuoso</a> (R2RML)</li>
  <li><a href="https://github.com/oeg-upm/morph-rdb">morph</a> (R2RML)</li>
</ol>

<p>The listing is clearly not actively maintained (last updated in August 2012) so one would also try searching so the latest and greatest. Here’s what I found:</p>

<ol>
  <li><a href="https://ontop-vkg.org">Ontop</a></li>
  <li><a href="https://github.com/usc-isi-i2/Web-Karma">Karma</a></li>
  <li><a href="https://github.com/d2rq/r2rml-kit">d2rq/r2rml-kit</a></li>
  <li><a href="https://github.com/chrdebru/r2rml">chrdebru/r2rml</a></li>
  <li><a href="https://github.com/carml/carml">CARML</a> (RML)</li>
  <li><a href="https://github.com/RMLio/rmlmapper-java">RML.io RMLMapper</a> (RML)</li>
  <li><a href="https://github.com/SDM-TIB/SDM-RDFizer">SDM-RDFizer</a> (RML)</li>
  <li><a href="https://github.com/semantifyit/RocketRML">RocketRML</a> (RML)</li>
</ol>

<p>Let’s take a closer look to check if they present a viable option. I’m only interested in R2RML so that eliminates D2RQ and SWObjects dm-materialize but let’s check them out either way.</p>

<p>Of the RML implementations, CARML and RocketRML do not support SQL data source and SDM-RDFizer does not support SQL Server. That leaves RMLMapper.</p>

<p>Finally, there are a bunch of commercial products which incorporate R2RML and other kinds of mappings and migrations from other data sources to semantic graphs. Names like Stardog or Anzo which are aimed at big corporate settings. They often don’t have free versions, require adopting their entire, integrated environment and cost big buck.</p>

<h3 id="rdf-rdb2rdf">RDF-RDB2RDF</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>0.008</td>
      <td>😕</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2013-09-20</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>Perl package manager</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>individual</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>The project page is rather developer-centric. An <strong>INSTALL</strong> file linked in an <strong>Other files</strong> section says</p>

<blockquote>
  <p>Installing RDF-RDB2RDF should be straightforward.
If you have cpanm, you only need one line:</p>
</blockquote>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class=""><span class="line">% cpanm RDF::RDB2RDF</span></code></pre></td></tr></table></div></figure>

<p>Looks simple, but I have no idea about PERL and <code>cpanm</code>. There is also a <code>README</code> file but the usage instructions are rather uninformative. I think this is only a library. Even if this gets the job done, there is no way I’m learning PERL for this 🙄</p>

<h3 id="xsparql">XSPARQL</h3>

<p>While the address linked from the implementation report is now dead, a quick google reveals its new home on GitHub.</p>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>1.1.0</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2019-02-04</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td><code>.jar</code> download</td>
      <td>🙄</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Company (?)</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>The R2RML feature is not well advertised but found in the wiki under <a href="https://github.com/semantalytics/xsparql/wiki/Working-with-RDBMS-SQL">Working with RDBMS SQL</a></p>

<p>Configuration is provided using a <code>.properties</code> file. Awkward but doable. Unfortunately the project does not show an example of how to set it up.</p>

<h3 id="ultrawrap">ultrawrap</h3>

<table>
  <tbody>
    <tr>
      <td>Developed by</td>
      <td>Company</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>The linked company Capsenta redirects to <a href="https://data.world">https://data.world</a> and appears to be a commercial product. There is also a <strong>Community tier</strong> of what seems to be a SaaS offering.</p>

<p>Not sure about this one.</p>

<h3 id="db2triples">db2triples</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>2.2</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2019-08-02</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>Build with maven</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Company</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>This one looks promising. Sadly, it appears that the sources have to be built manually. No thank you. On the other hand the <code>format</code> parameter can be one of <code>'RDFXML', 'N3', 'NTRIPLES' or 'TURTLE'</code> so I guess no named graphs? 😢</p>

<h3 id="d2rq">D2RQ</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>0.8.1</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2012-06-22</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>Download from d2rq.org</td>
      <td>🙄</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Universities</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Anyway, only Direct Mapping and unmaintained but if it works, it works…</p>

<h3 id="swobjects-dm-materialize">SWObjects dm-materialize</h3>

<p>❌ It’s dead Jim</p>

<h3 id="openlink-virtuoso">OpenLink Virtuoso</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>7.2</td>
      <td>✨</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2018-08-15</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>Dedicated installers + a plugin</td>
      <td>😕</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Company</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Virtuoso is a well-known name in the RDF space. It is a commercial product and a triple store. Support for R2RML comes as an add-on and the overall setup looks super complicated and not at all standalone 👎. Sorry</p>

<h3 id="morph">morph</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>3.12.5</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2019-09-20</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>JAR download</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Company</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Much outdated in the original 2012 implementation report, it turns out that Morph has seen much activity since and has been developed by a commercial company. Java-style setup using a JAR download and the awkward <code>.properties</code> file but definitely something to try out.</p>

<h3 id="ontop">Ontop</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>4.0-rc1</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2020-06-08</td>
      <td>🎉</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>JAR download</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>University</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Ontop is mainly a Virtual Graph endpoint, like d2rq, but comes with a CLI command <code>materialize</code> which takes a R2RML mapping graph and serializes the resulting triples to a file.</p>

<p>Unfortunately, at the time of writing named graphs <a href="https://github.com/ontop/ontop/issues/343">are not supported</a>. The project is very actively maintained and that might change very soon.</p>

<h3 id="karma">Karma</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>2.4</td>
      <td>👍</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2020-06-03</td>
      <td>✨</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>GUI? <code>mvn exec:java</code>?</td>
      <td>😕</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>University</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Another super active but also quite complex tool. An installation page shows how to install a GUI tool. The README gives examples of commands running Maven within a clone of the original repository. Maybe I’m missing something but it does look like it falls into “easy of use” category.</p>

<p>To do it justice, this definitely looks super useful as a</p>

<blockquote>
  <p>an information integration tool that enables users to quickly and easily integrate data from a variety of data sources</p>
</blockquote>

<p>as advertised in the repo. Not what I’m looking for though.</p>

<h3 id="d2rqr2rml-kit">d2rq/r2rml-kit</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>N/A</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Last update</td>
      <td>2019-06-19</td>
      <td>✨</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>scripts in repository</td>
      <td>😕</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Individual (?)</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<blockquote>
  <p>r2rml-kit is an offshoot of D2RQ, based on its abandoned develop branch
r2rml-kit is currently in pre-alpha stage.</p>
</blockquote>

<p>Not only is it <strong>pre-alpha</strong>, it is also not really maintained. Too bad…</p>

<h3 id="chrdebrur2rml">chrdebru/r2rml</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>N/A</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Last update</td>
      <td>2020-04-13</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>build sources</td>
      <td>👎</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>Individual</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>Another Java project which fails to even provide a pre-built JAR. This one has at least seen some development recent time and claims to support quad output formats. Maybe worth a go.</p>

<h3 id="rmlmapper">RMLMapper</h3>

<table>
  <tbody>
    <tr>
      <td>Version</td>
      <td>4.8.1</td>
      <td>✨</td>
    </tr>
    <tr>
      <td>Last release</td>
      <td>2020-07-03</td>
      <td>🎉</td>
    </tr>
    <tr>
      <td>Installation</td>
      <td>docker run</td>
      <td>👌</td>
    </tr>
    <tr>
      <td>Developed by</td>
      <td>University</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<p>The last RML implementation looks promising too. Actively maintained, supports SQL server, outputs quads, uses modern tooling. A definite candidate for success.</p>

<h2 id="summary">Summary</h2>

<p>For such a crucial piece of software it’s quite disappointing to see in what state the environment is and how little it has changed since 2012 when I first had a look at R2RML.</p>

<p>The old implementations died off or became commercial products. C’est la vie.</p>

<p>The surviving ones on the other hand mostly fail to provide a usable package. Why should I be interested in running Maven or even manually downloading a JAR to run. Where is the simplicity of package managers effortless installation one can find in JavaScript (<code>npm i -g hypothetical-r2rml</code>) or the latest .NET (<code>dotnet tool install -g hypothetical-r2rml</code>). Once installed it should simply create a global executable to run the transformation.</p>

<p>And why are so many poorly documented? Again, I can mostly speak of JS and .NET ecosystems and there are plenty of examples of beautiful, detailed documentation pages and guides. How is it possible that most of those above fail on that front.</p>

<p>Maybe I’m being unfair about that last point. Much software is poorly documented and I have been guilty of that myself in the past but for the RDF community at large it should be critical to provide working, well documented software in order for semantic technologies to achieve any wider recognition.</p>

<p>Finally, I would have said in the past that universities are part of the problem and the Semantic Web has been long viewed as academic and impractical. It pleases me to see that but of the above, the more recent uni-managed packages actually stand out as being more modern and better maintained overall. 👍</p>

<p>And I have not even looked at test coverage but I do not dare.</p>

<h2 id="coming-next">Coming next</h2>

<p>In the end, it’s still a little disappointing how limited the choice seems for someone looking for an unimposing but functional R2RML solution. In the two lists above I gathered 16 potential candidates out of which only a handful remain:</p>

<ol>
  <li><strong>XSPARQL</strong> (config is going to be a trial &amp; error thing)</li>
  <li><strong>db2triples</strong> (only if the docs are inaccurate and named graphs are supported)</li>
  <li><strong>morph</strong></li>
  <li><strong>Ontop</strong> (no named graph but deserves a closer look)</li>
  <li><strong>chrdebru/r2rml</strong></li>
  <li><strong>RMLMapper</strong></li>
</ol>

<p>I initially intended to give more details about each of the promising implementation in this post but I decided that I should look in more detail and actually try running and comparing those most promising implementations to see if they can actually deliver. In a subsequent post I will take my mappings and try processing them with the 5 tools I selected.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Testing APIs Hypermedia-style]]></title>
    <link href="http://t-code.pl/blog/2019/06/testing-hypermedia-api/"/>
    <updated>2019-06-27T12:15:00+00:00</updated>
    <id>http://t-code.pl/blog/2019/06/testing-hypermedia-api</id>
    <content type="html"><![CDATA[<p>One would expect the task of (REST) API testing to be a well-researched subject. After all, REST has been formulated over a
decade ago and the number of APIs being built keeps growing exponentially. Yet, it seems that the art of testing APIs hasn’t
changed much from the approaches used in testing RPC-style APIs or non-API code.</p>

<p>In this post I’d like to propose a different approach to defining and executing tests of a truly RESTful, hypermedia-driven API.</p>

<!--more-->

<h2 id="recap-what-is-a-hypermedia-driven-api">Recap. What is a Hypermedia-driven API?</h2>

<p>The Hypermedia REST constraint, originally called <code>Hypermedia As The Engine of Application State</code> by Roy Fielding, is probably 
easiest to grasp through the maxim <strong>follow your nose</strong>. It means that a client should base the subsequent state changes
(server requests) solely on information gathered from previously received resource representations. The information
available depends on the media type being used. Different media types may provide a different degree of hypermedia support.
The facets have been gathered by Mike Amundsen in his <a href="http://amundsen.com/hypermedia/hfactor/">H Factor</a> measurement model.</p>

<p>For example, the simple but popular media type <a href="http://stateless.co/hal_specification.html">HAL</a> supports links, which lets clients follow them without a priori
knowledge about specific URLs. All they need to know is a <a href="https://www.iana.org/assignments/link-relations">link relation</a> name, and look for that link in the resource
representation. What’s more, the links can appear and disappear in said representations based on resource’s state or the
user’s permissions. An adaptive client should only follow links which are present at the given moment.</p>

<p>More sophisticated media types would also provide forms, such as <code>&lt;form&gt;</code> in HTML, which allow clients to perform requests
with methods different than <code>GET</code> to change the state of resources.</p>

<h2 id="problem-with-existing-testing-tools">Problem with existing testing tools</h2>

<p>There are multiple popular tools used for testing APIs. Some of the names include <a href="https://www.getpostman.com">Postman</a>, <a href="http://rest-assured.io">REST Assured</a>,
<a href="https://intuit.github.io/karate/">Karate</a> or <a href="https://www.soapui.org">SoapUI</a>. Each one of these tool has their respective strengths and characteristics, but they
all share a similar flaw: they revolve around URI of individual resources and test them in isolation. While it may sound
good from a unit testing perspective, it’s pretty obvious that API tests will always be integration tests. Focusing on a resource
identifier prevents the tests from taking advantage of rich hypermedia controls. Those cannot be easily tested, even if 
the API under test uses a hypermedia media type. Such tests will mostly only reach level 3 of <a href="https://martinfowler.com/articles/richardsonMaturityModel.html">Richardson Maturity Model</a>.</p>

<p><img src="https://martinfowler.com/articles/images/richardsonMaturityModel/overview.png" alt="Richardson Maturity Model" /></p>

<h2 id="test-by-following-your-nose">Test by following your nose</h2>

<p>To overcome this problem I propose a different approach to building an API test suite. Most importantly, the test executor
must act just like a hypermedia-aware client. It should only ever follow links and submit forms found in received resource
representations. It also should never begin testing from any random URL because a REST API should only ever advertise just
a single stable home URL.</p>

<p>Thus, a test scenario must begin with requesting the aforementioned initial resource and making its way through other
representations via links and forms. I call this <strong>What you <code>GET</code> is what you test</strong> which would be abbreviated <code>WYGIWYT</code>,
taking after the ancient web development acronym.</p>

<h3 id="wygiwyt-dsl"><code>WYGIWYT</code> DSL</h3>

<p>To make this approach I propose a completely new DSL, or domain-specific language, which can capture the nature of
transitioning between resource representations.</p>

<p>The most basic building block would be to define expected hypermedia controls at the root of a test definition. Such top-level
(or ambient) declaration would be eagerly executed whenever it is encountered in any resource.</p>

<p>For example, the below snippet could instruct the runner to follow every <a href="http://www.w3.org/TR/html5/links.html#link-type-author"><code>author</code></a>
link and assert that it responds with a <code>200</code> HTTP status code:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class=""><span class="line">Follow Link author every time {
</span><span class="line">    Expect status code 200
</span><span class="line">}</span></code></pre></td></tr></table></div></figure>

<p>In more complex scenarios, such as involving creating and removal of resources, a nested structure would help build a
sequence of related requests. Here’s how I imagine a complete workflow:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
</pre></td><td class="code"><pre><code class=""><span class="line">With class Person {
</span><span class="line">    Expect Identifier [person]
</span><span class="line">
</span><span class="line">    With Form addFriend {
</span><span class="line">        Submit application/x-www-form-urlencoded 
</span><span class="line">        (
</span><span class="line">            newFriendId=/id/of/friend/
</span><span class="line">        ) {
</span><span class="line">            Expect Status Code 201
</span><span class="line">            Expect Header Location [newFriend]
</span><span class="line">            
</span><span class="line">            Follow [newFriend] {
</span><span class="line">                Expect Property friend /id/of/friend/
</span><span class="line">                Expect Property friendOf [person]
</span><span class="line">            
</span><span class="line">                With Form delete {
</span><span class="line">                    Submit {
</span><span class="line">                        Expect Status Code 204
</span><span class="line">                    }
</span><span class="line">                }
</span><span class="line">            }
</span><span class="line">        }
</span><span class="line">    }
</span><span class="line">}</span></code></pre></td></tr></table></div></figure>

<p>This is just a pseudocode draft but the intention is to keep a clear structure which should read like natural language.
This example should be interpreted as:</p>

<ol>
  <li>When you find a <code>Person</code> resource, remember its identifier as <code>person</code></li>
  <li>If it contains an <code>addFriend</code> form, submit it with a given body</li>
  <li>Check that a resource has been created and <code>GET</code> it by following the <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location"><code>Location</code> header</a></li>
  <li>Verify that it has been created with certain properties</li>
  <li>Use the <code>delete</code> form to remove that resource</li>
</ol>

<h2 id="some-notes-on-the-dsl">Some notes on the DSL</h2>

<p>It is clear that media types are not made equal. They also use various names for similar concepts (eg. <code>form</code> vs <code>operation</code>
vs <code>action</code>). While the initial version will focus on <a href="http://www.hydra-cg.com">Hydra</a>, the DSL should become customizable to
allow plug-in support for other specific media types.</p>

<p>Individual runners would also need to implement media type-specific ways for discovering the hypermedia.</p>

<p>The DSL will then be compiled to a JSON structure, which shall simplify the implementation of runners.</p>

<h2 id="next-steps">Next steps</h2>

<p>We are starting to build the DSL with <a href="https://www.eclipse.org/Xtext/">Eclipse Xtext</a> and generators with <a href="https://www.eclipse.org/xtend/">Xtend</a>. Those are very mature
DSL tools, probably the most sophisticated out there.</p>

<p>Work has also commenced on prototyping a runner targetting Hydra under 
<a href="https://github.com/hypermedia-app/hydra-validator/">https://github.com/hypermedia-app/hydra-validator/</a>.</p>

<h2 id="related-research">Related research</h2>

<p>There seems to be just a handful of research papers and even less development going on around testing hypermedia APIs.
The problem with research papers is also that most of them don’t really produce concrete, runnable tools. The only one that
does from those mentioned below, is apparently not available for download.</p>

<p>A fairly recent library exists, called <a href="https://github.com/Tabcorp/hyperactive">Hyperactive</a>. It crawls an API to check
that the links are not broken between resources. Unfortunately it is essentially just that, a simple crawler.</p>

<p>A similar paper has been published in 2010 titled <a href="https://www.researchgate.net/publication/220796420_Connectedness_testing_of_RESTful_web-services">Connectedness testing of RESTful web-services</a>
by Sujit Chakrabarti of Bangalore. The approach the authors take is quite similar to the proposed DSL. The downside,
shown also in the papers I mention below, is that it seems to be tightly coupled to URL structures and specific implementation
details, such as HTTP methods. Our approach differs in that it should rely more on the hypermedia control rather than
out-of-band information.</p>

<p>Another, quite promising paper is <a href="https://www.semanticscholar.org/paper/Model-Based-Testing-of-RESTful-Web-Services-Using-Pinheiro-Endo/4824acc46b9454da7c81e57a9c8eea3a1795bb90">Model-Based Testing of RESTful Web Services Using UML Protocol State Machines</a>
by Pedro Victor Pontes Pinheiro, André Takeshi Endo, Adenilso da Silva Simão, published in 2013. Instead of DSL, UML diagrams
are used to build the interaction paths. Other than that it seems that the proposed tool (I could not find the code) has
some good features, including coverage. The presented approach does not seem suffer from the problem of hardcoding URLs, etc.</p>

<p>There’s also <a href="https://link.springer.com/chapter/10.1007/978-3-642-22110-1_43">Formalization and Automated Verification of RESTful Behavior</a> by Uri Klein and Kedar S. Namjoshi which
unfortunately is exceedingly scientific, as the name rightfully implies, riddled with cryptic mathematical-esque formulas.</p>

<p>Finally, 2017’s <a href="https://link.springer.com/article/10.1007/s10270-017-0647-0">Towards Property-Based Testing of RESTful Web Services</a> by Pablo Lamela Seijas, Huiqing Li and
Simon Thompson proposes an Erlang-based DSL. Unfortunately the resulting syntax is hardly welcoming, and the approach in
general is again in opposition to hypermedia controls. The shown examples are limited to JSON and revolve around URIs and
hardcoded HTTP methods.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Writing storybook inlined with markdown]]></title>
    <link href="http://t-code.pl/blog/2018/08/storybook-markdown-inline/"/>
    <updated>2018-08-11T12:15:00+00:00</updated>
    <id>http://t-code.pl/blog/2018/08/storybook-markdown-inline</id>
    <content type="html"><![CDATA[<p>What good are stories if you’re not actually telling them? Storybook is a fantastic and versatile tool
to create runnable showrooms of elements written in a number of javascript libraries. It presents live
examples of components but lack in plain old storytelling - the prose. There are addons but they are not 
presentable enough (<a href="https://github.com/storybooks/storybook/blob/master/addons/notes">addon-notes</a>) or do not support all targets (<a href="https://github.com/storybooks/storybook/blob/master/addons/info">addon-info</a>).</p>

<p>Here I present a different approach, using <a href="https://polymer.github.io/lit-html/">lit-html</a> and a markdown <a href="https://developers.google.com/web/fundamentals/web-components/customelements">custom element</a>. It works
well with my web components but hopefully could be adapted to handle any supported framework.</p>

<!--more-->

<h2 id="what-are-you-talking-about">What are you talking about?</h2>

<p>Even if the addon-notes could be easily styled ot addon-info worked with something else than only React, I
kind of think that putting the textual description of the addons panel make them seem irrelevant. An
afterthought. Instead, I’d rather it was a prominent part of each story. Sharing space with the live examples.
Only then the stories create a complete documentation pages and not just a set of naked elements you can
prod.</p>

<p>Here’s the end effect presenting my web components: <a href="https://wikibus.github.io/lit-any/">https://wikibus.github.io/lit-any/</a></p>

<p><img src="http://t-code.pl/images/lit-any.png" alt="lit-any storybook" /></p>

<p>Isn’t that the way most documentations out there look like?</p>

<h2 id="writing-stories-inside-markdown">Writing stories inside markdown</h2>

<p>Creating a story is not much different from your typical setup.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">storiesOf</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;@storybook/polymer&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">html</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;lit-html/lib/lit-extended&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="nx">markdownNotes</span> <span class="nx">from</span> <span class="s1">&#39;./notes/lit-form/fallback-input&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="nx">storiesOf</span><span class="p">(</span><span class="s1">&#39;my-component&#39;</span><span class="p">,</span> <span class="nx">module</span><span class="p">)</span>
</span><span class="line">    <span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="s1">&#39;Nicely documented&#39;</span><span class="p">,</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">        <span class="kr">const</span> <span class="nx">story</span> <span class="o">=</span> <span class="nx">html</span><span class="err">`</span><span class="o">&lt;</span><span class="nx">my</span><span class="o">-</span><span class="nx">component</span><span class="o">&gt;&lt;</span><span class="err">/my-component&gt;`;</span>
</span><span class="line">
</span><span class="line">        <span class="k">return</span> <span class="nx">markdownNotes</span><span class="p">(</span><span class="nx">story</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>All of the markdown sits in a separate file. It is also javascript so I suppose it could be written right
nex to the story itself.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="nx">md</span> <span class="nx">from</span> <span class="s1">&#39;../markdown&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">story</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="k">return</span> <span class="nx">md</span><span class="err">`##</span> <span class="nx">My</span> <span class="nx">component</span>
</span><span class="line">
</span><span class="line"><span class="nx">This</span> <span class="nx">is</span> <span class="nx">markdown</span><span class="p">.</span>
</span><span class="line">
</span><span class="line"><span class="err">##</span> <span class="nx">Running</span> <span class="nx">example</span>
</span><span class="line">
</span><span class="line"><span class="nx">$</span><span class="p">{</span><span class="nx">story</span><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="err">##</span> <span class="nx">How</span> <span class="nx">it</span> <span class="nx">works</span>
</span><span class="line">
</span><span class="line"><span class="nx">More</span> <span class="nx">beautiful</span> <span class="nx">docs</span> <span class="nx">go</span> <span class="nx">here</span><span class="p">.</span> <span class="nx">And</span> <span class="nx">maybe</span> <span class="nx">more</span> <span class="nx">live</span> <span class="nx">examples</span> <span class="nx">too</span><span class="p">.</span>
</span><span class="line">
</span><span class="line"><span class="o">---</span><span class="nx">html</span>
</span><span class="line"><span class="o">&lt;</span><span class="nx">my</span><span class="o">-</span><span class="nx">component</span><span class="o">&gt;</span>
</span><span class="line">    <span class="nx">Instead</span> <span class="nx">of</span> <span class="nx">backticks</span> <span class="nx">hyphens</span> <span class="nx">are</span> <span class="nx">used</span> <span class="nx">to</span> <span class="nx">avoid</span> <span class="nx">slash</span> <span class="nx">escaping</span><span class="p">.</span>
</span><span class="line"><span class="o">&lt;</span><span class="err">/my-component&gt;</span>
</span><span class="line"><span class="o">---</span><span class="err">`</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>As you see, the actual story will be rendered within the formatted text</p>

<h2 id="rendering-markdown">Rendering markdown</h2>

<p>The interesting bit you may notice above is the <code>md</code> import which in fact is a <a href="(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates">template string tag function</a>.
Here’s my lit-html implementation which wraps all static portions with a markdown rendering custom element
and combines them with the stories. It also handles non-template values so that it’s possible to inject
not only stories but also any other content dynamically into the documentation template.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">html</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;lit-html/lib/lit-extended&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">TemplateResult</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;lit-html&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="s1">&#39;../../bower_components/zero-md/build/zero-md.html&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kd">function</span> <span class="nx">createZeroMd</span><span class="p">(</span><span class="nx">markdown</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="kr">const</span> <span class="nx">zeroMd</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">createElement</span><span class="p">(</span><span class="s1">&#39;zero-md&#39;</span><span class="p">);</span>
</span><span class="line">    <span class="nx">zeroMd</span><span class="p">.</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="err">`</span><span class="o">&lt;</span><span class="nx">template</span><span class="o">&gt;&lt;</span><span class="nx">xmp</span><span class="o">&gt;</span><span class="nx">$</span><span class="p">{</span><span class="nx">markdown</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/---/g</span><span class="p">,</span> <span class="s1">&#39;```&#39;</span><span class="p">)}</span><span class="o">&lt;</span><span class="err">/xmp&gt;&lt;/template&gt;`;</span>
</span><span class="line">
</span><span class="line">    <span class="k">return</span> <span class="nx">zeroMd</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">strings</span><span class="p">,</span> <span class="p">...</span><span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="k">if</span> <span class="p">(</span><span class="nx">keys</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">html</span><span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">strings</span><span class="p">[</span><span class="mi">0</span><span class="p">]}</span><span class="err">`</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="kd">let</span> <span class="nx">result</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">;</span>
</span><span class="line">    <span class="kd">let</span> <span class="nx">currentMarkdown</span> <span class="o">=</span> <span class="nx">strings</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>
</span><span class="line">    <span class="nx">keys</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">key</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">        <span class="k">if</span> <span class="p">(</span><span class="nx">key</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">===</span> <span class="nx">TemplateResult</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">            <span class="nx">result</span> <span class="o">=</span> <span class="nx">html</span><span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">result</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">createZeroMd</span><span class="p">(</span><span class="nx">currentMarkdown</span><span class="p">)}</span> <span class="o">&lt;</span><span class="nx">br</span><span class="o">&gt;</span> <span class="nx">$</span><span class="p">{</span><span class="nx">key</span><span class="p">}</span> <span class="o">&lt;</span><span class="nx">br</span> <span class="o">&lt;</span><span class="nx">hr</span><span class="o">&gt;</span> <span class="o">&lt;</span><span class="nx">br</span><span class="o">&gt;</span> <span class="err">`</span><span class="p">;</span>
</span><span class="line">            <span class="nx">currentMarkdown</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">;</span>
</span><span class="line">        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class="line">            <span class="nx">currentMarkdown</span> <span class="o">=</span> <span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">currentMarkdown</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">strings</span><span class="p">[</span><span class="nx">i</span><span class="p">]}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">key</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">        <span class="k">if</span> <span class="p">(</span><span class="nx">i</span> <span class="o">===</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">            <span class="nx">currentMarkdown</span> <span class="o">=</span> <span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">currentMarkdown</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">strings</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]}</span><span class="err">`</span><span class="p">;</span>
</span><span class="line">            <span class="nx">result</span> <span class="o">=</span> <span class="nx">html</span><span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="nx">result</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">createZeroMd</span><span class="p">(</span><span class="nx">currentMarkdown</span><span class="p">)}</span><span class="err">`</span><span class="p">;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">    <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Since I’m creating web components, it came natural to me to compose my documentation pages using lit-html
and a 3rd party custom element to render markdown. There are a number to <a href="https://www.webcomponents.org/search/markdown">choose from</a>. I chose
<code>&lt;zero-md&gt;</code> which works well and I simply have used it before. Any other should be good too as long as it
can be be fed with markdown directly from HTML (as opposed to external <code>.md</code> files).</p>

<p>To load it I use <code>polymer-webpack-loader</code>. I tried adding the element to <code>preview-head.html</code> but it 
somehow interferes with the polyfill. Bundling with webpack is good enough. Installing from bower could also
be a faux pas but hey, it works.</p>

<h2 id="room-for-improvement">Room for improvement</h2>

<p>At the point of writing the latest release of <code>lit-html</code> cannot render inside <code>&lt;template&gt;</code> tag. This should
change soon but for now I build the <code>&lt;zero-md&gt;</code> elements by hand.</p>

<p>Now that I think about it, the markdown rendering element could be replaced with simple JS-based
transformation. The element however comes with styling capabilities and by default imports GitHub rendering
styles.</p>

<p>Another current limitation of lit-html is that <code>import { html } from 'lit-html</code> cannot be mixed with
<code>import { html } from 'lit-html/lib/lit-extended</code>. It’s also about to change soon but something to keep in
mind.</p>

<p>A lit-html-based implementation probably would not work with React but it should be simple enough to compose
the content with jsx instead in a similar fashion.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Maintaining documentation of JS library in lockstep with code]]></title>
    <link href="http://t-code.pl/blog/2018/05/js-living-docs/"/>
    <updated>2018-05-06T18:20:00+00:00</updated>
    <id>http://t-code.pl/blog/2018/05/js-living-docs</id>
    <content type="html"><![CDATA[<p>I’ve long been aware of GitBook.com as a way to easily author documentation pages. What I did not know before
was that it also comes with a robust tooling for building the book locally and building as a static website.
Works great with GitHub Pages, albeit needing some specific setup to run from the <code>/docs</code> folder. In itself
though it may just be a good-looking alternative to other static page generators or documentation builders.
The difference however is the abundance of plugins, and one plugin in particular useful for documenting JS
code.</p>

<!--more-->

<h2 id="setting-up-gitbook-for-github-pages">Setting up GitBook for GitHub pages</h2>

<p>The GitBook toolchain has some setting which collide with how GitHub Pages expect you to organize your
repository. It’s nothing really problematic but impossible to change so you may as well be aware of how
to work with both together.</p>

<p>The first step is to prepare your repository to host both code and documentation. See <a href="https://toolchain.gitbook.com">this gitbook</a>
to set up and install the command line tool.</p>

<p>Next, initialize a folder for the markdown sources of your documentation pages. I called that folder <code>gitbook</code>.
You can do it by running the command below.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="bash"><span class="line">gitbook init ./gitbook
</span></code></pre></td></tr></table></div></figure>

<p>You will also have to create a <code>book.json</code> configuration file pointing to the root of you book</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;root&quot;</span><span class="p">:</span> <span class="s2">&quot;./gitbook&quot;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>To test your GitBook locally you can run <code>gitbook serve</code>. Note that you will want to ignore a folder called
<code>_book</code> which is where the site is being generated for serving locally.</p>

<p>Once you’ve created some pages you build static documentation pages by running <code>gitbook build</code>. In the latest
version of GitBook CLI there is no setting to control the output folder and it will by default write the
output to the same <code>_book</code> folder. GitHub pages however expects the static pages to be served from a <code>/docs</code>
folder, which also cannot be changed. The only way to make both happy is to pass an output folder to the
build command. I added a complete command to my <code>package.json</code>.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;scripts&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;build&quot;</span><span class="p">:</span> <span class="s2">&quot;webpack; gitbook build . docs&quot;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>This will take the sources from the root, as set up in <code>book.json</code> and write HTML to <code>/docs</code>. That should be,
just commit and push to publish your book on github.io.</p>

<h2 id="writing-live-snippets">Writing live snippets…</h2>

<p>As mentioned before, GitBook itself is nice but the real great feature are numerous plugins, and one plugin
in particular: <a href="https://www.npmjs.com/package/gitbook-plugin-runkit">RunKit</a>. 
As <a href="https://runkit.com/">its page</a> states</p>

<blockquote>
  <p>RunKit is Node prototyping</p>
</blockquote>

<p>It lets anyone create actual live snippets running node with any package available on npmjs.org registry.
It is actually possible to load any particular version, similarly to how you would install a specific version
with Yarn on NPM. Additionally it also wraps the snippets in an async function so that they can use the 
<code>await</code> keyword instead of promises and renders a nice output for JS objects or HTML. Go ahead and see what
happens when you paste the snippet below snippet on <a href="https://runkit.com/">runkit.com</a>.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">const</span> <span class="nx">fetch</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;isomorphic-fetch@2.2.0&#39;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line"><span class="kr">const</span> <span class="nx">xhr</span> <span class="o">=</span> <span class="nx">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
</span><span class="line"><span class="nx">await</span> <span class="nx">xhr</span><span class="p">.</span><span class="nx">text</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>

<h3 id="inside-you-gitbook">…inside you GitBook…</h3>

<p>Finally, the GitBook plugin makes it trivial to embed actual snippets within your documentation. To install
it add the plugin in you <code>book.json</code></p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;plugins&quot;</span><span class="p">:</span> <span class="p">[</span>
</span><span class="line">    <span class="s2">&quot;runkit&quot;</span>
</span><span class="line">  <span class="p">]</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>And run <code>gitbook install</code>.</p>

<p>Now you can add a runkit embed using a special <code>runkit</code> block:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class=""><span class="line">{% runkit %} 
</span><span class="line">const fetch = require('isomorphic-fetch@2.2.0');
</span><span class="line">
</span><span class="line">const xhr = await fetch('http://google.com');
</span><span class="line">await xhr.text();
</span><span class="line">{% endrunkit %}</span></code></pre></td></tr></table></div></figure>

<h3 id="and-keeping-them-up-to-date-with-code">…and keeping them up to date with code</h3>

<p>The ability to request any chosen version from NPM has one great implication. You can have the embedded
snippets always use a version matching the state of the repository. Instead of keeping a concrete number
in the runkit snippets, GitBook lets the authors create variables and inject them in their pages. Here’s
my config file:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;variables&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;version&quot;</span><span class="p">:</span> <span class="s2">&quot;0.4.0-a5&quot;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>It defines a variable which I use in all my snippets so that they use the most recent version of my
library:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class=""><span class="line">{% runkit %} 
</span><span class="line">const fetch = require('alcaeus@{{ book.version }}');
</span><span class="line">
</span><span class="line">// more snippet
</span><span class="line">{% endrunkit %}</span></code></pre></td></tr></table></div></figure>

<p>At this point there are simple steps to follow in order to always have the documentation using the right
version:</p>

<ol>
  <li>Change my code as usual</li>
  <li>Publish next version to NPM</li>
  <li>Update documentation</li>
  <li>Bump the GitBook <code>version</code> variable</li>
  <li>Build GitBook</li>
  <li>Commit, tag and push repository</li>
</ol>

<p>This way the online documentation always point to the most recent version but anyone checking out any tag
will be able to run the documentation as it existed at that point in time. And the code examples will use
the matching version from NPM!</p>

<h3 id="bonus-documentating-multiple-versions-of-the-library">Bonus: documentating multiple versions of the library</h3>

<p>With a simple modification of these steps one could also keep multiple versions of the documentation,
targeting multiple versions of the library.</p>

<ol>
  <li>Build the book to a <code>/docs/latest</code> instead</li>
  <li>Whenever you’re ready to tag you repository, make a copy of that folder to one named after the
version. For example <code>/docs/v0.9</code>, <code>/docs/1.0</code>, <code>/docs/2.0</code>, etc.</li>
</ol>

<p>This way you will keep all past versions documented alongside the latest on 
<code>github.io/my-lib/v0.9</code>, <code>github.io/my-lib/v1.0</code> and <code>github.io/my-lib/v2.0</code> respectively.</p>

<h2 id="see-it-in-action">See it in action</h2>

<p>Go ahead and check Alcaeus’ documentation pages at <a href="https://alcaeus.hydra.how">https://alcaeus.hydra.how</a>
to see live examples published using the RunKit plugin.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hidden gem - easiest way to manage SqlLocalDB]]></title>
    <link href="http://t-code.pl/blog/2018/05/hidden-gem-sqllocaldb/"/>
    <updated>2018-05-06T18:20:00+00:00</updated>
    <id>http://t-code.pl/blog/2018/05/hidden-gem-sqllocaldb</id>
    <content type="html"><![CDATA[<p>SQL Server LocalDB is not something new to me. The ability quickly to run, and destroy a database without much
hassle has been great aid in running test code which was meant to target a live SQL database. That said, it
wasn’t always without any hassle at all. I tried various tools which make it a bit easier than the command line
tool but nothing was perfect. Until recently, when I’ve discovered the humbly named <a href="https://www.nuget.org/packages/SqlLocalDb/">NuGet package</a> which
is as simple as it gets.</p>

<!--more-->

<h2 id="what-is-sql-localdb">What is SQL LocalDB?</h2>

<p>SQL Server LocalDB is a simple utility which ships with recent SQL Server editions (Express included). It let’s
you create a temporary, yet full-featured database. It may not seem like much, after all it’s possible with SQL
server itself to connect to <code>master</code> database, run <code>create database</code> and initialize it according to your needs.</p>

<p>The difference is though that one doesn;t have to manage two connections and doesn’t have to share credentials
to the <code>master</code> database or require Windows Authentication. Instead, an automatic instance is always available
locally. However to run a fully isolated database it is necessary to create it, start it and eventually destroy.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class=""><span class="line">&gt; SqlLocalDB.exe create MyTestingDB
</span><span class="line">&gt; SqlLocalDB.exe start MyTestingDB
</span><span class="line">&gt; SqlLocalDB.exe stop MyTestingDB
</span><span class="line">&gt; SqlLocalDB.exe delete MyTestingDB</span></code></pre></td></tr></table></div></figure>

<p>It is also possible to use a magic <code>Server=(LocalDB)\MSSQLLocalDB</code> connection string (also supports
attaching to file db). Personally though I’ve had mixed results with using a connection string. To be honest
I never fully understood how it’s supposed to be used ;).</p>

<h2 id="managed-code-to-the-rescue">Managed code to the rescue</h2>

<p>There are a number of C# libraries which aim at simplifying the use of SQL LocalDB. In a previous project
we had integration tests run against a temporary database created in code yet the steps still followed the 
same patter showed above:</p>

<ol>
  <li>Create LocalDB instance</li>
  <li>Start the instance</li>
  <li>Get its connection string</li>
  <li>Create a ADO.NET connection</li>
  <li>Stop the databse</li>
  <li>Destroy it</li>
</ol>

<p>I had a feeling back then that it’s not as friendly as it could get and just this week, while migrating an
old <a href="https://github.com/r2rml4net/r2rml4net">open source library of mine</a> to .NET Standard I discovered what has to be the most 
hassle-free solution.</p>

<h3 id="sqllocaldbnupkg">SqlLocalDb.nupkg</h3>

<p>Turns out it’s not new but it somehow slipped under my radar the last time I was looking.</p>

<p>It’s really dead simple to use it:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
</pre></td><td class="code"><pre><code class="c#"><span class="line"><span class="n">using</span><span class="p">(</span><span class="kt">var</span> <span class="n">database</span> <span class="p">=</span> <span class="k">new</span> <span class="n">LocalDatabase</span><span class="p">())</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">connection</span> <span class="p">=</span> <span class="n">database</span><span class="p">.</span><span class="n">GetConnection</span><span class="p">())</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">connection</span><span class="p">.</span><span class="n">Open</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">        <span class="c1">// do your thing</span>
</span><span class="line">        <span class="c1">// run your tests</span>
</span><span class="line">        <span class="c1">// or whatever</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>No need to manage the LocalDB instances at all. The library will make sure that it’s created and then get
rid of it once the <code>LocalDatabase</code> object is disposed. It’s as convenient as it gets.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to set up storybook to play nice with lit-html-based element]]></title>
    <link href="http://t-code.pl/blog/2018/03/storybook-with-lit-html-and-polymer-3/"/>
    <updated>2018-03-26T21:15:00+00:00</updated>
    <id>http://t-code.pl/blog/2018/03/storybook-with-lit-html-and-polymer-3</id>
    <content type="html"><![CDATA[<p>I’d like to share a few tips which might help you set up <a href="https://www.npmjs.com/package/@storybook/polymer">@storybook/polymer</a> with a JS-only elements. The
core issue was importing a base mixin class from Polymer 3, which causes the default babel configuration to 
transpile into an unusable bundle.</p>

<!--more-->

<h2 id="background">Background</h2>

<p>In <a href="https://github.com/wikibus/lit-any">my code</a> I have a base class which uses Polymer’s <code>PropertiesChanged</code> mixin to offload handling attributes
and properties:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">PropertiesChanged</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;@polymer/polymer/lib/mixins/properties-changed&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="k">default</span> <span class="kr">class</span> <span class="nx">LitAnyBase</span> <span class="kr">extends</span> <span class="nx">PropertiesChanged</span><span class="p">(</span><span class="nx">HTMLElement</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">  <span class="cm">/* rest of my code */</span>
</span><span class="line">
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>There is not build step and the code works great when bundled using webpack in an actual web application.</p>

<h2 id="the-issue-with-storybook">The issue with storybook</h2>

<p>Unfortunately this does not work of out the box with Storybook for Web Components (maybe it will be renamed
after all). By design the storybook generates a shell <code>index.html</code> which loads web components polyfill and 
<code>custom-elements-es5-adapter.js</code>. The latter requires that all <strong>ES6 be transpiled into ES5</strong>. Otherwise any of
the code would not work in older browsers which do not support classes.</p>

<p>The thing is though that it’s an all or nothing approach. Without the adapter all code must be ES6, as per
<a href="https://developers.google.com/web/fundamentals/web-components/customelements#define">custom elements v1</a> spec. With the adapter on other hand it is not possible to mix the two approaches. So what
happens when Storybook generates its bundle? My code gets transpiled as expected:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kd">var</span> <span class="nx">LitAnyBase</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">_PropertiesChanged</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">  <span class="nx">_inherits</span><span class="p">(</span><span class="nx">LitAnyBase</span><span class="p">,</span> <span class="nx">_PropertiesChanged</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">  <span class="kd">function</span> <span class="nx">LitAnyBase</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">    <span class="nx">_classCallCheck</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">LitAnyBase</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="kd">var</span> <span class="nx">_this</span> <span class="o">=</span> <span class="nx">_possibleConstructorReturn</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="p">(</span><span class="nx">LitAnyBase</span><span class="p">.</span><span class="nx">__proto__</span> <span class="o">||</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getPrototypeOf</span><span class="p">(</span><span class="nx">LitAnyBase</span><span class="p">)).</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">));</span>
</span><span class="line">
</span><span class="line">      <span class="k">return</span> <span class="nx">_this</span><span class="p">;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="cm">/* the ugly ES5 continues here */</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p><code>_PropertiesChanged</code> however would still be as ES6 class <em>(because it comes from <code>node_modules</code>?)</em> even though
they both end up in the very same bundle. The effect is a sight many web component developers probably witnessed:</p>

<blockquote>
  <p>TypeError: Class constructor PropertiesChanged cannot be invoked without ‘new’</p>
</blockquote>

<h2 id="solution">Solution</h2>

<p>It had taken me a few hours of futile experiments with various webpack and babel configs until my colleague 
<a href="https://twitter.com/riscent">Kamil</a> helped me out. Turns out all it took was a minimal babel setup.</p>

<h3 id="initial-setup">Initial setup</h3>

<p>First things first, here are the steps I used to set up Storybook in my repository, similar to the
<a href="https://storybook.js.org/basics/slow-start-guide/">Slow start guide</a></p>

<ol>
  <li><code>yarn add -D @storybook/polymer</code></li>
  <li>Create the NPM script</li>
  <li>Create my <a href="">config</a> and a first <a href="">story</a></li>
</ol>

<p>When started, nothing will show and the error mentioned above will be reported in the console.</p>

<h3 id="steps-to-fix">Steps to fix</h3>

<p>Turns out the fix is not so difficult but also not obvious for a weekend JS dev <img class="emoji" title=":joy:" alt=":joy:" src="https://assets.github.com/images/icons/emoji/unicode/1f602.png" height="20" width="20" align="absmiddle" />.
One, it’s necessary to install babel presets:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class=""><span class="line">yarn add -D babel-preset-react babel-preset-env babel-preset-es2015 babel-preset-stage-0</span></code></pre></td></tr></table></div></figure>

<p>Next create <code>.babelrc</code> as follows. It is interesting that <code>es2015</code> in not required to be called out
explicitly but it has to be installed as a dependency nonetheless.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;presets&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;react&quot;</span><span class="p">,</span> <span class="s2">&quot;env&quot;</span><span class="p">,</span> <span class="s2">&quot;stage-0&quot;</span><span class="p">]</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Then also <code>yarn add -D babel-plugin-transform-decorators-legacy</code>.</p>

<p>Finally the simplest webpack config must be added next to storybooks config file.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">  <span class="nx">module</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">    <span class="nx">rules</span><span class="o">:</span> <span class="p">[</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">        <span class="nx">test</span><span class="o">:</span> <span class="sr">/\.js$/</span><span class="p">,</span>
</span><span class="line">        <span class="nx">use</span><span class="o">:</span> <span class="s1">&#39;babel-loader&#39;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">    <span class="p">]</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>

<p>It will cause all of <code>node_modules</code> to be loaded by babel which can take significantly more to build, but
hey, at least now my storybook works! <img class="emoji" title=":sparkles:" alt=":sparkles:" src="https://assets.github.com/images/icons/emoji/unicode/2728.png" height="20" width="20" align="absmiddle" /></p>

<h2 id="update-for-storybook-4">Update for Storybook 4</h2>

<p>I recently updated to Storybook <code>4.0.alpha.9</code>. It seems to be working fine despite some benign errors showing 
in the console. It didn’t “just work” though. Right after upgrade I was running into similar issues with bundling.
To fix that the <code>babel-loader</code> needs a minor tweak:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="diff"><span class="line">rules: [
</span><span class="line">  {
</span><span class="line">    test: /\.js$/,
</span><span class="line">    use: &#39;babel-loader&#39;,
</span><span class="line"><span class="gi">+       exclude: /node_modules\/@webcomponents/</span>
</span><span class="line">  }
</span><span class="line">]
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Visual Studio must-have - Open Command Line (and Babun)]]></title>
    <link href="http://t-code.pl/blog/2017/10/vs-must-have-open-command-line/"/>
    <updated>2017-10-25T08:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2017/10/vs-must-have-open-command-line</id>
    <content type="html"><![CDATA[<p>I’ve been using the <a href="https://marketplace.visualstudio.com/items?itemName=MadsKristensen.OpenCommandLine">Open Command Line</a>
by Mads Kristensen for several months now. Recently I’ve finally figured out how to have it open <a href="http://babun.github.io">Babun</a> and
decided to share.</p>

<!--more-->

<p>The extension is quite simple. It adds a menu item to folders in Solution Explorer and a shortcut which will open a new
terminal window. By default it’s <code>Alt+Space</code>, which I haven’t felt like changing.</p>

<p>Not only does it open that console, it will actually set the working directory based on the open file or selected item in
Solution Explorer, whichever is focused.</p>

<p><img src="http://t-code.pl/images/open-cmd.png" alt="open cmd" /></p>

<p>Default settings include Developer Command Prompt but most importantly it let’s you set up a custom terminal to run.</p>

<h2 id="setting-up-with-babun-the-right-way">Setting up with Babun the right way</h2>

<p>In the past I had already set up Open Command Line to open Babun but it opened an ugly cygwin window which I didn’t like.
It made me use the extension sparingly. With proper setup however I use it all the time.</p>

<p>To set it up go to <code>Tools -&gt; Optiona -&gt; Command Line</code> and input these values</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
</pre></td><td class="code"><pre><code class="yaml"><span class="line"><span class="l-Scalar-Plain">Friendly Name</span><span class="p-Indicator">:</span>
</span><span class="line">  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Default (Custom)</span>
</span><span class="line"><span class="l-Scalar-Plain">Select preset</span><span class="p-Indicator">:</span>
</span><span class="line">  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">Custom</span>
</span><span class="line"><span class="l-Scalar-Plain">Command</span><span class="p-Indicator">:</span>
</span><span class="line">  <span class="p-Indicator">-</span> <span class="err">%</span><span class="l-Scalar-Plain">UserProfile%\.babun\cygwin\bin\mintty.exe</span>
</span><span class="line"><span class="l-Scalar-Plain">Command arguments</span><span class="p-Indicator">:</span>
</span><span class="line">  <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">/bin/env CHERE_INVOKING=1 /bin/zsh.exe</span>
</span></code></pre></td></tr></table></div></figure>

<p><img src="http://t-code.pl/images/open-cmd-babun.png" alt="open cmd" /></p>

<p>Oh, and guess what. <a href="https://github.com/madskristensen/OpenCommandLine/pull/60">Babun will become one of the default presets soon</a>.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Building UI like LEGO (with string template literals)]]></title>
    <link href="http://t-code.pl/blog/2017/08/lego-templates-with-template-literals/"/>
    <updated>2017-08-29T22:45:00+00:00</updated>
    <id>http://t-code.pl/blog/2017/08/lego-templates-with-template-literals</id>
    <content type="html"><![CDATA[<p>Some time ago I experimented and <a href="http://t-code.pl/blog/2016/04/hypermedia-driven-ui/">wrote</a> about building composable UI using Polymer and <code>&lt;template&gt;</code> was my
main building block. I used it to declare building blocks for my pages which I would dynamically interchange depending on
the displayed content. Unfortunately I’ve hit a number of roadblocks but I think I’ve just recently found a solution.</p>

<!--more-->

<p>Just last week I attended the <a href="https://www.youtube.com/watch?v=yTASiOaXlck">third Polymer Summit</a> in Copenhagen where <a href="https://twitter.com/justinfagnani">Justin Fagnani</a> showed his
newest experiment: <a href="https://github.com/PolymerLabs/lit-html"><code>lit-html</code></a>. You should definitely watch his presentation:</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">What is the middle ground between full VDOM and manual DOM manipulation? Check out <a href="https://twitter.com/justinfagnani">@justinfagnani</a>’s lit-html <a href="https://t.co/a0aR7c70FV">https://t.co/a0aR7c70FV</a></p>&mdash; Surma (@DasSurma) <a href="https://twitter.com/DasSurma/status/902500278905303041">August 29, 2017</a></blockquote>
<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

<h2 id="the-end-of-dom-module">The end of <code>&lt;dom-module&gt;</code></h2>

<p>Why is this important? Apparently, the next version of Polymer won’t directly use the <code>&lt;template&gt;</code> tag. Instead, it will
be 100% JS. Yes, you heard correctly. No more <code>&lt;dom-module&gt;</code>:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">Element</span> <span class="nx">as</span> <span class="nx">PolymerElement</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;../@polymer/polymer/polymer-element.js&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">MyElement</span> <span class="kr">extends</span> <span class="nx">PolymerElement</span> <span class="p">{</span>
</span><span class="line">    <span class="kr">static</span> <span class="nx">get</span> <span class="nx">template</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">      <span class="k">return</span> <span class="err">`</span>
</span><span class="line">      <span class="o">&lt;</span><span class="nx">h1</span><span class="o">&gt;</span><span class="nx">Hello</span> <span class="nx">World</span><span class="o">!&lt;</span><span class="err">/h1&gt;</span>
</span><span class="line">    <span class="err">`</span><span class="p">;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line"><span class="nx">customElements</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">&#39;my-element&#39;</span><span class="p">,</span> <span class="nx">MyElement</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>

<p>As you see, instead of HTML+JS, there is only code. Looks more like React+JSX, doesn’t it? It sparked heated discussions
at the conference, on Polymer’s Slack channel and on Twitter.</p>

<p>At this stage though, the above <code>template()</code> method returns a static HTML string, which is then injected into a HTML
template and later stamped into the element’s shadow root. Justin’s lit-html, akin to a number of earlier libraries takes
this one step further, thanks to the properties of JavaScript’s <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals">template literals</a> (the backtick strings, duh!).</p>

<h2 id="template-literals">Template literals</h2>

<p>Here’s an example how a basic component could use lit-html</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">CountLit</span> <span class="kr">extends</span> <span class="nx">HTMLElement</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">  <span class="nx">constructor</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">    <span class="kr">super</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">    <span class="k">this</span><span class="p">.</span><span class="nx">counter</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="nx">connectedCallback</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">    <span class="k">this</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s1">&#39;click&#39;</span><span class="p">,</span> <span class="nx">e</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">      <span class="k">this</span><span class="p">.</span><span class="nx">counter</span><span class="o">++</span><span class="p">;</span>
</span><span class="line">      <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span>
</span><span class="line">    <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="k">this</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line">  <span class="nx">render</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">    <span class="kd">let</span> <span class="nx">times</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">    <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">counter</span> <span class="o">===</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">      <span class="nx">times</span> <span class="o">=</span> <span class="s1">&#39;time&#39;</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class="line">      <span class="nx">times</span> <span class="o">=</span> <span class="s1">&#39;times&#39;</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="kr">const</span> <span class="nx">template</span> <span class="o">=</span> <span class="nx">html</span><span class="err">`</span><span class="nx">You</span> <span class="nx">clicked</span> <span class="nx">me</span> <span class="nx">$</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">counter</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="nx">times</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">    <span class="nx">render</span><span class="p">(</span><span class="nx">template</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>This is has all advantages of JS: scoping, syntax highlighting and suggestions and the ability to compose a template from
multiple other literals. This opens completely new possibilities where one can create decorator components or override
extensible points of parent element. Features which were very cumbersome with plain <code>&lt;template&gt;</code> tags.</p>

<p>But most importantly, lit-html is <strong>FAST</strong>.</p>

<h2 id="lit-html-is-not-virtual-dom-but-better">lit-html is not Virtual DOM… but better</h2>

<p>By design, the template literal can be prefixed with a tag (did you open the MDN link above?).
A tag, in this case called <code>html</code> is actually a function with a simple signature:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="nx">html</span><span class="p">(</span><span class="nx">strings</span><span class="p">,</span> <span class="p">...</span><span class="nx">values</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>

<p>The strings will be an array of all static parts and the values are the interpolated expressions. The trick is that
whenever the <code>render</code> function is called with the same template it will actually be just one instance (even if it’s) not
visible in code. lit-html takes advantage of that fact and whenever the same template is used, it will only update any
changed expressions.</p>

<p>In the example above each click will only update a tiny piece of the rendered HTML which will keep DOM operations to the
minimum. Even though the <code>times</code> variable is calculated each time, it will only ever be rendered when it actually changes
between renders. Not every time.</p>

<h2 id="so-what-about-declarative-ui">So what about declarative UI?</h2>

<p>Previously I struggled to bend <code>&lt;template&gt;</code> to suit my needs in pursuit of a declarative solution for defining views
which are dynamically selected based on the content.</p>

<p>First of all, in my current implementation the order in which the templates appear in the page is important for the
order in which the will be selected. In case there are multiple matches.</p>

<p>Secondly, I used Polymer 1.0’s <a href="http://t-code.pl/blog/2015/08/polymer-templatizer/"><code>Templatizer</code></a> which not only disappeared in Polymer 2.0, but it was also
notoriously buggy and hard to master.</p>

<p>With <code>lit-html</code> I will be “freed” from Polymer and likely implement my elements will in plain JS. Additionally it will
be much easier to work with those templates; to extend with ES classes and compose with less custom element on the page.</p>

<h3 id="some-sample-code">Some sample code</h3>

<p>At the top level would still be a view element</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="nt">&lt;object-view</span> <span class="na">id=</span><span class="s">&quot;top-view&quot;</span><span class="nt">&gt;&lt;/object-view&gt;</span>
</span><span class="line">
</span><span class="line"><span class="nt">&lt;script&gt;</span>
</span><span class="line">    <span class="kd">var</span> <span class="nx">objectView</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;top-view&#39;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="nx">objectView</span><span class="p">.</span><span class="nx">object</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">        <span class="s2">&quot;@type&quot;</span><span class="o">:</span> <span class="s2">&quot;http://example.com/vocab#Person&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="s2">&quot;http://example.com/vocab#avatar&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">            <span class="s2">&quot;@type&quot;</span><span class="o">:</span> <span class="s2">&quot;http://schema.org/ImageObject&quot;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line"><span class="nt">&lt;/script&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>But from there on it could be all templates composed of smaller parts. Each part rendered with a template selected from
some <code>templateRepository</code>.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span><span class="nx">TemplateRepository</span><span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;template-repository&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span><span class="nx">render</span><span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;template-selector&#39;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span><span class="nx">html</span><span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;lit-html&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="cm">/**</span>
</span><span class="line"><span class="cm">* Will render http://example.com/vocab#Person</span>
</span><span class="line"><span class="cm">*/</span>
</span><span class="line"><span class="kr">class</span> <span class="nx">PersonTemplate</span> <span class="kr">extends</span> <span class="nx">TypeTemplate</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">    <span class="nx">get</span> <span class="nx">type</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="s1">&#39;http://example.com/vocab#Person&#39;</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="nx">get</span> <span class="nx">context</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="p">{</span> <span class="s1">&#39;@vocab&#39;</span><span class="o">:</span> <span class="s1">&#39;http://example.com/vocab#&#39;</span> <span class="p">};</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="nx">get</span> <span class="nx">template</span><span class="p">(</span><span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">html</span><span class="err">`</span><span class="o">&lt;</span><span class="nx">h2</span><span class="o">&gt;</span>
</span><span class="line">                        <span class="nx">$</span><span class="p">{</span><span class="nx">person</span><span class="p">.</span><span class="nx">name</span><span class="p">}</span>
</span><span class="line">                    <span class="o">&lt;</span><span class="err">/h2&gt;</span>
</span><span class="line">                    <span class="o">&lt;</span><span class="nx">div</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;details&quot;</span><span class="o">&gt;</span>
</span><span class="line">                        <span class="o">&lt;</span><span class="nx">a</span> <span class="nx">href</span><span class="o">=</span><span class="s2">&quot;${person.website}&quot;</span><span class="o">&gt;</span><span class="nx">My</span> <span class="nx">website</span><span class="o">&lt;</span><span class="err">/a&gt;</span>
</span><span class="line">                    <span class="o">&lt;</span><span class="err">/div&gt;</span>
</span><span class="line">                    <span class="o">&lt;</span><span class="nx">div</span> <span class="kr">class</span><span class="o">=</span><span class="s2">&quot;avatar&quot;</span><span class="o">&gt;</span>
</span><span class="line">                        <span class="nx">$</span><span class="p">{</span><span class="nx">render</span><span class="p">(</span><span class="nx">person</span><span class="p">.</span><span class="nx">avatar</span><span class="p">)}</span>
</span><span class="line">                    <span class="o">&lt;</span><span class="err">/div&gt;`;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="cm">/**</span>
</span><span class="line"><span class="cm">* Will render http://schema.org/ImageObject </span>
</span><span class="line"><span class="cm">*/</span>
</span><span class="line"><span class="kr">class</span> <span class="nx">SchemaImageTemplate</span> <span class="kr">extends</span> <span class="nx">TypeTemplate</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="nx">TemplateRepository</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">PersonTemplate</span><span class="p">);</span>
</span><span class="line"><span class="nx">TemplateRepository</span><span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="nx">SchemaImageTemplate</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>

<p>The <code>render</code> imported above would select a template from the repository and insert it into the parent template. No need
for nesting <code>&lt;object-view&gt;</code> elements. That is of course if I figure out how to observe changes ;)</p>

<h2 id="bottom-line">Bottom line</h2>

<p>I really do like what’s coming with Polymer 3.0. It will embrace ES6 modules, finally. It may be that Polymer will become
more similar to Vue or React but it will still be closest to the Web Platform.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Towards server-side routing with URI Templates (RFC 6570)]]></title>
    <link href="http://t-code.pl/blog/2016/11/Towards-server-side-routing-with-URI-Templates/"/>
    <updated>2016-11-13T23:15:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/11/Towards-server-side-routing-with-URI-Templates</id>
    <content type="html"><![CDATA[<p>There are many MVC frameworks out there and all of them share a common feature - routing. Most libraries use a form of
URI patterns to match incoming requests. On top of powerful features like limiting allowed values to certain types or
by using regular expressions, they all share a common flaw - great simplification of the <a href="https://tools.ietf.org/html/rfc3986">URI</a>.</p>

<p>There is however a similar <a href="https://en.wikipedia.org/wiki/Internet_Standard#Proposed_Standard">proposed standard</a> described by <a href="https://tools.ietf.org/html/rfc6570">RFC 6570</a> and appropriately called <strong>URI Template</strong>
As the name implies, it defines URI patterns which can be then expanded to actual URIs by substituting variables and work
the opposite way by extracting variables from a given URI. This makes it a viable option for matching request URIs on the
server to determine what code to execute, if any.</p>

<!--more-->

<h2 id="so-what-is-wrong-with-typical-routing">So what is wrong with typical routing?</h2>

<p>In most libraries I have seen routing is declared simply by defining variables as URI segments:</p>

<ul>
  <li><a href="https://docs.djangoproject.com/en/1.10/topics/http/urls/">Django</a> is all about regex:
    <ul>
      <li><code>r'^blog/page(?P&lt;num&gt;[0-9]+)/$'</code></li>
    </ul>
  </li>
  <li><a href="https://github.com/NancyFx/Nancy/wiki/Defining-routes">Nancy</a>, <a href="https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api">ASP.NET Web API</a>, <a href="https://github.com/ServiceStack/ServiceStack/wiki/Routing">Service Stack</a> or <a href="http://www.baeldung.com/spring-requestmapping">Spring</a> support a form of simplified URI Templates but extended with optional constraints:
    <ul>
      <li><code>/content/{Version*}/literal/{Slug*}</code> - greedy segments (All)</li>
      <li><code>/ex/bars/{numericId:[\\d]+}</code> - number-only segment (Spring)</li>
      <li><code>users/{id:int}</code> number-only segment (Nancy, Web API)</li>
    </ul>
  </li>
  <li><a href="https://github.com/NancyFx/Nancy/wiki/Defining-routes">Nancy</a> also does regular expressions:
    <ul>
      <li><code>/(?&lt;age&gt;[\d]{1,2})</code></li>
    </ul>
  </li>
  <li><a href="https://metacpan.org/pod/Dancer2::Manual#Route-Handlers">Dancer</a> and <a href="http://expressjs.com/en/guide/routing.html">Express</a> use a similar syntax and also support regular expressions:
    <ul>
      <li><code>/users/:userId/books/:bookId</code> (Both)</li>
      <li><code>/team/:team/**</code> (Dancer)</li>
      <li><code>/ab(cd)?e</code> (Express)</li>
    </ul>
  </li>
</ul>

<p>The features to constrain segments to specific values using regular expression or some custom feature certainly is a
powerful one but there is much more to URL than just segments (not to mention URI in general). The general syntax of an
URL is as follows:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class=""><span class="line">scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]</span></code></pre></td></tr></table></div></figure>

<p>In a web application the scheme will usually be HTTP(S). Let’s also ignore the user/password and host/port which aren’t
usually the concern of a REST server accepting requests. That leaves us with the <a href="https://tools.ietf.org/html/rfc3986#section-3.3">path</a>, <a href="https://tools.ietf.org/html/rfc3986#section-3.4">query</a> 
and <a href="https://tools.ietf.org/html/rfc3986#section-3.5">fragment</a>.</p>

<blockquote>
  <p>Do you notice already how typical routing completely neglects query and fragment?</p>
</blockquote>

<p>In my opinion they should be part of it. Why, you ask? Read on!</p>

<h2 id="using-identifiers-like-you-probably-should">Using identifiers like you probably should</h2>

<h3 id="query-string-is-part-of-the-identifier">Query string is part of the identifier</h3>

<p>In a RESTful API the identifier is the complete URL. If the client does a request like</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">GET /user?id=123 HTTP/1.1 </span>
</span></code></pre></td></tr></table></div></figure>

<p>Why shouldn’t it be possible to include the query string parameter as part of the route? It is after all an integral part
of the identifier. Yet all libraries that I’ve worked with require manual work to extract the value of <code>id</code> in user code.</p>

<h3 id="segments-are-so-much-more-powerful">Segments are so much more powerful</h3>

<p>The URI path is a series of zero or more segments delimited by the slash <code>/</code> character. And so an absolute path like
<code>/users/tomasz/articles/uri-template</code> has four segments:</p>

<ul>
  <li>users</li>
  <li>tomasz</li>
  <li>articles</li>
  <li>uri-template</li>
</ul>

<p>But segments are not necessarily just text. And they certainly don’t have to represent hierarchy of file system folders.</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">A friendly <a href="https://twitter.com/hashtag/RESTful?src=hash">#RESTful</a> reminder - URL path does not represent file-system hierarchy. Heck, it doesn&#39;t have to be a hierarchy at all <a href="https://twitter.com/hashtag/hypermedia?src=hash">#hypermedia</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/797895110306369536">November 13, 2016</a></blockquote>
<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

<p>A little known feature, which I’ve only just discovered very recently, are parametrized path segments. They work similarly
to query strings:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">/segment1;param1=val1;param2=val2,val3/segment2</span>
</span></code></pre></td></tr></table></div></figure>

<p>See how <code>segment1</code> has extra bits attached. As JSON these parameters would be represented as</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;param1&quot;</span><span class="p">:</span> <span class="s2">&quot;val1&quot;</span><span class="p">,</span>
</span><span class="line">  <span class="nt">&quot;param2&quot;</span><span class="p">:</span> <span class="p">[</span>
</span><span class="line">    <span class="s2">&quot;val2&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="s2">&quot;val3&quot;</span>
</span><span class="line">  <span class="p">]</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>And here’s a practical example. A hypothetical API could serve a resource representing a collection of, say, books:
<code>http://example.rest/books</code>. Another resource could be used to retrieve covers of those books: <code>http://example.rest/books/gallery</code>.
Usually any manipulation of such resource is handed over ot query strings.</p>

<p>So, what if the resource owner wanted to offer a gallery of books cover but only books by Oscar Wilde and only covers in
PNG format. Because why not?</p>

<p>The URL would probably look like <code>http://example.rest/books/gallery?author=Oscar%20Wilde&amp;format=png</code>. Do you also see the
<a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">SRP</a> rule being validated? Why not instead use the URL like</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">http://example.rest/books;author=Oscar%20Wilde/gallery;format=png</span>
</span></code></pre></td></tr></table></div></figure>

<p>And hey! Now it’s possible to just drop the last segment and leave</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">http://example.rest/books;author=Oscar%20Wilde</span>
</span></code></pre></td></tr></table></div></figure>

<p>That looks like a resource containing books by Oscar Wilde. Despite the tweet quoted above, people do love URL hierarchies
right? They probably <em>are</em> quite useful in the end.</p>

<p>And it’s not just about <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility</a> of path segments (there probably isn’t such a thing). I don’t have
empirical proof, but I would guess that parametrizing segments could lead to better controller/module/handler design.</p>

<h2 id="so-how-do-i-implement-that">So how do I implement that?</h2>

<p>Most languages probably have a library out there that implements the URI Template standard. So just go ahead a try to
replace the routing in your favourite Web framework so that it works with RFC6570 instead. The books covers resource route
shown above would become:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">/books{;author}/gallery{;format}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Isn’t that nice? No query strings lurking in the implementation. Everything clear up front.</p>

<h3 id="my-nancy-experiment">My Nancy experiment</h3>

<p>I did precisely that and created a proof of concept in <a href="https://github.com/NancyFx/Nancy">Nancy</a>. It’s available on GitHub under 
<a href="https://github.com/tpluscode/nancy.routing.uritemplates">https://github.com/tpluscode/nancy.routing.uritemplates</a> and
hopefully I will make it into a useful library (there are still some issues to sort out).</p>

<p>In the current shape the usage is akin to implementing a typical Nancy application. Instead of <code>NancyModule</code> you implement
<code>UriTemplateModule</code> and replace the RouteResolver with <code>UriTemplateRouteResolver</code>. The actual route definition is just the
same:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="c#"><span class="line"><span class="k">public</span> <span class="k">class</span> <span class="nc">TestModule</span> <span class="p">:</span> <span class="n">UriTemplateModule</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="k">public</span> <span class="nf">TestModule</span><span class="p">()</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="s">&quot;/books&quot;</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">Get</span><span class="p">(</span><span class="s">&quot;{;author}/gallery{;format}&quot;</span><span class="p">,</span> <span class="n">GetCoverGallery</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Such route will match <code>/books/gallery</code>, <code>/books;author=Shakespeare/gallery</code> and <code>/books/gallery;format=square</code>.</p>

<h2 id="possible-issues">Possible issues?</h2>

<p>Have you made that far? Great. Now it’s time to learn about the thorns that this rose has.</p>

<h3 id="route-variable-constrains">Route variable constrains</h3>

<p>In the beginning of this post I show how routing in existing libraries allow constraining captured variables using
custom syntax or regular expressions. The URI Templates specification has no such notion neither any extensibility mechanism.</p>

<p>This is not a great deal as far as I’m concerned. It should be possible to replicate the constraining functionality of
Nancy or Spring by extending variable template expressions. The standard does reserve some characters for future use and
they could serve as a separator between the variable name and the constraining expression. For example, to only allow
the page variable of a collection to be an integer the template could be</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="http"><span class="line"><span class="err">/collection{/page|int}</span>
</span></code></pre></td></tr></table></div></figure>

<p>The pipe character is reserved and as such cannot be a legal part of the expression. As a consequence, a web framework
could reliably separate the <code>page</code> variable from the <code>int</code> constrain.</p>

<h3 id="route-prioritization">Route prioritization</h3>

<p>This is something I haven’t figured out yet. Nancy, and likely other libraries have the ability to work with multiple
routes matching a given URL. For example routes <code>/page/{title}/{sub}</code> and <code>/page/about/{sub}</code> would both match a
request for <code>/page/about/cats</code>. But because the <code>about</code> segment is an exact literal match in the latter route, that route
would actually be executed. It’s potentially a deal-breaker and I’m eager to find a solution to that problem.</p>

<h2 id="closing-words">Closing words</h2>

<p>I’m looking forward to your comments here or at the <a href="https://github.com/tpluscode/nancy.routing.uritemplates">Nancy.Routing.UriTemplates</a>
repository.</p>

<p>I’m quite convinced that URI Templates should have been used for routing from get-go. To me it seems quite obvious now
that hypermedia is becoming more and more recognized as an important design pattern and URI Templates are an important
part of driving the clients between application states. 
Being able to reuse the same technology on both client and server side should be very beneficial to visibility and maintainability
of hypermedia-driven APIs.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Problem Details for HTTP APIs and Nancy]]></title>
    <link href="http://t-code.pl/blog/2016/11/RFC-7807-and-Nancy/"/>
    <updated>2016-11-11T10:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/11/RFC-7807-and-Nancy</id>
    <content type="html"><![CDATA[<p>There are a number of guidelines for returning errors from a REST API in a consistent way. Of course, API authors should
<a href="http://www.micheltriana.com/blog/2013/09/27/http-status-codes-in-rest-web-api">use status codes correctly</a> to signify
the result of an action. It is also a good idea to return some error details in resource representation. Some API vendors,
like Facebook or Twitter <a href="https://apigee.com/about/blog/technology/restful-api-design-what-about-errors">have come up with their own solutions</a>.</p>

<p>However, there is a Internet Engineering Task Force (IETF) proposed standard called <strong>Problem Details for HTTP APIs</strong>. It
was created be Mark Nottingham and is described in document <a href="https://tools.ietf.org/html/rfc7807">RFC 7807</a>. Interestingly
though there is fairly little support in for .NET Web API libraries and none for Nancy 1.x.</p>

<!--more-->

<p>Actually, I found precisely three NuGet packages: one for <a href="https://www.nuget.org/packages/HttpProblemDetails.Nancy">Nancy 2.0</a>,
one for <a href="https://www.nuget.org/packages/HttpProblemDetails.Nancy">ASP.NET Core</a> and one for <a href="https://www.nuget.org/packages/WebApiProblem/">ASP.NET Web API</a>.</p>

<p>Unfortunately there wasn’t one for the stable branch of Nancy, currently at 1.4.3.</p>

<h2 id="introducing-nancyproblemdetails">Introducing Nancy.ProblemDetails</h2>

<p>And so, I have created my own package I called <a href="https://www.nuget.org/packages/Nancy.ProblemDetails"><strong>Nancy.ProblemDetails</strong></a>.
It adds support for serializing JSON Problem Details and uses the library Tavis.Problem by Darrel Miller.</p>

<p>Do have a look at the repository for usage examples: <a href="https://github.com/tpluscode/Nancy.ProblemDetails">https://github.com/tpluscode/Nancy.ProblemDetails</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Publishing Polymer elements written in TypeScript (with dependencies)]]></title>
    <link href="http://t-code.pl/blog/2016/08/publishing-polymerts-elements/"/>
    <updated>2016-08-15T10:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/08/publishing-polymerts-elements</id>
    <content type="html"><![CDATA[<p>I love consuming <a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements">custom elements</a> but writing them in <a href="http://polymer-project.org">Polymer</a> with ES5 is far from ideal. ES6 (<a href="http://benmccormick.org/2015/09/14/es5-es6-es2016-es-next-whats-going-on-with-javascript-versioning/">or
more correctly ES2015</a>) could offer some improvement but it is still not officially supported by the Polymer
team and their toolset.</p>

<p>Thankfully, there is <a href="https://github.com/nippur72/PolymerTS">PolymerTS</a> which offers a vastly improved Polymer API, mainly thanks to decorators. It 
also let’s developers take advantage of ES6 modules but there is one problem: how do you publish elements with dependencies
both on JSPM packages and other elements from Bower?</p>

<!--more-->

<h2 id="tldr">TL;DR;</h2>

<p>Here are some highlights from this post:</p>

<ol>
  <li>Don’t reference bower dependencies directly to avoid vulcanizing <code>polymer.html</code>
  * reference them in package manager-specific entrypoint instead</li>
  <li>Use <code>jspm build-sfx</code> to publish for Bower</li>
  <li>Use <code>jspm bundle</code> to publish for JSPM</li>
</ol>

<h3 id="show-me-the-code-already">Show me the code already</h3>

<p>I’ve created two example repositories:</p>

<ol>
  <li><a href="https://github.com/tpluscode/md-ed"><code>md-ed</code> - a component written in PolymerTS</a></li>
  <li>its sample usage with <a href="https://github.com/tpluscode/md-ed-sample/tree/bower">Bower</a> and <a href="https://github.com/tpluscode/md-ed-sample/tree/jspm">JSPM</a></li>
</ol>

<h2 id="repo-setup">Repo setup</h2>

<p>Inspired by the <a href="http://blog.charto.net/typescript/Taming-Polymer-with-SystemJS-and-TypeScript-part-1/">Taming Polymer post</a> by Juha Järvi, the initial setup involves preparing JSPM, SystemJS and 
TypeScript. The original post however, discusses creating apps. Here I will show how to create, publish and consume a
reusable element.</p>

<p><strong>First</strong>, bootstrap JSPM by running <code>jspm init</code>. All question can be left with default answers except choosing TypeScript 
as the transpiler.</p>

<p><strong>Second</strong>, instruct SystemJS to assume <code>ts</code> as the default extension when loading your code. I usually place it in the 
<code>src</code> folder and so update <code>config.js</code> file accordingly by adding the <code>packages</code> property for the sources folder.</p>

<figure class="code"><figcaption><span>config.js</span><a href="https://github.com/tpluscode/md-ed/blob/master/config.js#L10">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="nx">System</span><span class="p">.</span><span class="nx">config</span><span class="p">({</span>
</span><span class="line">
</span><span class="line">  <span class="nx">packages</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">    <span class="s2">&quot;src&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">      <span class="s2">&quot;defaultExtension&quot;</span><span class="o">:</span> <span class="s2">&quot;ts&quot;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>

<p><strong>Lastly</strong>, you will need PolymerTS itself and SystemJS plugin for loading HTML files using the ES6 <code>import</code> syntax. They
are installed by running:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="sh"><span class="line">bower init
</span><span class="line">bower i nippur72/PolymerTS --save
</span><span class="line">jspm i <span class="nv">html</span><span class="o">=</span>github:Hypercubed/systemjs-plugin-html
</span></code></pre></td></tr></table></div></figure>

<p>Note that unlike Juha Järvi, I install systemjs-plugin-html from jspm and not bower. It is also crucial that you explicitly
set the name for the plugin by installing with <code>html=</code> prefix. Otherwise bundling, which I explain later in this post, will
not work.</p>

<h2 id="creating-elements">Creating elements</h2>

<h3 id="internal-dependencies-and-html-templates">Internal dependencies and HTML templates</h3>

<p>Because I’m using SystemJS with a transpiler, each element will be split into separate html and ts files. The HTML will 
contain the <code>&lt;dom-module&gt;</code> element but no script. Instead, each of the elements’ code will import the template using the
import syntax via the systemjs-plugin-html plugin. Note the <code>.html!</code> suffix. This is the outline of my <code>&lt;md-ed&gt;</code> element.</p>

<figure class="code"><figcaption><span>md-ed.ts</span><a href="https://github.com/tpluscode/md-ed/blob/master/src/md-ed.ts">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="ts"><span class="line"><span class="kr">import</span> <span class="s1">&#39;./md-ed.html!&#39;</span>
</span><span class="line"><span class="kr">import</span> <span class="p">{</span><span class="nx">DefaultMdBehavior</span><span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;DefaultMdBehavior&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="err">@</span><span class="nx">component</span><span class="p">(</span><span class="s1">&#39;md-ed&#39;</span><span class="p">)</span>
</span><span class="line"><span class="err">@</span><span class="nx">behavior</span><span class="p">(</span><span class="nx">DefaultMdBehavior</span><span class="p">)</span>
</span><span class="line"><span class="kr">class</span> <span class="nx">MdEd</span> <span class="kr">extends</span> <span class="nx">polymer</span><span class="p">.</span><span class="nx">Base</span> <span class="p">{</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="nx">MdEd</span><span class="p">.</span><span class="nx">register</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>

<p>Similarly, any shared module or other local elements can be referenced using modules. Above you can see the second line
which imports a behavior.</p>

<h3 id="external-library-dependencies">External library dependencies</h3>

<p>With the help of JSPM and SystemJS, your elements written in TypeScript (or ES6 I imagine) can reference virtually any
external library. They can be packaged as AMD or CommonJS modules or as globals. JSPM unifies the module definitions so
that most libraries simply work in the browser.</p>

<p>The example component uses the <a href="https://github.com/chjj/marked">marked</a> library to parse markdown. It is an npm module
which I install with JSPM as usual.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="sh"><span class="line">jspm i npm:marked
</span></code></pre></td></tr></table></div></figure>

<p>Now, it’s possible to import the library and use its functionality in the custom element:</p>

<figure class="code"><figcaption><span>md-ed.ts</span><a href="https://github.com/tpluscode/md-ed/blob/master/src/md-ed.ts">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="s1">&#39;marked&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kr">class</span> <span class="nx">MdEd</span> <span class="kr">extends</span> <span class="nx">polymer</span><span class="p">.</span><span class="nx">Base</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">    <span class="err">@</span><span class="nx">property</span><span class="p">({</span> <span class="nx">notify</span><span class="o">:</span> <span class="kc">true</span> <span class="p">})</span>
</span><span class="line">    <span class="nx">markdown</span><span class="o">:</span><span class="nb">String</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">    <span class="err">@</span><span class="nx">observe</span><span class="p">(</span><span class="s1">&#39;markdown&#39;</span><span class="p">)</span>
</span><span class="line">    <span class="nx">_markdownChanged</span><span class="p">(</span><span class="nx">md</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kd">var</span> <span class="nx">html</span> <span class="o">=</span> <span class="nx">marked</span><span class="p">(</span><span class="nx">md</span><span class="p">);</span>
</span><span class="line">        <span class="c1">// do something with parsed markdown</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<h3 id="external-web-component-dependencies">External web component dependencies</h3>

<p>Most web components are currently installed with bower. This is true for Google’s elements from <a href="https://elements.polymer-project.org">elements.polymer-project.org</a>
and most I’ve seen on <a href="https://customelements.io">customelements.io</a>. Bower is used because it creates a flat directory structure which allows
for predictable import links. Unfortunately, there is no built-in way for importing such dependencies. Also bundling won’t
work for elements which explicitly import polymer.html. There is currently no way to exclude certain imports from the bundle
which causes multiple Polymers. Needless to say, it is bad.</p>

<p>So, if you need to reference a third party component like some Iron or Paper Elements simply install them from bower but
don’t import them in any of your source files. Instead they will all be imported in an entrypoint - separate for Bower and
JSPM.</p>

<h2 id="publishing-for-bower">Publishing for Bower</h2>

<p>Follow the instructions below if you want to publish you element to be consumed from Bower.</p>

<h3 id="bundling">Bundling</h3>

<p>Bundling is done by running the JSPM CLI which has a number of options. For Bower, I’ve found the <code>bundle-sfx</code>
command works best, because it allows creating packages which require neither any specific module loader
nor JSPM/SystemJS. Elements bundled this way will be possible to consume using bower just like any other element.</p>

<p>I usually add the bundling command to NPM scripts:</p>

<figure class="code"><figcaption><span>package.json</span><a href="https://github.com/tpluscode/md-ed/blob/master/package.json#L4">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="s2">&quot;scripts&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">    <span class="s2">&quot;build-bower&quot;</span><span class="o">:</span> <span class="s2">&quot;jspm bundle-sfx src/md-ed - marked dist/bower/build.js --format global --globals \&quot;{&#39;marked&#39;: &#39;marked&#39;}\&quot;&quot;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p><code>src/md-ed - marked dist/build/build.js</code> means that the root <code>src/md-ed.ts</code> file and it’s dependent modules will be bundled 
into <code>dist/bower/build.js</code> but will not include the marked library. The marked library will be added later as a bower
dependency.</p>

<p><code>--format global</code> creates a bundle without any module loaders. This is enough for bower and web components.</p>

<p>Finally, the <code>--globals "{'marked': 'marked'}"</code> switch is required for some excluded modules when bundling. It tells
JSPM what global variable to use when injecting dependencies into your bundled modules.</p>

<p>I’m intentionally not minifying the contents. The consumer will do so when bundling his or her actual application.</p>

<p>Now, running <code>npm run build-bower</code> will create a <code>bower/dist/build.js</code> with transpiled and bundled scripts and <code>bower/dist/build.html</code>
with <a href="https://github.com/Polymer/vulcanize">vulcanized</a> files. Interestingly, the html must exist beforehand, which looks like a bug in the SystemJS html
plugin. Simply create one before running the npm script:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="sh"><span class="line">mkdir dist
</span><span class="line">touch build.html
</span><span class="line">npm run build-bower
</span></code></pre></td></tr></table></div></figure>

<p>Oh, and don’t exclude the <code>dist</code> folder from git. You’ll want to push the bundled files with everything else.</p>

<h3 id="packaging">Packaging</h3>

<p>Most components published with Bower include a html file named same as the repository (and element). My element is called
<code>md-ed</code> and so I created a <code>md-ed.html</code> file in the root of my repository. This will be the main entrypoint for consumers
to import. Here’s the complete file:</p>

<figure class="code"><figcaption><span>md-ed.html</span><a href="https://github.com/tpluscode/md-ed/blob/master/md-ed.html">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="c">&lt;!-- imports of bower dependencies --&gt;</span>
</span><span class="line"><span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;../polymer-ts/polymer-ts.min.html&quot;</span><span class="nt">/&gt;</span>
</span><span class="line"><span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;../paper-input/paper-textarea.html&quot;</span> <span class="nt">/&gt;</span>
</span><span class="line"><span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;../paper-tabs/paper-tabs.html&quot;</span> <span class="nt">/&gt;</span>
</span><span class="line"><span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;../iron-pages/iron-pages.html&quot;</span> <span class="nt">/&gt;</span>
</span><span class="line"><span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;../marked/lib/marked.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span><span class="line">
</span><span class="line"><span class="c">&lt;!-- import of bundled HTML files --&gt;</span>
</span><span class="line"><span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;dist/bower/build.html&quot;</span> <span class="nt">/&gt;</span>
</span><span class="line">
</span><span class="line"><span class="c">&lt;!-- this is required due to a bug in HTML loader for SystemJS --&gt;</span>
</span><span class="line"><span class="nt">&lt;script&gt;</span>
</span><span class="line">    <span class="kd">var</span> <span class="nx">System</span> <span class="o">=</span> <span class="nx">System</span> <span class="o">||</span> <span class="p">{};</span>
</span><span class="line">    <span class="nx">System</span><span class="p">.</span><span class="nx">register</span> <span class="o">=</span> <span class="nx">System</span><span class="p">.</span><span class="nx">register</span> <span class="o">||</span> <span class="kd">function</span><span class="p">(){};</span>
</span><span class="line"><span class="nt">&lt;/script&gt;</span>
</span><span class="line">
</span><span class="line"><span class="c">&lt;!-- referencing the bundled, transpiled code of the element --&gt;</span>
</span><span class="line"><span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;dist/bower/build.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>At the top I added bower dependencies. It’s important that the paths don’t include <code>bower_components</code>. On the consumer 
side, the elements will already live alongside other bower dependencies. I include all component dependencies and marked,
which I excluded from the bundle. Shall you choose not to exclude some dependency, you would then keep it out of your
bower entrypoint.</p>

<p>Below the bundled files are referenced. There is some additional boilerplate here. The extra script is a remedy for another 
shortcoming of the systemjs-plugin-html. It doesn’t play nice with the <code>bundle-sfx</code> command and leaves some references to
SystemJS. This is simply to avoid <code>System is undefined</code> or similar errors.</p>

<p>Finally, you may also want to add the file to you bower.json as <code>"main": "md-ed.html"</code>.</p>

<figure class="code"><figcaption><span>bower.json</span><a href="https://github.com/tpluscode/md-ed/blob/master/bower.json#L4">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;md-ed&quot;</span><span class="p">,</span>
</span><span class="line">  <span class="nt">&quot;main&quot;</span><span class="p">:</span> <span class="s2">&quot;md-ed.html&quot;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<h3 id="consuming">Consuming</h3>

<p>Consuming with Bower is as easy as it gets. Simply install the element:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="sh"><span class="line">bower install --save tpluscode/md-ed
</span></code></pre></td></tr></table></div></figure>

<p>add an import <code>&lt;link&gt;</code> and use the element on you page:</p>

<figure class="code"><figcaption><span>https://github.com/tpluscode/md-ed-sample/blob/bower/index.html </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="cp">&lt;!doctype html&gt;</span>
</span><span class="line"><span class="nt">&lt;html&gt;</span>
</span><span class="line"><span class="nt">&lt;head&gt;</span>
</span><span class="line">    <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;bower_components/webcomponentsjs/webcomponents.min.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span><span class="line">    <span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;import&quot;</span> <span class="na">href=</span><span class="s">&quot;bower_components/md-ed/md-ed.html&quot;</span><span class="nt">/&gt;</span>
</span><span class="line"><span class="nt">&lt;/head&gt;</span>
</span><span class="line"><span class="nt">&lt;body&gt;</span>
</span><span class="line">    <span class="nt">&lt;md-ed&gt;&lt;/md-ed&gt;</span>
</span><span class="line"><span class="nt">&lt;/body&gt;</span>
</span><span class="line"><span class="nt">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<h2 id="publishing-for-jspm">Publishing for JSPM</h2>

<p>Follow the instructions below if you want to publish you element to be consumed from JSPM.</p>

<h3 id="bundling-1">Bundling</h3>

<p>Unfortunately, the same bundling command doesn’t work for both Bower and JSPM. I’ve found that for JSPM it is best to
use the <code>jspm bundle</code> command which produces a similar output but for use exclusively with SystemJS and no other module
loaders. The npm script is similar but simpler than the command used for Bower:</p>

<figure class="code"><figcaption><span>package.json</span><a href="https://github.com/tpluscode/md-ed/blob/master/package.json#L5">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="s2">&quot;scripts&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">    <span class="s2">&quot;build-jspm&quot;</span><span class="o">:</span> <span class="s2">&quot;jspm bundle src/md-ed - marked dist/jspm/bundle.js&quot;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>It produces a similar output - combined scripts in <code>dist/jspm/bundle.js</code> file and vulcanized <code>dist/jspm/bundle.html</code>. Here
the marked library is also excluded from the bundle.</p>

<h3 id="packaging-1">Packaging</h3>

<p>For consumers to be able to use your JSPM package it is also necessary to create a main entrypoint. For that purpose I 
created an <code>md-ed.js</code> file in the root of the repository.</p>

<figure class="code"><figcaption><span>md-ed.js</span><a href="https://github.com/tpluscode/md-ed/blob/master/md-ed.js">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="ts"><span class="line"><span class="kr">import</span> <span class="s2">&quot;bower_components/polymer-ts/polymer-ts.min.html!&quot;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="s2">&quot;bower_components/paper-input/paper-textarea.html!&quot;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="s2">&quot;bower_components/paper-tabs/paper-tabs.html!&quot;</span><span class="p">;</span>
</span><span class="line"><span class="kr">import</span> <span class="s2">&quot;bower_components/iron-pages/iron-pages.html!&quot;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kr">import</span> <span class="s1">&#39;./dist/jspm/bundle.html!&#39;</span>
</span><span class="line"><span class="kr">import</span> <span class="s1">&#39;./dist/jspm/bundle&#39;</span>
</span><span class="line">
</span><span class="line"><span class="nx">System</span><span class="p">.</span><span class="kr">import</span><span class="p">(</span><span class="s1">&#39;src/md-ed.ts&#39;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>

<p>The outline is very similar to Bower’s entrypoint:</p>

<ol>
  <li>Import bower dependencies with HTML plugin</li>
  <li>Import the bundled HTML and scripts</li>
  <li>Load the element from the bundle</li>
</ol>

<p>The last step is necessary because JSPM bundles don’t immediately load any modules. They are just used to combine multiple
modules in one script.</p>

<p>For the element’s package to be installed correctly, the configuration file must include the main file, similarly to that
of bower.</p>

<p>A perceptive reader will also notice that I’m using ES6 module syntax above. SystemJS can handle this just fine provided the
format option is set in <code>package.json</code>. Here’s mine, with both entrypoint script and the format set.</p>

<figure class="code"><figcaption><span>package.json</span><a href="https://github.com/tpluscode/md-ed/blob/master/package.json#L8">link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">  <span class="nt">&quot;jspm&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;main&quot;</span><span class="p">:</span> <span class="s2">&quot;md-ed.js&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;format&quot;</span><span class="p">:</span> <span class="s2">&quot;es6&quot;</span>
</span><span class="line">  <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Publishing a package in ES6 syntax will also enable rescursive bundling of the element’s dependencies. Otherwise JSPM
would not be able to bundle direct usages of <code>System.import</code>. In other words some dependencies would remain unbundled.</p>

<h3 id="consuming-1">Consuming</h3>

<p>Consumers, in order to us the element, must install it using JSPM but also install the necessary bower packages. The
easiest seems to be installing the same element from both JSPM <strong>and</strong> bower. This way, albeit cumbersome when updating,
will ensure that all necessary dependencies are pulled as well. To install the sample element one would eun the two 
commands</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="sh"><span class="line">bower i tpluscode/md-ed --save
</span><span class="line">jspm i github:tpluscode/md-ed
</span></code></pre></td></tr></table></div></figure>

<p>Typically there would be single application module, like <a href="https://github.com/tpluscode/md-ed-sample/blob/jspm/app.js"><code>app.js</code></a>, which references all it’s dependencies. For our jspm
component the import would be a simple <code>import 'tpluscode/md-ed'</code></p>

<p>At runtime, it will pull all necessary files from bower and jspm components. The main <code>index.html</code> file will then reference
the <code>app.js</code> script and uses SystemJS to load the add.</p>

<figure class="code"><figcaption><span>https://github.com/tpluscode/md-ed-sample/blob/jspm/index.html </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="cp">&lt;!doctype html&gt;</span>
</span><span class="line">        <span class="nt">&lt;html&gt;</span>
</span><span class="line"><span class="nt">&lt;head&gt;</span>
</span><span class="line">    <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;jspm_packages/system.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span><span class="line">    <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;config.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span><span class="line">    <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&quot;bower_components/webcomponentsjs/webcomponents.min.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span>
</span><span class="line"><span class="nt">&lt;/head&gt;</span>
</span><span class="line"><span class="nt">&lt;body&gt;</span>
</span><span class="line">
</span><span class="line"><span class="nt">&lt;md-ed&gt;&lt;/md-ed&gt;</span>
</span><span class="line">
</span><span class="line"><span class="nt">&lt;script&gt;</span>
</span><span class="line">    <span class="nx">System</span><span class="p">.</span><span class="kr">import</span><span class="p">(</span><span class="s1">&#39;app&#39;</span><span class="p">);</span>
</span><span class="line"><span class="nt">&lt;/script&gt;</span>
</span><span class="line"><span class="nt">&lt;/body&gt;</span>
</span><span class="line"><span class="nt">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<h2 id="conclusion">Conclusion</h2>

<p>I realize that the presented ideas are far from ideal. The web stack is not yet consistent enough, with its multiple
package managers etc, to support the modern ideas around web components. Until it matures I hope that someone out there
will find my ideas helpful.</p>

<p>And please, if you think my bundling routine can be simplified, do leave me a note in the comments.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Lest I forget&#58; International Conference on Web Engineering - Day 2 Research Tracks]]></title>
    <link href="http://t-code.pl/blog/2016/06/ICWE-2016-day-2/"/>
    <updated>2016-06-19T22:45:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/06/ICWE-2016-day-2</id>
    <content type="html"><![CDATA[<p>Earlier this week I shortly wrote about the first day Research Track at <a href="http://icwe2016.inf.usi.ch">International Conference on Web Engineering</a> 
which took place between June 6-7. Today it’s time for day 2 (keynote included).</p>

<!--more-->

<h2 id="day-2">Day 2</h2>

<p>The second day ended with a lovely dinner in the restaurant and lookout point atop <a href="https://en.wikipedia.org/wiki/Monte_Br%C3%A8">Monte Brè</a>. Although the weather
was a little flaky, everyone got a chance for some pictures of Lugano from above. Here’s my panorama shot of Lugano:</p>

<p><a href="http://t-code.pl/uploads/2016/06/lugano_pano_big.jpg"><img src="http://t-code.pl/uploads/2016/06/lugano_pano.jpg" alt="Lugano panorama" /></a></p>

<p>Other than that, unsurprisingly, the conference research track featured a whole lot of interesting lectures, ideas and
showcases.</p>

<h3 id="the-keynote---microservices---the-hunting-of-the-snark">The keynote - Microservices - The Hunting of the Snark</h3>

<p><a href="https://www.thoughtworks.com/profiles/james-lewis">James Lewis of ThoughtWorks</a> (yes, the one famous for Martin Fowler)
gave and extensive and wildly interesting talk about the state of Microservices, organizational challenges for companies
which want to try and also consultancy work at banks. As we all know, a picture is worth a thousand words, so here are
some tweet highlights from the keynote</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">&quot;how big are they&quot; doesn&#39;t matter for <a href="https://twitter.com/hashtag/Microservices?src=hash">#Microservices</a>; what matters is how many you can handle. do you have a plan? <a href="https://twitter.com/boicy">@boicy</a> at <a href="https://twitter.com/hashtag/icwe2016?src=hash">#icwe2016</a></p>&mdash; Erik Wilde (@dret) <a href="https://twitter.com/dret/status/740090382013374465">June 7, 2016</a></blockquote>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">And areas necessary for an organization to excel at before going for <a href="https://twitter.com/hashtag/microservices?src=hash">#microservices</a> <a href="https://twitter.com/hashtag/ICWE2016?src=hash">#ICWE2016</a>  <a href="https://t.co/g8hr42yctQ">https://t.co/g8hr42yctQ</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/740093263890223104">June 7, 2016</a></blockquote>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">&#39;The death of the integration env&#39; predicted by <a href="https://twitter.com/boicy">@boicy</a>. It&#39;s not possible for 100s of <a href="https://twitter.com/hashtag/Microservices?src=hash">#Microservices</a> <a href="https://twitter.com/hashtag/icwe2016?src=hash">#icwe2016</a> <a href="https://t.co/kgzPEpqa60">pic.twitter.com/kgzPEpqa60</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/740092582609494016">June 7, 2016</a></blockquote>

<h3 id="rest-apis-a-large-scale-analysis-of-compliance-with-principles-and-best-practices">REST APIs: A Large-Scale Analysis of Compliance with Principles and Best Practices</h3>

<p>Unfortunately, this one I didn’t enjoy as much. <a href="https://twitter.com/floriandanielit">Florian Daniel</a> showed their results
of a large scale analysis of mobile data usage around Milan. The team gathered 78 GB worth of data and calculated a number
of metrics like HTTP method usage, media types, user agents and more. An intriguing idea, but without interaction context
it is really hard to come to any conclusions about the “quality” of any single endpoint from REST perspective. Not to mention
that the dataset likely misses any encrypted traffic, which I would expect to be a significant portion. If anything, the
work is an analysis of HTTP protocol usage on the mobile web, but not really REST at all. This is quite evident when you
look closely at some of the metrics, most of which revolve around the URI (eg. presence of underscores, <code>api</code> in path, 
presence of a trailing slash, version in URI). <a href="../../02/rest-misconceptions-1/">I find it very harmful to consider anything specific about URIs a REST
principle and best practice</a>.</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">URI practices are so unnecessary. Come to my <a href="https://twitter.com/wsREST">@wsREST</a> talk on Thursday at 12 to hear more <a href="https://twitter.com/floriandanielit">@floriandanielit</a> <a href="https://twitter.com/hashtag/ICWE2016?src=hash">#ICWE2016</a> <a href="https://t.co/bW1tNi14gy">pic.twitter.com/bW1tNi14gy</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/740111148465422336">June 7, 2016</a></blockquote>

<h3 id="benchmarking-web-api-quality">Benchmarking Web API Quality</h3>

<p>A similar presentation followed, in which <a href="https://twitter.com/erikwittern">Erik Wittern</a> presented the results of a daunting benchmarking of a
some popular public APIs including Twitter and Flickr, but also less popular services like police.co.uk. It was very 
entertaining to see how wildly some APIs differ when requested from different regions and at various time of day.</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">.<a href="https://twitter.com/erikwittern">@erikwittern</a> is painting a dire picture of Web API availability <a href="https://twitter.com/hashtag/icwe2016?src=hash">#icwe2016</a> <a href="https://t.co/1UNoTonVWO">pic.twitter.com/1UNoTonVWO</a></p>&mdash; Philipp Leitner (@xLeitix) <a href="https://twitter.com/xLeitix/status/740120861550739456">June 7, 2016</a></blockquote>

<p>Of course one could argue that such comparison is a bit unfair. For example the aforementioned police.co.uk is likely not
intended to work smoothly from Australia but on the other hand it may of interest to someone anywhere on the globe occasionally.
The authors are aware of limitation to their method but rightly point out that it is very important to plan for availability
when designing a public API. Especially that every single tested service showed occasional peaks in latencies and even 
outages.</p>

<h3 id="mira-a-model-driven-framework-for-semantic-interfaces-for-web-applications">MIRA: A Model-Driven Framework for Semantic Interfaces for Web Applications</h3>

<p>Next presentation that really got my attention was <a href="https://twitter.com/SchwabeD">Daniel Schwabe’s</a> presentation of <a href="https://github.com/TecWebLab/mira">MIRA</a>, a JavaScript tool
and RDF notation for modelling user interfaces with abstract elements, so that they can be replaced by concrete elements
on a given platform. This method addresses the volatility of the web and the shortcoming of responsive design, which will
soon be unable to cater for futuristic User Interfaces.</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">These are four reasons why responsive web design isn&#39;t enough. Talk by <a href="https://twitter.com/SchwabeD">@schwabed</a> <a href="https://twitter.com/hashtag/ICWE2016?src=hash">#ICWE2016</a> <a href="https://t.co/XuJIGz3lXt">pic.twitter.com/XuJIGz3lXt</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/740152809941827584">June 7, 2016</a></blockquote>

<p>MIRA authors also boast their results which show that for seasoned programmers and non-developers alike it took the least
time to build a simple application using their tool. However we’ve seen that already, which makes me skeptical (I’m looking
at you Lightswitch!).</p>

<p>There is a demo available at <a href="http://mira.tecweb.inf.puc-rio.br/">http://mira.tecweb.inf.puc-rio.br/</a>.</p>

<h3 id="volatile-functionality-in-action-methods-techniques-and-assessmenthttpicwe2016infusichsitesicwe2016filesslidesdarian-frajbergpptx"><a href="http://icwe2016.inf.usi.ch/sites/icwe2016/files/slides/Darian-Frajberg.pptx">Volatile Functionality in Action: Methods, Techniques and Assessment</a></h3>

<p>This was another slight disappointment of that day. Darian Frajberg presented their evaluation of using various techniques
to manage volatile functionalities of an application. An example is a time-limited prices reduction on certain item on an
e-commerce platform. The problem there is that programming tools such as code weaving and regular expressions, which were
the basis for their platform are very demanding on the programmers. Even when packaged as a reusable library, such a library
would take a great effort to support and evolve. Also, the authors didn’t really compare their results to real methods
used in practice. The baseline for their evaluation included naïve and unrealistice methods, which would not be used in
serious software.</p>

<h3 id="abstracting-and-structuring-web-contents-for-supporting-personal-web-experiencehttpicwe2016infusichsitesicwe2016filesslidesgabriela-bosettiwoapdf"><a href="http://icwe2016.inf.usi.ch/sites/icwe2016/files/slides/Gabriela-Bosetti_WOA.pdf">Abstracting and Structuring Web contents for supporting Personal Web Experience</a></h3>

<p>This very unassuming and slightly misleading title hides an exciting piece of software called <a href="https://sites.google.com/site/webobjectambient">Web Objects Ambient</a>.
It is a Firefox extension (with a Chrome version a possibility) which allows end users to enhance web content by interacting
with elements and combining it with more data like tweets, reviews, etc. And it uses DBpedia as the source of concepts <img class="emoji" title=":+1:" alt=":+1:" src="https://assets.github.com/images/icons/emoji/unicode/1f44d.png" height="20" width="20" align="absmiddle" />.
In case I somehow failed to convey the general idea, here’s a video showing WOA in action.</p>

<iframe width="640" height="360" src="https://www.youtube.com/embed/kZyHZpU8ue8" frameborder="0" allowfullscreen=""></iframe>

<p>Finally, there is also an <a href="https://sites.google.com/site/webobjectambient/documentation">extensibility model</a>, which allows
developers to extend the sidebar seen above with more powers!</p>

<h3 id="the-direwolf-inside-you-end-user-development-for-heterogeneous-web-of-things-applianceshttpwwwslidesharenetistvankorenthe-direwolf-inside-you-end-user-development-for-heterogeneous-web-of-things-appliances"><a href="http://www.slideshare.net/IstvanKoren/the-direwolf-inside-you-end-user-development-for-heterogeneous-web-of-things-appliances">The DireWolf Inside You: End User Development for Heterogeneous Web of Things Appliances</a></h3>

<p>Because RDF Streams are above me I’m skipping right to the last presentation of the day. Awarded the Best Vision Paper Award,
the vision by <a href="https://twitter.com/istinhere">Istvan Koren</a> and <a href="https://twitter.com/klamma">Ralf Klamma</a> ongoing effort to build a distributed End User Interface for
the Web of Things. Build with Polymer and their own peer-to-peer communication library! Again, because a picture will
explain uch better than words, here the slide showing an overview of the authors’ vision.</p>

<p><a href="http://t-code.pl/uploads/2016/06/direwolf_big.png"><img src="http://t-code.pl/uploads/2016/06/direwolf.png" alt="DireWolf overview slide" /></a></p>

<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Lest I forget&#58; International Conference on Web Engineering - Day 1 Research Tracks]]></title>
    <link href="http://t-code.pl/blog/2016/06/ICWE-2016-day-1/"/>
    <updated>2016-06-13T22:45:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/06/ICWE-2016-day-1</id>
    <content type="html"><![CDATA[<p>This past week I took part in the <a href="http://icwe2016.inf.usi.ch">International Conference on Web Engineering</a> or ICWE for
short. I’d like to round up the lectures I attended, demos I watched and posters I saw. While I still remember, I want to
write about my impressions about each individual one, at least at the very shortest. Of course I won’t be able to cover
it all, because I wasn’t able to attend every session for obvious reasons and at the same time I didn’t find everything
equally interesting. I’ll do my best nonetheless.</p>

<!--more-->

<h2 id="day-1">Day 1</h2>

<p>Throughout the three conference days I mostly attended the Research Tracks. They contain a whole lot of knowledge, new ideas
and ingenious tools. Here’s what caught my attention on the first day.</p>

<p><img src="http://t-code.pl/uploads/2016/06/icwe-program.jpg" alt="icwe program cover" /></p>

<h3 id="the-keynote---quo-vadis-google-knowledge-graph">The keynote - Quo vadis Google Knowledge Graph</h3>

<p>The conference started with a <a href="http://icwe2016.inf.usi.ch/program/keynotes/xin.luna.dong">talk</a> by <a href="http://lunadong.com/">Xin Luna Dong</a>
about the Google Knowledge Graph. It was very interesting to see how Google employs a data model similar in concept to
RDF to build it’s massive knowledge base, how Freebase is currently being replaced by <a href="https://en.wikipedia.org/wiki/Knowledge_Vault">Google Knowledge Vault</a> and
learn about (Lightweight) Verticals, which are one how Google collects its data. However, as one listener points out:</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr"><a href="https://twitter.com/hashtag/ICWE2016?src=hash">#ICWE2016</a>! Nice presentation by Luna Dong but... where is Wikidata?</p>&mdash; Alberto Tonon (@otreblanonot) <a href="https://twitter.com/otreblanonot/status/739729514750742529">June 6, 2016</a></blockquote>

<h3 id="ctat-tilt-and-tap-across-devices">CTAT: Tilt-and-Tap Across Devices</h3>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Thank you really much <a href="https://twitter.com/hashtag/icwe2016?src=hash">#icwe2016</a> for this award.Special thanks to <a href="https://twitter.com/mariahusman">@MariaHusman</a> <a href="https://twitter.com/hashtag/Abhi?src=hash">#Abhi</a> <a href="https://twitter.com/hashtag/can?src=hash">#can</a>  and my mentor <a href="https://twitter.com/emseen">@emseen</a>. <a href="https://t.co/q4PI4otfHM">pic.twitter.com/q4PI4otfHM</a></p>&mdash; Lindig (@lindadigeronimo) <a href="https://twitter.com/lindadigeronimo/status/740461345632620544">June 8, 2016</a></blockquote>

<p>This interesting work by <a href="https://twitter.com/lindadigeronimo">Linda Di Geronimo</a>, <a href="https://twitter.com/MariaHusmann">Maria Husmann</a>, Abhimanyu Patel, Can Tuerk and <a href="https://twitter.com/emseen">Moira C. Norrie</a>
was awarded the Best Paper Award for good reason. During the talk we could watch what great interactions can be achieve
by communicating multiple mobile device equipped with accelerometers. That has some proper SCI-FI potential. And just thing
about all the games. Well, unfortunately we’ve heard that lagging would be too great for dynamic real-time interaction, 
but some less intensive activities? After all we all have a universal Wii-like controller in our pockets. It just so happens
that it can take calls.</p>

<video width="870" preload="metadata" controls=""><source src="http://t-code.pl/uploads/2016/06/ctat.mp4" type="video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;" /></video>

<h3 id="revisting-web-data-extraction-using-in-browser-structural-analysis-and-visual-cues-in-modern-web-designs">Revisting Web Data Extraction using In-Browser Structural Analysis and Visual Cues in Modern Web Designs</h3>

<p>In his presentation, <a href="https://twitter.com/alfonsomurolo">Alfonso Murolo</a> showcased his Chrome extension called <strong>DeepDesign</strong>, which uses some 
state-of-the-art techniques to aid extracting (scraping) data from websites. The extension takes advantage of structural
and visual cues to automate the creation of wrappers for extracting data from web pages. I only wish that the extension
was made public and included a crawler so that entire websites can be scraped. Currently it requires manual action.</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">DeepDesign: Chrome Web extractor extension. I was looking for this! Will have to try it out. <a href="https://twitter.com/hashtag/ICWE2016?src=hash">#ICWE2016</a> <a href="https://t.co/8e74pJct1D">pic.twitter.com/8e74pJct1D</a></p>&mdash; Tomasz Pluskiewicz (@tpluscode) <a href="https://twitter.com/tpluscode/status/739754809729929216">June 6, 2016</a></blockquote>

<p>Unfortunately, currently trying DeepDesign won’t be possible.</p>

<h3 id="clustering-aided-page-object-generation-for-web-testing">Clustering-Aided Page Object Generation for Web Testing</h3>

<p>First day, and yet another award - for best student paper. In this presentation the audience was introduced to <a href="http://sepl.dibris.unige.it/APOGEN.php">APOGEN</a> 
or <strong>Automatic Page Objects Generator</strong>. It is a Java tool, which crawls a website to create Page Objects for its pages.
However it does by combining multiple instances of the same page into clusters. It allows correcting these clusters with
a simple UI. That UI even shows small renderings of crawled pages. There is so much usefulness in this tool and it is
already <a href="https://app.assembla.com/spaces/sepl-apo/subversion/source/HEAD/trunk">available as open source</a> (just why SVN?).</p>

<video width="870" preload="metadata" controls="" poster="http://t-code.pl/uploads/2016/06/apogen.png"><source src="http://t-code.pl/uploads/2016/06/apogen.mp4" type="video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;" /></video>

<p>How cool is that? If I ever hear that scientific conferences don’t have anything practical offer to I will slap that
person across their face <img class="emoji" title=":wink:" alt=":wink:" src="https://assets.github.com/images/icons/emoji/unicode/1f609.png" height="20" width="20" align="absmiddle" />.</p>

<h3 id="qwwwq-querying-wikipedia-without-writing-queries">QwwwQ: Querying Wikipedia without writing queries</h3>

<p>This is another cool Chrome extension I’m very excited about, which unfortunately is not available publicly just yet.
QwwwQ (pron. quick) is an ingenious tool for querying <a href="http://dbpedia.org">DBpedia</a> in a way I would describe as a mix of
query-by-example and maybe faceted search. It would allow non-technical users to explore the wealth of data stored on
wikipedia and also help developers build SPARQL queries for DBpedia with a nice GUI instead of a text editor. In their
paper the authors (<a href="https://www.linkedin.com/in/massimiliano-battan-2b49b359">Massimiliano Battan</a> and <a href="http://latemar.science.unitn.it/segue/index.php?&amp;login=logout&amp;action=site&amp;site=ronchet">Marco Ronchetti</a>) mention future plans for allowing <code>JOIN</code>
operations to traverse relations. I would add to that list the possibility to retrieve the underlying query for further
customization.</p>

<p>What I found most interesting is that QwwwQ cites a 1975 paper <em>A psychological study of query by example</em> by John C. Thomas
and John D. Gould. Isn’t that cool?</p>

<h3 id="aspect-based-sentiment-analysis-on-the-web-using-rhetorical-structure-theory">Aspect-Based Sentiment Analysis on the Web using Rhetorical Structure Theory</h3>

<p>Another award that day - Distinguished Paper Award. Although the topic is quite advanced, the presenters succinctly explained
their application of <a href="https://en.wikipedia.org/wiki/Rhetorical_Structure_Theory">Rhetorical Structure Theory or RST</a> in
deriving sentiment from product reviews written in English. What does it mean? It means that by deconstructing and
classifying parts of a review multiple algorithms devised by the authors can determine what positive and negative sentiments
the reviewer expressed. Interestingly, the results are slightly worse for negative reviews (allegedly, because positive 
words are often used to express negative sentiments) and results are better for reviews consisting of multiple sentences.</p>

<h3 id="diversity-in-social-media-urban-analytics">Diversity in Social Media Urban Analytics</h3>

<p>An fairly interesting paper, which shows how social media activity data (from Twitter and Instagram in this case) can be
used to determine patterns of their respective users in cities. The authors analyzed the usage of said social networks in
four European cities: Amsterdam, London, Paris and Rome. Among the findings the authors discovered that</p>

<ul>
  <li>Instagram users concentrate more in the vicinity of tourist locations</li>
  <li>The same can be said for tourists as opposed to residents</li>
  <li>Instagram shows more uniform activity over time in various cities that Twitter (for example Amsterdam Twitter activity dies out much quicker in evenings)</li>
  <li>Both social networks’ activity is proportional to city size</li>
  <li>The activity of people aged 45+ is very low</li>
</ul>

<p>As unsurprising as they may seem, these results are not really meant to reflect the reality in 100%. The authors are aware
of the challenges and acknowledge shortcomings of their techniques. This paper is however a good exploration of various
possibilities which await a data scientist who is interested in analyzing social behaviour on the web.</p>

<h3 id="design-of-cqa-systems-for-flexible-and-scalable-deployment-and-evaluationhttpicwe2016infusichsitesicwe2016filesslidesivan-srbapdf"><a href="http://icwe2016.inf.usi.ch/sites/icwe2016/files/slides/Ivan-Srba.pdf">Design of CQA Systems for Flexible and Scalable Deployment and Evaluation</a></h3>

<p>Another work with so much potential for practical application. Long ago I have been looking for a good, open source
alternative to Stack Exchange. It turns out that there is one such open-source project being developed at Slovak University
of Technology in Bratislava. Its focus is on educational use and has unique features for teachers/students and integrates
with edX and MOOC. It is also used on the University in Lugano.</p>

<p>Oh, and did I mention that it is open source and <a href="https://github.com/AskalotCQA/askalot">hosted on GitHub</a>? Go give it a
try.</p>

<script async="" src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Interesting JavaScript quirk when accessing object with index]]></title>
    <link href="http://t-code.pl/blog/2016/05/indexing-object-with-array/"/>
    <updated>2016-05-15T20:20:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/05/indexing-object-with-array</id>
    <content type="html"><![CDATA[<p>I’ve just discovered an interesting JavaScript quirk which I haven’t seen before. It turns out that indexing an object with a single
element array has the exact same effect as indexing with that array’s element.</p>

<!--more-->

<p>JavaScript just doesn’t stop amazing developers. There are <a href="http://developer.telerik.com/featured/seven-javascript-quirks-i-wish-id-known-about/">many quirks</a> I’ve seen, known and exploited. I was so surprised to hav
found one that I’ve never seen before. It turns out that <a href="http://www.quirksmode.org/js/associative.html">accessing object as an associative array</a> works not only with the actual
index but also when a <strong>single element array</strong> is used for index.</p>

<p>See for yourself below:</p>

<p><a class="jsbin-embed" href="http://jsbin.com/lizeho/embed?js,console">JS Bin on jsbin.com</a></p>

<p>Of course, the array with two elements returns <code>undefined</code> as expected.</p>

<p>What is even more insane, is that the array can be of any depth as long as it is a single element array within a single element array 
within a single element array etc. How weird is that?</p>

<p><a class="jsbin-embed" href="http://jsbin.com/fawihu/embed?js,console">JS Bin on jsbin.com</a></p>

<p>Digging deeper, I discovered that it is possible to index an object with arrays on any dimension and that would be equivalent
as indexing with the elements joined with a comma. Here’s a demonstration</p>

<p><a class="jsbin-embed" href="http://jsbin.com/xokoze/embed?js,console">JS Bin on jsbin.com</a><script src="http://static.jsbin.com/js/embed.min.js?3.35.12"></script></p>

<p>Again, it does not matter whether the array is deeply nested inside other arrays</p>

<p>I imaging this can actually be quite useful though I’d be afraid that programmers not familiar with this trick would not understand
what is happening.</p>

<p>A superfluous google search didn’t answer why this works like that. A comment with an explanation will be greatly appreciated <img class="emoji" title=":smile:" alt=":smile:" src="https://assets.github.com/images/icons/emoji/unicode/1f604.png" height="20" width="20" align="absmiddle" />.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Consuming hypermedia - declarative UI]]></title>
    <link href="http://t-code.pl/blog/2016/04/hypermedia-driven-ui/"/>
    <updated>2016-04-30T22:25:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/04/hypermedia-driven-ui</id>
    <content type="html"><![CDATA[<p>I’ve been going on about hypermedia for a long time now. I’ve touched both client and server side in terms of processing
resource representations. There is however a big missing piece in how developers should build user interfaces. Personally
I have been hooked on the idea of <a href="http://webcomponents.org/">Web Components</a> and I’ve had some success experimenting with using a declarative
way for defining User Interface building blocks.</p>

<!--more-->

<h2 id="declarative-views-for-resource-representations">Declarative views for resource representations</h2>

<p>Assuming the use of RDF (<a href="https://www.w3.org/RDF/">Resource Description Framework</a>), the user interface can be defined by creating a template
for given RDF class or data type. A few examples can include: <img class="emoji" title=":one:" alt=":one:" src="https://assets.github.com/images/icons/emoji/unicode/0031-20e3.png" height="20" width="20" align="absmiddle" /> a dedicated custom element for displaying a person,
<img class="emoji" title=":two:" alt=":two:" src="https://assets.github.com/images/icons/emoji/unicode/0032-20e3.png" height="20" width="20" align="absmiddle" /> a lightbox element for images typed as <code>schema:ImageObject</code> or <img class="emoji" title=":three:" alt=":three:" src="https://assets.github.com/images/icons/emoji/unicode/0033-20e3.png" height="20" width="20" align="absmiddle" /> custom datepicker for <code>xsd:date</code>.</p>

<p>My idea for such syntax is to extend the <code>&lt;template&gt;</code> tag so that whenever it is added to DOM, it somehow registers itself
for use in specific case. Because the template tag itself is quite dumb I would take advantage of <a href="http://polymer-project.org">Polymer</a> data
binding features and <a href="http://t-code.pl/blog/2015/08/polymer-templatizer/"><code>Polymer.templatizer</code></a> so that developers can define dynamic templates.</p>

<p>At the top level, I envision a generic <code>&lt;object-view&gt;</code> element. It would inspect the given resource and choose among the
available specialized or generic templates (see further down).</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="nt">&lt;object-view</span> <span class="na">id=</span><span class="s">&quot;top-view&quot;</span><span class="nt">&gt;&lt;/object-view&gt;</span>
</span><span class="line">
</span><span class="line"><span class="nt">&lt;script&gt;</span>
</span><span class="line">    <span class="kd">var</span> <span class="nx">objectView</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">&#39;top-view&#39;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="nx">objectView</span><span class="p">.</span><span class="nx">object</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">        <span class="s2">&quot;@type&quot;</span><span class="o">:</span> <span class="s2">&quot;http://example.com/vocab#Person&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="s2">&quot;@id&quot;</span><span class="o">:</span> <span class="s2">&quot;http://example.com/tomasz&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="s2">&quot;http://example.com/vocab#name&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">            <span class="s2">&quot;@value&quot;</span><span class="o">:</span> <span class="s2">&quot;Tomasz Pluskiewicz&quot;</span>
</span><span class="line">        <span class="p">},</span>
</span><span class="line">        <span class="s2">&quot;http://example.com/vocab#website&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">            <span class="s2">&quot;@value&quot;</span><span class="o">:</span> <span class="s2">&quot;http://t-code.pl&quot;</span>
</span><span class="line">        <span class="p">},</span>
</span><span class="line">        <span class="s2">&quot;http://example.com/vocab#avatar&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">            <span class="s2">&quot;@type&quot;</span><span class="o">:</span> <span class="s2">&quot;http://schema.org/ImageObject&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="s2">&quot;http://schema.org/caption&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">                <span class="s2">&quot;@value&quot;</span><span class="o">:</span> <span class="s2">&quot;Me in Kraków&quot;</span>
</span><span class="line">            <span class="p">},</span>
</span><span class="line">            <span class="s2">&quot;http://schema.org/contentUrl&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">                <span class="s2">&quot;@value&quot;</span><span class="o">:</span> <span class="s2">&quot;http://example.com/tomasz/avatar-large.jpg&quot;</span>
</span><span class="line">            <span class="p">},</span>
</span><span class="line">            <span class="s2">&quot;http://schema.org/thumbnail&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">                <span class="s2">&quot;http://schema.org/contentUrl&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">                    <span class="s2">&quot;@value&quot;</span><span class="o">:</span> <span class="s2">&quot;http://example.com/tomasz/avatar-small.jpg&quot;</span>
</span><span class="line">                <span class="p">}</span>
</span><span class="line">            <span class="p">}</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line"><span class="nt">&lt;/script&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>By setting the <code>objectView.resource</code> property the element would then look for template dedicated to the <code>ex:Person</code> class
(example <img class="emoji" title=":one:" alt=":one:" src="https://assets.github.com/images/icons/emoji/unicode/0031-20e3.png" height="20" width="20" align="absmiddle" />):</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="nt">&lt;template</span> <span class="na">is=</span><span class="s">&quot;resource-view-template&quot;</span>
</span><span class="line">          <span class="na">type=</span><span class="s">&quot;http://example.com/vocab#Person&quot;</span> <span class="na">as=</span><span class="s">&quot;person&quot;</span>
</span><span class="line">          <span class="na">compact-with=</span><span class="s">&#39;{ &quot;@vocab&quot;: &quot;http://example.com/vocab#&quot; }&#39;</span><span class="nt">&gt;</span>
</span><span class="line">    <span class="nt">&lt;h2&gt;</span>
</span><span class="line">        [[person.name]]
</span><span class="line">    <span class="nt">&lt;/h2&gt;</span>
</span><span class="line">    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;details&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;[[person.website]]&quot;</span><span class="nt">&gt;</span>My website<span class="nt">&lt;/a&gt;</span>
</span><span class="line">    <span class="nt">&lt;/div&gt;</span>
</span><span class="line">    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;avatar&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;object-view</span> <span class="na">object=</span><span class="s">&quot;[[person.avatar]]&quot;</span><span class="nt">&gt;&lt;/object-view&gt;</span>
</span><span class="line">    <span class="nt">&lt;/div&gt;</span>
</span><span class="line"><span class="nt">&lt;/template&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>See the <code>&lt;object-view&gt;</code> used again for <code>person.avatar</code>? This way it would be possible to create composable user interface
with specialized building blocks. Here’s how a template for <code>schema:ImageObject</code> can be rendered as a kind of a lightbox
(example <img class="emoji" title=":two:" alt=":two:" src="https://assets.github.com/images/icons/emoji/unicode/0032-20e3.png" height="20" width="20" align="absmiddle" />):</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="nt">&lt;template</span> <span class="na">is=</span><span class="s">&quot;resource-view-template&quot;</span>
</span><span class="line">          <span class="na">type=</span><span class="s">&quot;http://schema.org/ImageObject&quot;</span> <span class="na">as=</span><span class="s">&quot;img&quot;</span>
</span><span class="line">          <span class="na">compact-with=</span><span class="s">&#39;{ &quot;@vocab&quot;: &quot;http://schema.org&quot; }&#39;</span><span class="nt">&gt;</span>
</span><span class="line">    <span class="nt">&lt;a</span> <span class="na">title=</span><span class="s">&quot;[[img.caption]]&quot;</span> <span class="na">href=</span><span class="s">&quot;[[img.contentUrl]]&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;img</span> <span class="na">src=</span><span class="s">&quot;[[img.thumbnail.contentUrl]]&quot;</span> <span class="na">alt=</span><span class="s">&quot;[[img.caption]]&quot;</span> <span class="nt">/&gt;</span>
</span><span class="line">    <span class="nt">&lt;/a&gt;</span>
</span><span class="line"><span class="nt">&lt;/template&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>A simple proof of concept is available <a href="http://plnkr.co/edit/pQ6NOd">on Plunker</a></p>

<h3 id="template-selection-api">Template selection API</h3>

<p>Further extensions could introduce new ways for selecting a template based on the resource content, based on the predicate
(<em>for example to use different template for property <code>ex:husband</code> vs property <code>ex:wife</code>)</em> or to be able to create templates
not only for resources but also for literals (<em>for example to select templates based on language tag?</em>)</p>

<h2 id="common-elements-for-hydra-core-types">Common elements for Hydra Core types</h2>

<p>Building on top of the API above it would be possible to created predefined common elements for <a href="http://hydra-cg.com">Hydra Core</a> resources
returned by <a href="http://github.com/wikibus/heracles">heracles</a>. The most obvious idea is to build a reusable template for a <code>hydra:Collection</code>:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="c">&lt;!-- first template for collection elements --&gt;</span>
</span><span class="line"><span class="nt">&lt;template</span> <span class="na">is=</span><span class="s">&quot;resource-view-template&quot;</span>
</span><span class="line">          <span class="na">type=</span><span class="s">&quot;https://www.w3.org/ns/hydra/core#Collection&quot;</span> <span class="na">as=</span><span class="s">&quot;collection&quot;</span>
</span><span class="line">          <span class="na">compact-with=</span><span class="s">&quot;http://www.w3.org/ns/hydra/context.jsonld&quot;</span><span class="nt">&gt;</span>
</span><span class="line">
</span><span class="line">    <span class="nt">&lt;template</span> <span class="na">is=</span><span class="s">&quot;dom-repeat&quot;</span> <span class="na">items=</span><span class="s">&quot;[[collection.member]]&quot;</span> <span class="na">as=</span><span class="s">&quot;member&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;object-view</span> <span class="na">object=</span><span class="s">&quot;[[member]]&quot;</span><span class="nt">&gt;&lt;/object-view&gt;</span>
</span><span class="line">    <span class="nt">&lt;/template&gt;</span>
</span><span class="line">
</span><span class="line">    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;pager&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;object-view</span> <span class="na">object=</span><span class="s">&quot;[[collection.view]]&quot;</span><span class="nt">&gt;&lt;/object-view&gt;</span>
</span><span class="line">    <span class="nt">&lt;/div&gt;</span>
</span><span class="line"><span class="nt">&lt;/template&gt;</span>
</span><span class="line">
</span><span class="line"><span class="c">&lt;!-- views can have specialized templates --&gt;</span>
</span><span class="line"><span class="c">&lt;!-- the current spec defines the PartialCollectionView type --&gt;</span>
</span><span class="line"><span class="nt">&lt;template</span> <span class="na">is=</span><span class="s">&quot;resource-view-template&quot;</span>
</span><span class="line">          <span class="na">type=</span><span class="s">&quot;https://www.w3.org/ns/hydra/core#PartialCollectionView&quot;</span> <span class="na">as=</span><span class="s">&quot;view&quot;</span><span class="nt">&gt;</span>
</span><span class="line">    <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;[[view.first]]&quot;</span><span class="nt">&gt;</span>First<span class="nt">&lt;/a&gt;</span>
</span><span class="line">    <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;[[view.previous]]&quot;</span><span class="nt">&gt;</span>Previous<span class="nt">&lt;/a&gt;</span>
</span><span class="line">    <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;[[view.next]]&quot;</span><span class="nt">&gt;</span>Next<span class="nt">&lt;/a&gt;</span>
</span><span class="line">    <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;[[view.last]]&quot;</span><span class="nt">&gt;</span>Last<span class="nt">&lt;/a&gt;</span>
</span><span class="line"><span class="nt">&lt;/template&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>See how again the <code>&lt;object-view&gt;</code> is used to delegate the decision on what template to render.</p>

<p>Obviously real life Hydra templates will need to be much more sophisticated. Template for <code>hydra:PartialCollectionView</code>
would definitely want to hide unnecessary link. Template for <code>hydra:Collection</code> would likely display a different view for
collection member from the view displaying the same object outside a collection. This could be a feature of the <code>&lt;object-view&gt;</code>
element though.</p>

<h2 id="distributing-templates">Distributing templates</h2>

<p>Finally one wouldn’t want to declare these templates every time in an application. I imagine that a package containing
reusable Hydra Core elements would be simply wired up by a single element:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="c">&lt;!-- drop this on a page, and the above templates would be --&gt;</span>
</span><span class="line"><span class="c">&lt;!-- available for &lt;object-view&gt; elements --&gt;</span>
</span><span class="line"><span class="nt">&lt;hydra-core-templates&gt;&lt;/hydra-core-templates&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>Similar element could be offered by data publishers somewhat satisfying the REST’s code-on-demand constraint.</p>

<p>It is unclear however how it would be possible to customize behaviour of those templates/elements. Overriding the selected
template could be an easy way out though…</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Third labour of hypermedia - extensible media types]]></title>
    <link href="http://t-code.pl/blog/2016/04/hypermedia-must-be-extensible/"/>
    <updated>2016-04-24T16:00:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/04/hypermedia-must-be-extensible</id>
    <content type="html"><![CDATA[<p>Most hypermedia media types like <a href="http://stateless.co/hal_specification.html">HAL</a> or <a href="https://github.com/kevinswiber/siren">SIREN</a> are some sort of extension of JSON, which is understandable.
JSON is a natural choice because it already is the most common data interchange format for APIs. However JSON as syntax 
is by design simple and it doesn’t support vital part - <strong>links</strong>. To plug that hole these media types are intrusive in
that they impose a very specific structure of documents. Instead of extending the meaning of representations they hijack
the syntax and structure.</p>

<p>Personally I’m biased towards <a href="http://hydra-cg.com/spec/latest/core/">Hydra Core Vocabulary</a> because, unlike all other I have come across, it is based on
RDF. Why is it important?</p>

<!--more-->

<h2 id="extending-json-creates-rigid-structures">Extending JSON creates rigid structures</h2>

<p>Consider this sample from HAL specification website (excerpt).</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;_links&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">        <span class="nt">&quot;self&quot;</span><span class="p">:</span> <span class="p">{</span> <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;/orders&quot;</span> <span class="p">},</span>
</span><span class="line">        <span class="nt">&quot;curies&quot;</span><span class="p">:</span> <span class="p">[{</span> <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;ea&quot;</span><span class="p">,</span> <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;http://example.com/docs/rels/{rel}&quot;</span><span class="p">,</span> <span class="nt">&quot;templated&quot;</span><span class="p">:</span> <span class="kc">true</span> <span class="p">}],</span>
</span><span class="line">        <span class="nt">&quot;next&quot;</span><span class="p">:</span> <span class="p">{</span> <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;/orders?page=2&quot;</span> <span class="p">},</span>
</span><span class="line">        <span class="nt">&quot;ea:find&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">            <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;/orders{?id}&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="nt">&quot;templated&quot;</span><span class="p">:</span> <span class="kc">true</span>
</span><span class="line">        <span class="p">},</span>
</span><span class="line">        <span class="nt">&quot;ea:admin&quot;</span><span class="p">:</span> <span class="p">[{</span>
</span><span class="line">            <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;/admins/2&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="nt">&quot;title&quot;</span><span class="p">:</span> <span class="s2">&quot;Fred&quot;</span>
</span><span class="line">        <span class="p">}]</span>
</span><span class="line">    <span class="p">},</span>
</span><span class="line">    <span class="nt">&quot;currentlyProcessing&quot;</span><span class="p">:</span> <span class="mi">14</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;shippedToday&quot;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;_embedded&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">        <span class="nt">&quot;ea:order&quot;</span><span class="p">:</span> <span class="p">[{</span>
</span><span class="line">            <span class="nt">&quot;_links&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">                <span class="nt">&quot;self&quot;</span><span class="p">:</span> <span class="p">{</span> <span class="nt">&quot;href&quot;</span><span class="p">:</span> <span class="s2">&quot;/orders/123&quot;</span> <span class="p">}</span>
</span><span class="line">            <span class="p">},</span>
</span><span class="line">            <span class="nt">&quot;total&quot;</span><span class="p">:</span> <span class="mf">30.00</span><span class="p">,</span>
</span><span class="line">            <span class="nt">&quot;currency&quot;</span><span class="p">:</span> <span class="s2">&quot;USD&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="nt">&quot;status&quot;</span><span class="p">:</span> <span class="s2">&quot;shipped&quot;</span>
</span><span class="line">        <span class="p">}]</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>What is all that business with <code>_links</code> and <code>_embedded</code>? Also would you prefer to serve or consume XML for some reason?
Well, that will not be possible because most other media types above, except Hydra, HAL is JSON-based. Hence the need for
that convoluted document structure.</p>

<p>JSON also suffers from another deficiency - key ambiguity. It would be very easy to bump into clashes if we were to enrich
such representations with custom extensions.</p>

<h2 id="rdf-is-not-syntax">RDF is not syntax</h2>

<p>I wrote that Hydra Core is RDF-based. Unlike JSON RDF is a standard way to describe data structures and not syntax. It is
possible to write the very same piece of data in a multitude of ways yet still retaining the exact same meaning. First
there are <a href="https://www.w3.org/2001/sw/RDFCore/ntriples/"><strong>n-triples</strong></a>. Let’s state that my name is <em>Tomasz</em> and my friend can be downloaded from <code>http://t-code.pl/tomasz/friends</code>.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="text"><span class="line">&lt;http://t-code.pl/tomasz&gt; &lt;http://xmlns.com/foaf/0.1/name&gt; &quot;Tomasz&quot; .
</span><span class="line">&lt;http://t-code.pl/tomasz&gt; &lt;http://t-code.pl/api#friends&gt; &lt;http://t-code.pl/tomasz/friends&gt; .
</span></code></pre></td></tr></table></div></figure>

<p>As you see almost everything is an URI. This solves the ambiguity problem. This is however very verbose and redundant and
will waste a lot od bandwidth for large response. Fortunately there are other media types, which can represent the same
information in different form. There is the compacted flavour of n-triples called <a href="https://www.w3.org/TR/2014/REC-turtle-20140225/"><strong>Turtle</strong></a> and it’s similar twin
<a href="https://www.w3.org/TeamSubmission/n3/"><strong>Notation3</strong></a>.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="text"><span class="line">@base &lt;http://t-code.pl/&gt; .
</span><span class="line">@prefix foaf: &lt;http://xmlns.com/foaf/0.1/&gt; .
</span><span class="line">@prefix api: &lt;http://t-code.pl/api#&gt; .
</span><span class="line">
</span><span class="line">&lt;tomasz&gt; foaf:name &quot;Tomasz&quot; ;
</span><span class="line">         api:friends &lt;tomasz/friends&gt; .
</span></code></pre></td></tr></table></div></figure>

<p>There is also the old school XML-based format called <a href="https://www.w3.org/TR/2014/REC-rdf-syntax-grammar-20140225/"><strong>RDF/XML</strong></a>.</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class="xml"><span class="line"><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
</span><span class="line"><span class="nt">&lt;rdf:RDF</span>
</span><span class="line">   <span class="na">xmlns:foaf=</span><span class="s">&quot;http://xmlns.com/foaf/0.1/&quot;</span>
</span><span class="line">   <span class="na">xmlns:api=</span><span class="s">&quot;http://t-code.pl/api#&quot;</span>
</span><span class="line"><span class="nt">&gt;</span>
</span><span class="line">  <span class="nt">&lt;rdf:Description</span> <span class="na">rdf:about=</span><span class="s">&quot;http://t-code.pl/tomasz&quot;</span><span class="nt">&gt;</span>
</span><span class="line">    <span class="nt">&lt;api:friends</span> <span class="na">rdf:resource=</span><span class="s">&quot;http://t-code.pl/tomasz/friends&quot;</span><span class="nt">/&gt;</span>
</span><span class="line">    <span class="nt">&lt;foaf:name&gt;</span>Tomasz<span class="nt">&lt;/foaf:name&gt;</span>
</span><span class="line">  <span class="nt">&lt;/rdf:Description&gt;</span>
</span><span class="line"><span class="nt">&lt;/rdf:RDF&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>And finally there are multiple JSON-based formats, the most prominent example being <a href="http://json-ld.org"><strong>JSON-LD</strong></a> which does a great
job pretending it is not really RDF.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;@context&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">        <span class="nt">&quot;@base&quot;</span><span class="p">:</span> <span class="s2">&quot;http://t-code.pl/&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;http://xmlns.com/foaf/0.1/name&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="nt">&quot;friends&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">            <span class="nt">&quot;@id&quot;</span><span class="p">:</span> <span class="s2">&quot;http://t-code.pl/api#friends&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="nt">&quot;@type&quot;</span><span class="p">:</span> <span class="s2">&quot;@id&quot;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">    <span class="p">},</span>
</span><span class="line">    <span class="nt">&quot;@id&quot;</span><span class="p">:</span> <span class="s2">&quot;tomasz&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Tomasz&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;friends&quot;</span><span class="p">:</span> <span class="s2">&quot;tomasz/friends&quot;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<h2 id="hydra-extends-meaning-not-syntax">Hydra extends meaning not syntax</h2>

<p>What Hydra core does is essentially extend the data (<strong>not the syntax</strong>) with various hint for the clients so that they
can discover how to perform more requests. For example let’s add a hypermedia control stating that the above resource
<code>&lt;tomasz&gt;</code> can be updated with a <code>PUT</code> request.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
</pre></td><td class="code"><pre><code class="json"><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="nt">&quot;@context&quot;</span><span class="p">:</span> <span class="s2">&quot;http://same.as.above/but/remote&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;@id&quot;</span><span class="p">:</span> <span class="s2">&quot;tomasz&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;Tomasz&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;friends&quot;</span><span class="p">:</span> <span class="s2">&quot;tomasz/friends&quot;</span><span class="p">,</span>
</span><span class="line">    <span class="nt">&quot;http://www.w3.org/ns/hydra/core#operation&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">        <span class="nt">&quot;http://www.w3.org/ns/hydra/core#method&quot;</span><span class="p">:</span> <span class="s2">&quot;PUT&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="nt">&quot;http://www.w3.org/ns/hydra/core#expects&quot;</span><span class="p">:</span> <span class="p">{</span>
</span><span class="line">            <span class="nt">&quot;@id&quot;</span><span class="p">:</span> <span class="s2">&quot;http://t-code.pl/api#Person&quot;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>You could still convert this between various RDF serailizations and retain the meaning!</p>

<p>For a Hydra-based API to be complete there must be a lot of information provided by the server. The above is the tip of
the iceberg. The body of the described <code>PUT</code> request must conform the definition of the <code>http://t-code.pl/api#Person</code> type.</p>

<blockquote>
  <p>But where is this definition? And how detailed can this definition be?</p>
</blockquote>

<p>Hydra core is served as a runtime API documentation, linked with a specific Link header relation. This documentation can
contain a number of simple definitions such as required fields, expected data types etc. It can also be extended so that
clients aware of the extension can adapt better to the API. For example a server can annotate a field as being a password,
so that an appropriate control is presented on the website.</p>

<p>This is where the first really big hurdle stands.</p>

<blockquote>
  <p>Where are these aware clients? And where are the servers?</p>
</blockquote>

<h2 id="we-need-the-tooling">We need the tooling</h2>

<p>I’ve started creating both a server and client tools to produce and consume Hydra-based hypermedia. The server side is 
implemented as a .NET Library for Nancy called <a href="http://github/com/wikibus/Argolis">Argolis</a>. The client side is a JavsScript library called 
<a href="http://github/com/wikibus/heracles">heracles</a>. I’m also experimenting with a way to produce a dynamic yet customizable UI with Web Components.</p>

<p>I will be showing usage examples and discussing ideas in future blog posts.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The labours of hypermedia]]></title>
    <link href="http://t-code.pl/blog/2016/04/labours-of-hypermedia/"/>
    <updated>2016-04-24T15:40:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/04/labours-of-hypermedia</id>
    <content type="html"><![CDATA[<p>Going full-on with hypermedia takes some preparations and to convince the masses will require some tooling and examples.
Especially tooling. The ability to produce rich developer experience is the determinant of many successful technologies. 
Another crucial factor are real-life uses cases solved by the technology in question. I would like to focus on the former.</p>

<p>As much as the industry has bought into lightweight Web APIs, often inappropriately called RESTful, there has been an uphill
battle to have real hypermedia gain traction.</p>

<!--more-->

<h2 id="why-hasnt-hypermedia-gone-mainstream">Why hasn’t hypermedia gone mainstream?</h2>

<p>I’m still not sure what are the main factors which contribute to the slow adoption of real, unadulterated hypermedia. It
seems that a lot of people find hypermedia hard. Just look at all those question on StackOverflow. There are also a number
of proponents of <em>pragmatic REST</em>. In my opinion that mostly means throwing the baby out with bath water. Pragmatic approach
to REST is seen everywhere.</p>

<p>At the worse end there are for example <a href="http://t-code.pl/blog/2016/03/rest-misconceptions-5">REST documentation</a> and <a href="http://t-code.pl/blog/2016/03/rest-misconceptions-6">flawed methods of
API versioning</a>. To my despair it seems that too many supporters of such practices
completely neglect hypertext and best practices around <a href="http://t-code.pl/blog/2016/02/rest-misconceptions-1">URIs</a> and <a href="http://t-code.pl/blog/2016/02/rest-misconceptions-2">links</a>.</p>

<p>A slightly brighter shade of <em>pragmatic REST</em> are the various media types, which actually do put the emphasis on runtime
discoverability. This is not something I will disagree with. Not every API is made equal and not every media type needs
all the features necessary for a complete hypertext-driven interaction. With links to begin with (think <a href="http://stateless.co/hal_specification.html">HAL</a>),
through forms (supported by <a href="http://amundsen.com/media-types/collection/">Collection+JSON</a> for example) all the way to rich (<a href="https://github.com/kevinswiber/siren">SIREN</a> or <a href="http://www.narwhl.com/">NARWHL</a>)
and extensible (<a href="http://hydra-cg.com/spec/latest/core/">Hydra Core</a>) hypermedia.</p>

<p>An optimistic person could envision a proliferation of cool media types and servers using them. Is this so far from the
truth! I think that there are still a number of puzzles missing which hinders adoption of proper hypermedia.</p>

<h2 id="the-labours-of-hypermedia">The labours of hypermedia</h2>

<p>Hypermedia has still a long way to go. To make it happen for real there has to be an active community which understands
its benefits and will produce all the necessary moving parts.</p>

<ol>
  <li>We must raise <strong>awareness</strong> of the benefits of hypermedia</li>
  <li>We must define <strong>best practices</strong> around the shady parts of REST such as versioning</li>
  <li>We need powerful and <strong>extensible media types</strong></li>
  <li>We must create the <strong>tooling</strong> around these media types for both server and client side</li>
  <li>We need a new paradigm for creating <strong>adaptive user interfaces</strong> both of autonomous and bespoke clients</li>
</ol>

<p>In future posts I will try to address these labours and show some of my recipes for actual hypermedia-ness.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Heracles resources vs JSON-LD compaction - enumerable js properties]]></title>
    <link href="http://t-code.pl/blog/2016/04/heracles-compacting-resources/"/>
    <updated>2016-04-20T08:45:00+00:00</updated>
    <id>http://t-code.pl/blog/2016/04/heracles-compacting-resources</id>
    <content type="html"><![CDATA[<p>In my <a href="http://t-code.pl/blog/2016/04/introducing-heracles/">previous post</a> I presented the first incarnation of Heracles, the Hydra Core
client library. While trying to replace my makeshift client I’d implemented for an in-house training project at <a href="http://pgs-soft.com">PGS</a>
I quickly decided that I’m going to need a way to compact my resources. It wasn’t that hard but there was one simple
hurdle to overcome.</p>

<!--more-->

<h2 id="tldr-with-heracles-you-can-do-this"><strong>TL;DR; with Heracles you can do this:</strong></h2>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="nx">Hydra</span><span class="p">.</span><span class="nx">Resource</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="s1">&#39;http://my.api/my/resource&#39;</span><span class="p">)</span>
</span><span class="line">    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">res</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">jsonld</span><span class="p">.</span><span class="nx">promises</span><span class="p">.</span><span class="nx">compact</span><span class="p">(</span><span class="nx">res</span><span class="p">,</span> <span class="nx">context</span><span class="p">);</span>
</span><span class="line">    <span class="p">})</span>
</span><span class="line">    <span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">compacted</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">        <span class="c1">// do something with the compacted resource</span>
</span><span class="line">    <span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>

<h2 id="uri-properties-are-a-nuisance">URI properties are a nuisance</h2>

<p>Just as in heracles, in my proof of concept code I too mostly worked with expanded JSON-LD objects. This has the downside
that any time I needed to access the properties full property identifiers must be used. Also it is not possible with
<a href="https://www.polymer-project.org/">Polymer</a> to use the indexer notation for declarative data binding:</p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="c">&lt;!-- Such markup is not valid data binding syntax in Polymer --&gt;</span>
</span><span class="line"><span class="nt">&lt;span&gt;</span>{{myObject[&#39;http://xmlns.com/foaf/0.1/name&#39;]}}<span class="nt">&lt;/span&gt;</span>
</span><span class="line">
</span><span class="line"><span class="c">&lt;!-- Databound object&#39;s properties must be accessed with the dot notation --&gt;</span>
</span><span class="line"><span class="nt">&lt;span&gt;</span>{{myObject.name}}<span class="nt">&lt;/span&gt;</span>
</span></code></pre></td></tr></table></div></figure>

<p>This is precisely what JSON-LD compaction algorithm is for. It translates URI keys in a compacted JSON object. This 
translation is defined in a <code>@context</code> object.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="c1">// before</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">original</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">    <span class="s1">&#39;http://xmlns.com/foaf/0.1/name&#39;</span><span class="o">:</span> <span class="s1">&#39;Tomasz Pluskiewicz&#39;</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="c1">// after</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">compacted</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">    <span class="s1">&#39;@context&#39;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">        <span class="s1">&#39;name&#39;</span><span class="o">:</span> <span class="s1">&#39;http://xmlns.com/foaf/0.1/&#39;</span>
</span><span class="line">    <span class="p">},</span>
</span><span class="line">    <span class="s1">&#39;name&#39;</span><span class="o">:</span> <span class="s1">&#39;Tomasz Pluskiewicz&#39;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>There are many tricks up compaction’s sleeve, which can help turning ugly JSON-LD into a digestive form. Have a look at
<a href="http://www.slideshare.net/lanthaler/building-next-generation-web-ap-is-with-jsonld-and-hydra/23">this presentation</a> by <a href="https://twitter.com/markuslanthaler">Markus Manthaler</a> for some more examples.</p>

<h2 id="my-code-before">My code before</h2>

<p>In my code I used compaction to get rid of long URI keys so that I can take advantage of Polymer’s data binding without
verbose methods like <a href="https://www.polymer-project.org/1.0/docs/devguide/properties.html#computed-properties">computed properties</a> or wrapping the object in a view model class.</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="nx">getAuthors</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="kd">var</span> <span class="nx">context</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">        <span class="s2">&quot;@vocab&quot;</span><span class="o">:</span> <span class="s2">&quot;http://wykop.pgs-soft.com/vocab#WeeksLink/&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="s2">&quot;member&quot;</span><span class="o">:</span> <span class="s2">&quot;http://www.w3.org/ns/hydra/core#member&quot;</span><span class="p">,</span>
</span><span class="line">        <span class="s2">&quot;links&quot;</span><span class="o">:</span> <span class="p">{</span>
</span><span class="line">            <span class="s2">&quot;@id&quot;</span><span class="o">:</span> <span class="s2">&quot;http://wykop.pgs-soft.com/vocab#WeeksLinksBySubmitter/links&quot;</span><span class="p">,</span>
</span><span class="line">            <span class="s2">&quot;@container&quot;</span><span class="o">:</span><span class="s2">&quot;@set&quot;</span>
</span><span class="line">        <span class="p">},</span>
</span><span class="line">        <span class="s2">&quot;submitter&quot;</span><span class="o">:</span> <span class="s2">&quot;http://wykop.pgs-soft.com/vocab#WeeksLinksBySubmitter/submitter&quot;</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">
</span><span class="line">    <span class="nx">jsonld</span><span class="p">.</span><span class="nx">promises</span><span class="p">.</span><span class="nx">compact</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">context</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="nx">model</span> <span class="o">=&gt;</span> <span class="p">{</span>
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="nx">compactedModel</span> <span class="o">=</span> <span class="nx">model</span><span class="p">;</span>
</span><span class="line">    <span class="p">});</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>This is simple, the jsonld.js library takes care of the heavy lifting and produces a compacted object which is data binding
friendly.</p>

<h2 id="enter-heracles">Enter heracles</h2>

<p>How is this relevant to the heracles library? In my previous post I showed the <code>Operation</code> type (and other parts of the
ApiDocumentation classes) can be compacted so that working with them is easier.</p>

<p>Resources however are a little different. They are always returned expanded and thus should be ready for being compacted.
I was surprised to see that <code>jsonld.promises.compact</code> throws a stack overflow error. The reason is that JSON-LD algorithms
are not designed to work with cyclical object graphs. It simply loops until the call stack runs out.</p>

<h3 id="the-resource-class">The Resource class</h3>

<p>In my code I have this <code>PartialCollectionView</code> class (excerpt):</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">PartialCollectionView</span> <span class="kr">extends</span> <span class="nx">Resource</span> <span class="p">{</span>
</span><span class="line">    <span class="kr">private</span> <span class="nx">_collection</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">    <span class="nx">constructor</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="o">:</span><span class="nx">IApiDocumentation</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kr">super</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="nx">_collection</span> <span class="o">=</span> <span class="nx">findCollection</span><span class="p">(</span><span class="nx">incomingLinks</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="nx">get</span> <span class="nx">collection</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_collection</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>See the <code>collection</code> getter? This is where I had a cycle (collection -&gt; view -&gt; collection …). There was also another
cycle inside the <code>apiDocumentation</code> getter in the base <code>Resource</code> class. There are actually two thing going on here. The
first and obvious culprit is the <em>private field</em>. Of course this is just TypeScript sugar, because it will become just a
typical field in the compiled JavaScript. JavaScript has no such notion of private members.</p>

<h2 id="solution">Solution</h2>

<p>The first step was to get rid of the field. There is no perfect way to do that but a friend of mine sent me <a href="http://davidvujic.blogspot.com/2015/03/what-wait-really-oh-no-a-post-about-es6-classes-and-privacy.html">this post</a>,
which presents the use of <code>WeakMap</code> as a possible solution. With that I changed my code so that it no longer contains
unwanted fields. <em>(actual code is actually a little different but you get the drift)</em></p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kd">var</span> <span class="nx">_collection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">WeakMap</span><span class="p">();</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">PartialCollectionView</span> <span class="kr">extends</span> <span class="nx">Resource</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">    <span class="nx">constructor</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="o">:</span><span class="nx">IApiDocumentation</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kr">super</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">        <span class="nx">_collection</span> <span class="o">=</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">findCollection</span><span class="p">(</span><span class="nx">incomingLinks</span><span class="p">));</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="nx">get</span> <span class="nx">collection</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>Unfortunately the compaction algorithm still entered the vicious cycle and failed. Why is that? Because <a href="https://developer.mozilla.org/pl/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable 
properties</a>. jsonld.js <a href="https://github.com/digitalbazaar/jsonld.js/blob/master/js/jsonld.js#L6594">iterates over the object</a> using simple <code>for (var i in obj)</code> loop, which also
returns all getters by default. One way is to use the native <code>Object.defineProperty</code> method instead of ES6 <code>get x()</code> 
syntax but it breaks TypeScript code analysis and generally smells. There is a better way though.</p>

<h3 id="solution-part-two">Solution part two</h3>

<p>Luckily TypeScript has the decorators and there is a decorator, which does precisely what I wanted. Instead of writing</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kd">var</span> <span class="nx">_collection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">WeakMap</span><span class="p">();</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">PartialCollectionView</span> <span class="kr">extends</span> <span class="nx">Resource</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">    <span class="nx">constructor</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="o">:</span><span class="nx">IApiDocumentation</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kr">super</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">        <span class="nx">_collection</span> <span class="o">=</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">findCollection</span><span class="p">(</span><span class="nx">incomingLinks</span><span class="p">));</span>
</span><span class="line">
</span><span class="line">        <span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">&#39;collection&#39;</span><span class="p">,</span> <span class="p">{</span>
</span><span class="line">            <span class="nx">get</span><span class="o">:</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="k">this</span><span class="p">)</span>
</span><span class="line">        <span class="p">});</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<p>I can simply install the <a href="https://www.npmjs.com/package/core-decorators">core-decorators package</a> from jspm (npm) and 
decorate the property with <code>@nonenumerable</code></p>

<figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="bash"><span class="line">jpsm install npm:core-decorators
</span></code></pre></td></tr></table></div></figure>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="p">{</span> <span class="nx">nonenumerable</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">&#39;core-decorators&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kd">var</span> <span class="nx">_collection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">WeakMap</span><span class="p">();</span>
</span><span class="line">
</span><span class="line"><span class="kr">export</span> <span class="kr">class</span> <span class="nx">PartialCollectionView</span> <span class="kr">extends</span> <span class="nx">Resource</span> <span class="p">{</span>
</span><span class="line">
</span><span class="line">    <span class="nx">constructor</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="o">:</span><span class="nx">IApiDocumentation</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kr">super</span><span class="p">(</span><span class="nx">actualResource</span><span class="p">,</span> <span class="nx">apiDoc</span><span class="p">,</span> <span class="nx">incomingLinks</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">        <span class="nx">_collection</span> <span class="o">=</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">findCollection</span><span class="p">(</span><span class="nx">incomingLinks</span><span class="p">));</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="err">@</span><span class="nx">nonenumerable</span>
</span><span class="line">    <span class="nx">get</span> <span class="nx">collection</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">_collection</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>

<h3 id="one-caveat">One Caveat</h3>

<p>Of course this will still fail if there are actual cycles in the object graph. I’m hoping though that it won’t be the
case all too often. And for the rare occasion a library like <a href="https://www.npmjs.com/package/circular-json">circular-json</a> can be used as suggested in <a href="https://github.com/digitalbazaar/jsonld.js/issues/97">this github
issue</a>. It will make sure that there are no reference cycles. Unfortunately it is a only replacement for
<code>JSON.stringify</code> and so to use it with jsonld.js it’s necessary to deserialize and serialize every time:</p>

<figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kr">import</span> <span class="o">*</span> <span class="nx">as</span> <span class="nx">CircularJSON</span> <span class="nx">from</span> <span class="s1">&#39;circular-json&#39;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="kd">var</span> <span class="nx">serialized</span> <span class="o">=</span> <span class="nx">CircularJSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">object</span><span class="p">);</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">jsonLd</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">serialized</span><span class="p">);</span>
</span><span class="line"><span class="nx">jsonld</span><span class="p">.</span><span class="nx">promises</span><span class="p">.</span><span class="nx">compact</span><span class="p">(</span><span class="nx">jsonLd</span><span class="p">,</span> <span class="nx">context</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="cm">/* ... */</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>

<p>This is because jsonld.js wants to treat a string parameter as URI.</p>

<p>Please let me know if there is a better way for handling cyclical objects…</p>

]]></content>
  </entry>
  
</feed>
