Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,3 +13,7 @@ matrix:
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-2

before_install:
- gem install bundler
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/hyperclient/link_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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 = /(?<ns>[^:]+):(?<short_name>.+)/.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
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/element.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
Expand Down
7 changes: 6 additions & 1 deletion test/hyperclient/curie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/hyperclient/link_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down