Skip to content

Commit

Permalink
Added support for templated links with no arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Oct 16, 2014
1 parent 538f113 commit e4162ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This version introduces several backwards incompatible changes. See [UPGRADING](
* [#51](https://github.com/codegram/hyperclient/issues/51), [#75](https://github.com/codegram/hyperclient/pull/75): Added support for setting headers and overriding or extending the default Faraday connection block before a connection is constructed - [@dblock](https://github.com/dblock).
* [#41](https://github.com/codegram/hyperclient/issues/41), [#73](https://github.com/codegram/hyperclient/pull/73): All Link HTTP methods now return a Resource, including `_get`, which has been aliased to `_resource`, `_post`, `_put`, `_patch`, `_head` and `_options` - [@dblock](https://github.com/dblock).
* [#72](https://github.com/codegram/hyperclient/pull/72): The default Faraday block now uses `Faraday::Response::RaiseError` and will cause HTTP errors to be raised as exceptions - [@dblock](https://github.com/dblock).
* [#77](https://github.com/codegram/hyperclient/pull/77): Added support for templated links with all optional arguments - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.5.0 (October 1, 2014)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ puts "Spline #{spline.uuid} is #{spline.reticulated ? 'reticulated' : 'not retic

Invoking `api.spline(uuid: 'uuid').reticulated` is equivalent to `api._links.spline._expand(uuid: 'uuid')._resource._attributes.reticulated`.

The client is responsible for supplying all the necessary parameters. Templated links don't do any strict parameter name checking and don't support required vs. optional parameters. Parameters not declared by the API will be dropped and will not have any effect when passed to `_expand`.

### 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.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ The default Faraday block now uses `Faraday::Response::RaiseError` and will caus

The `Link#_get` method has been aliased to `_resource`. All HTTP methods, including `_post`, `_put`, `_delete`, `_patch`, `_options` and `_head` now return instances of Resource. Older versions returned a `Faraday::Response`.

#### Changes in URI Template Expansion

A `MissingURITemplateVariablesException` exception will no longer be raised when expanding a link with no arguments. The `Link#_expand` method will now also accept zero arguments and default the variables to `{}`. This enables support for templated links with all optional arguments.

### Upgrading to >= 0.5.0

#### Remove Navigational Elements
Expand Down
18 changes: 2 additions & 16 deletions lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,14 @@ def _templated?
# uri_variables - The Hash with the variables to expand the URITemplate.
#
# Returns a new Link with the expanded variables.
def _expand(uri_variables)
def _expand(uri_variables = {})
self.class.new(@key, @link, @entry_point, uri_variables)
end

# Public: Returns the url of the Link.
#
# Raises MissingURITemplateVariables if the Link is templated but there are
# no uri variables to expand it.
def _url
return @link['href'] unless _templated?
fail MissingURITemplateVariablesException if @uri_variables.nil?

@url ||= _uri_template.expand(@uri_variables)
@url ||= _uri_template.expand(@uri_variables || {})
end

# Public: Returns an array of variables from the URITemplate.
Expand Down Expand Up @@ -202,13 +197,4 @@ def _uri_template
@uri_template ||= URITemplate.new(@link['href'])
end
end

# Public: Exception that is raised when building a templated Link without uri
# variables.
class MissingURITemplateVariablesException < StandardError
# Public: Returns a String with the exception message.
def message
'The URL to this links is templated, but no variables where given.'
end
end
end
32 changes: 23 additions & 9 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,38 @@ module Hyperclient
end

describe '_expand' do
it 'buils a Link with the templated URI representation' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
describe 'required argument' do
it 'builds a Link with the templated URI representation' do
link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point)
link._expand(id: '1')._url.must_equal '/orders/1'
end

Link.expects(:new).with('key', anything, entry_point, id: '1')
link._expand(id: '1')
it 'expands an uri template without variables' do
link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point)
link._expand._url.must_equal '/orders/'
link._url.must_equal '/orders/'
end
end

it 'raises if no uri variables are given' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
lambda { link._expand }.must_raise ArgumentError
describe 'query string argument' do
it 'builds a Link with the templated URI representation' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
link._expand(id: '1')._url.must_equal '/orders?id=1'
end

it 'expands an uri template without variables' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
link._expand._url.must_equal '/orders'
link._url.must_equal '/orders'
end
end
end

describe '_url' do
it 'raises when missing required uri_variables' do
it 'expands an uri template without variables' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)

lambda { link._url }.must_raise MissingURITemplateVariablesException
link._url.must_equal '/orders'
end

it 'expands an uri template with variables' do
Expand Down

0 comments on commit e4162ae

Please sign in to comment.