Skip to content

Commit

Permalink
Support rest_client 2.0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jlsherrill committed Apr 25, 2017
1 parent ee18d55 commit e5f2394
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/runcible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
14 changes: 3 additions & 11 deletions lib/runcible/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,22 +132,14 @@ 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

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 == :_
Expand Down
36 changes: 36 additions & 0 deletions lib/runcible/response.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/runcible/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Runcible
VERSION = '1.11.0'.freeze
VERSION = '2.0.0'.freeze
end
2 changes: 1 addition & 1 deletion runcible.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 1 addition & 3 deletions test/extensions/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5f2394

Please sign in to comment.