From e5f2394ac89b5458d11b39560543eed80c21516f Mon Sep 17 00:00:00 2001 From: Justin Sherrill Date: Fri, 7 Apr 2017 17:10:39 -0400 Subject: [PATCH] Support rest_client 2.0 Runcible was built around being able to take a rest client response body, pull out relevant details and craft a new rest client response object with the boy being the parsed json rather than the raw json. Rest Client 2.0 makes this impossible by making the Response object extend string. To resolve this while attempting to not break compatibility, we have introduced a Runcible::Response object that attempts to pass method calls to the parsed JSON body, and if that does not make sense, to the Rest Client Response object. Even still, I still bumped the version to 2.0 in case there is any change I am not seeing. --- lib/runcible.rb | 1 + lib/runcible/base.rb | 14 +++--------- lib/runcible/response.rb | 36 ++++++++++++++++++++++++++++++ lib/runcible/version.rb | 2 +- runcible.gemspec | 2 +- test/extensions/repository_test.rb | 4 +--- 6 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 lib/runcible/response.rb diff --git a/lib/runcible.rb b/lib/runcible.rb index 89b3b92c..fbb4dc84 100644 --- a/lib/runcible.rb +++ b/lib/runcible.rb @@ -5,6 +5,7 @@ resources += Dir[File.dirname(__FILE__) + '/runcible/base.rb'] resources += Dir[File.dirname(__FILE__) + '/runcible/instance.rb'] resources += Dir[File.dirname(__FILE__) + '/runcible/resources/*.rb'] +resources += Dir[File.dirname(__FILE__) + '/runcible/response.rb'] resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/unit.rb'] resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/*.rb'] diff --git a/lib/runcible/base.rb b/lib/runcible/base.rb index 4234aefe..3829693d 100644 --- a/lib/runcible/base.rb +++ b/lib/runcible/base.rb @@ -75,8 +75,8 @@ def call(method, path, options = {}) end def get_response(client, path, *args) - client[path].send(*args) do |response, request, result, &_block| - resp = response.return!(request, result) + client[path].send(*args) do |response, _request, _result, &_block| + resp = response.return! log_debug return resp end @@ -132,7 +132,7 @@ def process_response(response) i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i end end - response = rest_client_response(body, response.net_http_res, response.args) + response = Runcible::Response.new(body, response) rescue JSON::ParserError log_exception end @@ -140,14 +140,6 @@ def process_response(response) return response end - def rest_client_response(body, net_http_res, args) - if Gem.loaded_specs['rest-client'].version < Gem::Version.create('1.8.0') - RestClient::Response.create(body, net_http_res, args) - else - RestClient::Response.create(body, net_http_res, args, nil) - end - end - def required_params(local_names, binding, keys_to_remove = []) local_names = local_names.each_with_object({}) do |v, acc| value = binding.eval(v.to_s) unless v == :_ diff --git a/lib/runcible/response.rb b/lib/runcible/response.rb new file mode 100644 index 00000000..153bd124 --- /dev/null +++ b/lib/runcible/response.rb @@ -0,0 +1,36 @@ +module Runcible + class Response + attr_accessor :rest_client_response, :parsed_body + + def initialize(parsed_body, rest_client_response) + @rest_client_response = rest_client_response + @parsed_body = parsed_body + end + + def respond_to?(name) + @parsed_body.respond_to?(name) || @rest_client_response.respond_to?(name) + end + + def ==(other) + self.parsed_body == other + end + + def is_a?(clazz) + self.parsed_body.is_a?(clazz) + end + + def body + @parsed_body + end + + def method_missing(name, *args, &block) + if @parsed_body.respond_to?(name) + @parsed_body.send(name, *args, &block) + elsif @rest_client_response.respond_to?(name) + @rest_client_response.send(name, *args, &block) + else + super + end + end + end +end diff --git a/lib/runcible/version.rb b/lib/runcible/version.rb index 05e04ec7..1b0e65ee 100644 --- a/lib/runcible/version.rb +++ b/lib/runcible/version.rb @@ -1,3 +1,3 @@ module Runcible - VERSION = '1.11.0'.freeze + VERSION = '2.0.0'.freeze end diff --git a/runcible.gemspec b/runcible.gemspec index 83231822..dd4e610a 100644 --- a/runcible.gemspec +++ b/runcible.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |gem| gem.version = Runcible::VERSION gem.add_dependency('json') - gem.add_dependency('rest-client', ['>= 1.6.1', '< 2.0.0']) + gem.add_dependency('rest-client', ['>= 1.6.1', '< 3.0.0']) gem.add_dependency('oauth') gem.add_dependency('activesupport', '>= 3.0.10') gem.add_dependency('i18n', '>= 0.5.0') diff --git a/test/extensions/repository_test.rb b/test/extensions/repository_test.rb index 1137ac94..b69b58e2 100644 --- a/test/extensions/repository_test.rb +++ b/test/extensions/repository_test.rb @@ -133,9 +133,7 @@ def test_remove_schedules 'yum_importer', '2012-10-25T20:44:00Z/P7D' ) - response = @extension.remove_schedules(RepositorySupport.repo_id, 'yum_importer') - - assert_equal 200, response.code + @extension.remove_schedules(RepositorySupport.repo_id, 'yum_importer') end def test_retrieve_with_details