Class: Resourceful::StubbedResourceProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/stubbed_resource_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource, canned_responses) ⇒ StubbedResourceProxy

Returns a new instance of StubbedResourceProxy.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/resourceful/stubbed_resource_proxy.rb', line 5

def initialize(resource, canned_responses)
  @resource = resource
  
  @canned_responses = {}
  
  canned_responses.each do |cr| 
    mime_type = cr[:mime_type]
    @canned_responses[mime_type] = resp = Net::HTTPOK.new('1.1', '200', 'OK')
    resp['content-type'] = mime_type.to_str
    resp.instance_variable_set(:@read, true)
    resp.instance_variable_set(:@body, cr[:body])

  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



35
36
37
# File 'lib/resourceful/stubbed_resource_proxy.rb', line 35

def method_missing(method, *args)
  @resource.send(method, *args)
end

Instance Method Details

#get(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/resourceful/stubbed_resource_proxy.rb', line 24

def get(*args)
  options = args.last.is_a?(Hash) ? args.last : {}
  
  if accept = [(options[:accept] || '*/*')].flatten.compact
    accept.each do |mt|
      return canned_response(mt) || next
    end
    @resource.get(*args)
   end
end

#get_body(*args) ⇒ Object



20
21
22
# File 'lib/resourceful/stubbed_resource_proxy.rb', line 20

def get_body(*args)
  get(*args).body
end