Project

General

Profile

Revision e5f2394a

Added by Justin Sherrill over 5 years ago

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.

View differences:

lib/runcible.rb
5 5
resources += Dir[File.dirname(__FILE__) + '/runcible/base.rb']
6 6
resources += Dir[File.dirname(__FILE__) + '/runcible/instance.rb']
7 7
resources += Dir[File.dirname(__FILE__) + '/runcible/resources/*.rb']
8
resources += Dir[File.dirname(__FILE__) + '/runcible/response.rb']
8 9

  
9 10
resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/unit.rb']
10 11
resources += Dir[File.dirname(__FILE__) + '/runcible/extensions/*.rb']
lib/runcible/base.rb
75 75
    end
76 76

  
77 77
    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!
80 80
        log_debug
81 81
        return resp
82 82
      end
......
132 132
            i.respond_to?(:with_indifferent_access) ? i.with_indifferent_access : i
133 133
          end
134 134
        end
135
        response = rest_client_response(body, response.net_http_res, response.args)
135
        response = Runcible::Response.new(body, response)
136 136
      rescue JSON::ParserError
137 137
        log_exception
138 138
      end
......
140 140
      return response
141 141
    end
142 142

  
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

  
151 143
    def required_params(local_names, binding, keys_to_remove = [])
152 144
      local_names = local_names.each_with_object({}) do |v, acc|
153 145
        value = binding.eval(v.to_s) unless v == :_
lib/runcible/response.rb
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
1 1
module Runcible
2
  VERSION = '1.11.0'.freeze
2
  VERSION = '2.0.0'.freeze
3 3
end
runcible.gemspec
16 16
  gem.version       = Runcible::VERSION
17 17

  
18 18
  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'])
20 20
  gem.add_dependency('oauth')
21 21
  gem.add_dependency('activesupport', '>= 3.0.10')
22 22
  gem.add_dependency('i18n', '>= 0.5.0')
test/extensions/repository_test.rb
133 133
        'yum_importer',
134 134
        '2012-10-25T20:44:00Z/P7D'
135 135
      )
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')
139 137
    end
140 138

  
141 139
    def test_retrieve_with_details

Also available in: Unified diff