From 0345d4ddcb169dcd5ae613482ea6b3e93c84cfd8 Mon Sep 17 00:00:00 2001 From: dB Date: Sat, 15 Aug 2015 16:46:54 -0400 Subject: [PATCH] WIP: always expand additional link variables, #84. --- lib/hyperclient/link.rb | 1 + lib/uri_template/rfc6570/expression.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 lib/uri_template/rfc6570/expression.rb diff --git a/lib/hyperclient/link.rb b/lib/hyperclient/link.rb index c660ed5..fa98cf8 100644 --- a/lib/hyperclient/link.rb +++ b/lib/hyperclient/link.rb @@ -1,6 +1,7 @@ require 'hyperclient/resource' require 'uri_template' require 'futuroscope' +require_relative '../uri_template/rfc6570/expression' module Hyperclient # Internal: The Link is used to let a Resource interact with the API. diff --git a/lib/uri_template/rfc6570/expression.rb b/lib/uri_template/rfc6570/expression.rb new file mode 100644 index 0000000..1e20351 --- /dev/null +++ b/lib/uri_template/rfc6570/expression.rb @@ -0,0 +1,24 @@ +require 'uri_template/rfc6570/expression' + +class URITemplate::RFC6570 + class Expression < Token + def expand(vars) + unused_vars = vars.keys.dup + result = [] + @variable_specs.each do |var, expand, max_length| + if Utils.def?(vars[var]) + result.push(*expand_one(var, vars[var], expand, max_length)) + end + unused_vars.delete(var) + end + unused_vars.each do |k| + result.push(*expand_one(k, vars[k], false, 0)) + end + if result.any? + return (self.class::PREFIX + result.join(self.class::SEPARATOR)) + else + return '' + end + end + end +end