From a0162423df94e077ebf3efa5520403aa86f6c38c 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. --- .rubocop_todo.yml | 16 +++++++++++----- lib/hyperclient/link.rb | 1 + lib/uri_template/rfc6570/expression.rb | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 lib/uri_template/rfc6570/expression.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a928c0e..8d616aa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,11 +1,15 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2015-08-15 17:01:12 -0400 using RuboCop version 0.33.0. +# on 2015-08-15 17:12:38 -0400 using RuboCop version 0.33.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +# Offense count: 1 +Metrics/AbcSize: + Max: 19 + # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: @@ -16,10 +20,10 @@ Metrics/ClassLength: Metrics/LineLength: Max: 142 -# Offense count: 3 +# Offense count: 4 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 14 + Max: 16 # Offense count: 3 # Configuration parameters: CountComments. @@ -31,14 +35,15 @@ Style/AsciiComments: Exclude: - 'lib/hyperclient/collection.rb' -# Offense count: 2 +# Offense count: 3 # Configuration parameters: EnforcedStyle, SupportedStyles. Style/ClassAndModuleChildren: Exclude: - 'features/steps/api_navigation.rb' - 'features/steps/default_config.rb' + - 'lib/uri_template/rfc6570/expression.rb' -# Offense count: 14 +# Offense count: 15 Style/Documentation: Exclude: - 'features/steps/api_navigation.rb' @@ -46,6 +51,7 @@ Style/Documentation: - 'features/support/api.rb' - 'features/support/fixtures.rb' - 'lib/hyperclient/version.rb' + - 'lib/uri_template/rfc6570/expression.rb' - 'test/faraday/connection_test.rb' - 'test/hyperclient/attributes_test.rb' - 'test/hyperclient/collection_test.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