diff --git a/.travis.yml b/.travis.yml index b7f6289..a9f85ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,8 @@ rvm: - 2.0.0 - 2.1 - 2.2 - - rbx-2.2.10 + - 2.3.0 + - rbx-2 - jruby-19mode - ruby-head - jruby-head @@ -12,3 +13,7 @@ matrix: allow_failures: - rvm: ruby-head - rvm: jruby-head + - rvm: rbx-2 + +before_install: + - gem install bundler diff --git a/CHANGELOG.md b/CHANGELOG.md index 712b01a..b2b0931 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ -### 0.7.3 (Next) +### 0.8.0 (Next) + +* [#97](https://github.com/codegram/hyperclient/issues/97): Fix: curies are no longer used as link shorteners - [@joshco](https://github.com/joshco), [@dblock](https://github.com/dblock). * Your contribution here. diff --git a/README.md b/README.md index 0fdf265..3a294b1 100644 --- a/README.md +++ b/README.md @@ -127,10 +127,11 @@ The client is responsible for supplying all the necessary parameters. Templated ### Curies -Curies are named tokens that you can define in the document and use to express curie relation URIs in a friendlier, more compact fashion. For example, the demo API contains very long links to images that use an "images" curie. Hyperclient handles curies and resolves these into full links automatically. +Curies are a suggested means by which to link documentation of a given resource. For example, the demo API contains very long links to images that use an "images" curie. ```ruby puts spline['image:thumbnail'] # => https://grape-with-roar.herokuapp.com/api/splines/uuid/images/thumbnail.jpg +puts spline.links._curies['image'].expand('thumbnail') # => /docs/images/thumbnail ``` ### Attributes diff --git a/UPGRADING.md b/UPGRADING.md index 2ab6df4..2912977 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,50 @@ Upgrading Hyperclient ===================== +### Upgrading to >= 0.8.0 + +### Changes in curies + +Previous versions of Hyperclient misinterpreted the HAL RFC and used curies as if they were designed to enable compact URIs. Curies in HAL are provided for documentation purposes. + +Since version 0.8.0 they are no longer expanded. Consider the following example. + +```json +{ + "_links": { + "self": { "href": "/orders" }, + "curies": [{ + "name": "acme", + "href": "http://docs.acme.com/relations/{rel}", + "templated": true + }], + "acme:widgets": { "href": "/widgets" } + } +} +``` + +```rb +client = Hyperclient.new('http://example.org/api') + +client._links["acme:widgets"] + +# before +# => "http://docs.acme.com/relations/widgets" + +# after +# => "http://example.org/api/widgets" +``` + +In addition, you can now access and expand the curie. + +```rb +curie = client._links._curies["acme"] +curie.expand('widgets') +# => "http://docs.acme.com/relations/widgets" +``` + +See [#97](https://github.com/codegram/hyperclient/issues/97) for more information. + ### Upgrading to >= 0.7.0 #### Changes in default headers diff --git a/lib/hyperclient/link_collection.rb b/lib/hyperclient/link_collection.rb index cf98d67..09f525c 100644 --- a/lib/hyperclient/link_collection.rb +++ b/lib/hyperclient/link_collection.rb @@ -11,6 +11,8 @@ module Hyperclient # resource.links.author # class LinkCollection < Collection + attr_reader :_curies + # Public: Initializes a LinkCollection. # # collection - The Hash with the links. @@ -19,13 +21,13 @@ class LinkCollection < Collection def initialize(collection, curies, entry_point) fail "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect) - @curies = (curies || {}).reduce({}) do |hash, curie_hash| + @_curies = (curies || {}).reduce({}) do |hash, curie_hash| curie = build_curie(curie_hash, entry_point) hash.update(curie.name => curie) end @collection = (collection || {}).reduce({}) do |hash, (name, link)| - hash.update(name => build_link(name, link, @curies, entry_point)) + hash.update(name => build_link(name, link, @_curies, entry_point)) end end @@ -46,10 +48,6 @@ def build_link(name, link_or_links, curies, entry_point) link_or_links.map do |link| build_link(name, link, curies, entry_point) end - elsif (curie_parts = /(?[^:]+):(?.+)/.match(name)) - curie = curies[curie_parts[:ns]] - link_or_links['href'] = curie.expand(link_or_links['href']) if curie - Link.new(name, link_or_links, entry_point) else Link.new(name, link_or_links, entry_point) end diff --git a/test/fixtures/element.json b/test/fixtures/element.json index b18edc3..6d93cdb 100644 --- a/test/fixtures/element.json +++ b/test/fixtures/element.json @@ -18,13 +18,13 @@ "curies" : [ { "name": "image", - "href": "/images/{rel}", + "href": "/docs/images/{rel}", "templated": true } ], "null_link": null, "image:thumbnail": { - "href": "thumbnails/{version}.jpg", + "href": "/images/thumbnails/{version}.jpg", "templated": true } }, diff --git a/test/hyperclient/curie_test.rb b/test/hyperclient/curie_test.rb index c983990..b480f71 100644 --- a/test/hyperclient/curie_test.rb +++ b/test/hyperclient/curie_test.rb @@ -23,12 +23,17 @@ module Hyperclient end let(:curie) do - Curie.new({ 'name' => 'image', 'href' => '/images/{?rel}', 'templated' => true }, entry_point) + Curie.new({ 'name' => 'image', 'href' => '/images/{rel}', 'templated' => true }, entry_point) end describe '_name' do it 'returns curie name' do curie.name.must_equal 'image' end end + describe 'expand' do + it 'expands link' do + curie.expand('thumbnail').must_equal '/images/thumbnail' + end + end end end diff --git a/test/hyperclient/link_collection_test.rb b/test/hyperclient/link_collection_test.rb index db9c824..cb2b658 100644 --- a/test/hyperclient/link_collection_test.rb +++ b/test/hyperclient/link_collection_test.rb @@ -46,9 +46,13 @@ module Hyperclient describe 'curied link collection' do let(:curied_link) { links['image:thumbnail'] } + let(:curie) { links._curies['image'] } it 'must expand' do curied_link._expand(version: 'small')._url.must_equal '/images/thumbnails/small.jpg' end + it 'exposes curie' do + curie.expand('thumbnail').must_equal '/docs/images/thumbnail' + end end describe 'array of links' do