Skip to content

Commit e5f2394

Browse files
committed
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.
1 parent ee18d55 commit e5f2394

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

lib/runcible.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
resources += Dir[File.dirname(__FILE__) + '/runcible/base.rb']
66
resources += Dir[File.dirname(__FILE__) + '/runcible/instance.rb']
77
resources += Dir[File.dirname(__FILE__) + '/runcible/resources/*.rb']
8+
resources += Dir[File.dirname(__FILE__) + '/runcible/response.rb']
89

910
resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/unit.rb']
1011
resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/*.rb']

lib/runcible/base.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def call(method, path, options = {})
7575
end
7676

7777
def get_response(client, path, *args)
78-
client[path].send(*args) do |response, request, result, &_block|
79-
resp = response.return!(request, result)
78+
client[path].send(*args) do |response, _request, _result, &_block|
79+
resp = response.return!
8080
log_debug
8181
return resp
8282
end
@@ -132,22 +132,14 @@ def process_response(response)
132132
i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i
133133
end
134134
end
135-
response = rest_client_response(body, response.net_http_res, response.args)
135+
response = Runcible::Response.new(body, response)
136136
rescue JSON::ParserError
137137
log_exception
138138
end
139139

140140
return response
141141
end
142142

143-
def rest_client_response(body, net_http_res, args)
144-
if Gem.loaded_specs['rest-client'].version < Gem::Version.create('1.8.0')
145-
RestClient::Response.create(body, net_http_res, args)
146-
else
147-
RestClient::Response.create(body, net_http_res, args, nil)
148-
end
149-
end
150-
151143
def required_params(local_names, binding, keys_to_remove = [])
152144
local_names = local_names.each_with_object({}) do |v, acc|
153145
value = binding.eval(v.to_s) unless v == :_

lib/runcible/response.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Runcible
2+
class Response
3+
attr_accessor :rest_client_response, :parsed_body
4+
5+
def initialize(parsed_body, rest_client_response)
6+
@rest_client_response = rest_client_response
7+
@parsed_body = parsed_body
8+
end
9+
10+
def respond_to?(name)
11+
@parsed_body.respond_to?(name) || @rest_client_response.respond_to?(name)
12+
end
13+
14+
def ==(other)
15+
self.parsed_body == other
16+
end
17+
18+
def is_a?(clazz)
19+
self.parsed_body.is_a?(clazz)
20+
end
21+
22+
def body
23+
@parsed_body
24+
end
25+
26+
def method_missing(name, *args, &block)
27+
if @parsed_body.respond_to?(name)
28+
@parsed_body.send(name, *args, &block)
29+
elsif @rest_client_response.respond_to?(name)
30+
@rest_client_response.send(name, *args, &block)
31+
else
32+
super
33+
end
34+
end
35+
end
36+
end

lib/runcible/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Runcible
2-
VERSION = '1.11.0'.freeze
2+
VERSION = '2.0.0'.freeze
33
end

runcible.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
1616
gem.version = Runcible::VERSION
1717

1818
gem.add_dependency('json')
19-
gem.add_dependency('rest-client', ['>= 1.6.1', '< 2.0.0'])
19+
gem.add_dependency('rest-client', ['>= 1.6.1', '< 3.0.0'])
2020
gem.add_dependency('oauth')
2121
gem.add_dependency('activesupport', '>= 3.0.10')
2222
gem.add_dependency('i18n', '>= 0.5.0')

test/extensions/repository_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ def test_remove_schedules
133133
'yum_importer',
134134
'2012-10-25T20:44:00Z/P7D'
135135
)
136-
response = @extension.remove_schedules(RepositorySupport.repo_id, 'yum_importer')
137-
138-
assert_equal 200, response.code
136+
@extension.remove_schedules(RepositorySupport.repo_id, 'yum_importer')
139137
end
140138

141139
def test_retrieve_with_details

0 commit comments

Comments
 (0)