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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Your contribution here.

* [#334](https://github.com/codegram/hyperclient/pull/334): Added `Link#_query` to send query parameters that a link doesn't declare - [@dblock](https://github.com/dblock).
* [#333](https://github.com/codegram/hyperclient/pull/333): Updated danger-pr-comment to 0.2.0 and fixed the hash conversion assertion for Minitest 6.1 - [@dblock](https://github.com/dblock).

### 2.1.0 (2026/10/14)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON
- [Resources and Attributes](#resources-and-attributes)
- [Links and Embedded Resources](#links-and-embedded-resources)
- [Templated Links](#templated-links)
- [Query Parameters](#query-parameters)
- [Curies](#curies)
- [Attributes](#attributes)
- [HTTP](#http)
Expand Down Expand Up @@ -156,6 +157,17 @@ Invoking `api.place(id: 'oxpoints:23232339').name` is equivalent to `api._links.

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`.

## Query Parameters

HAL APIs declare the query parameters they accept in templated links, and `_expand` only fills in those. To send query parameters that a link doesn't declare, or to add them to a link that isn't templated, use `_query`. It returns a new link with the parameters appended to its URL, after any existing query string.

```ruby
api._links.places._query(sort: 'name')._get # GET /places/?sort=name
api._links.place._expand(id: 'oxpoints:23232339')._query(fields: 'name')._get # GET /places/oxpoints%3A23232339?fields=name
```

Array values are sent as repeated parameters, and parameters with `nil` values are dropped.

## Curies

Curies provide compact names for relation documentation. The local demo adds an `oxford` CURIE and an `oxford:places` relation.
Expand Down
32 changes: 27 additions & 5 deletions lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class Link
# entry_point - The EntryPoint object to inject the configuration.
# uri_variables - The optional Hash with the variables to expand the link
# if it is templated.
def initialize(key, link, entry_point, uri_variables = nil)
# query_params - The optional Hash with query parameters to add to the
# link's URL.
def initialize(key, link, entry_point, uri_variables = nil, query_params = nil)
@key = key
@link = link
@entry_point = entry_point
@uri_variables = uri_variables
@query_params = query_params
@resource = nil
end

Expand Down Expand Up @@ -54,14 +57,22 @@ def _templated?
#
# Returns a new Link with the expanded variables.
def _expand(uri_variables = {})
self.class.new(@key, @link, @entry_point, uri_variables)
self.class.new(@key, @link, @entry_point, uri_variables, @query_params)
end

# Public: Adds query parameters to the Link's URL, whether or not the link
# is templated or declares them. Parameters with nil values are dropped.
#
# query_params - The Hash with the query parameters to add.
#
# Returns a new Link with the query parameters.
def _query(query_params = {})
self.class.new(@key, @link, @entry_point, @uri_variables, (@query_params || {}).merge(query_params))
end

# Public: Returns the url of the Link.
def _url
return @link['href'] unless _templated?

@url ||= _uri_template.expand(@uri_variables || {}).to_s
@url ||= _add_query_params(_templated? ? _uri_template.expand(@uri_variables || {}).to_s : @link['href'])
end

# Public: Returns an array of variables from the URITemplate.
Expand Down Expand Up @@ -189,6 +200,17 @@ def _uri_template
@uri_template ||= Addressable::Template.new(@link['href'])
end

# Internal: Appends the query parameters to the URL, keeping its existing
# query string and fragment.
def _add_query_params(url)
params = (@query_params || {}).compact
return url if params.empty?

uri = Addressable::URI.parse(url)
uri.query = [uri.query, Addressable::URI.form_encode(params)].compact.reject(&:empty?).join('&')
uri.to_s
end

def http_method(method, body = nil)
response = @entry_point.connection.run_request(method, _url, body, nil)
resource = Resource.new(response.body, @entry_point, response)
Expand Down
62 changes: 62 additions & 0 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,68 @@ module Hyperclient
end
end

describe '_query' do
it 'adds query parameters to a link that is not templated' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)

_(link._query(sort: 'name')._url).must_equal '/orders?sort=name'
end

it 'adds query parameters a templated link does not declare' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)

_(link._expand(id: '1')._query(sort: 'name')._url).must_equal '/orders?id=1&sort=name'
end

it 'keeps query parameters when expanded afterwards' do
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)

_(link._query(sort: 'name')._expand(id: '1')._url).must_equal '/orders?id=1&sort=name'
end

it 'keeps the existing query string and fragment' do
link = Link.new('key', { 'href' => '/orders?status=open&status=paid#top' }, entry_point)

_(link._query(sort: 'name')._url).must_equal '/orders?status=open&status=paid&sort=name#top'
end

it 'combines chained query parameters' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)

_(link._query(sort: 'name')._query(page: 2, sort: 'date')._url).must_equal '/orders?sort=date&page=2'
end

it 'encodes values and repeats array parameters' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)

_(link._query(q: 'a&b c', ids: [1, 2])._url).must_equal '/orders?q=a%26b+c&ids=1&ids=2'
end

it 'drops nil and empty query parameters' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)

_(link._query(sort: nil)._url).must_equal '/orders'
_(link._query({})._url).must_equal '/orders'
end

it 'does not change the original link' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)
link._query(sort: 'name')

_(link._url).must_equal '/orders'
end

it 'sends the query parameters with the request' do
link = Link.new('key', { 'href' => '/orders' }, entry_point)

stub_request(entry_point.connection) do |stub|
stub.get('http://api.example.org/orders?sort=name') { [200, {}, { 'sorted' => true }] }
end

_(link._query(sort: 'name')._get.sorted).must_equal true
end
end

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