Sha256: 0752b9fbdfa8b69da60786bad8529c78a548c496411f7f19967b130252b3e26d

Contents?: true

Size: 1.1 KB

Versions: 63

Compression:

Stored size: 1.1 KB

Contents

require 'resourceful/resource'
  
module Resourceful
  class StubbedResourceProxy
    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

    def get_body(*args)
      get(*args).body
    end
    
    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

    def method_missing(method, *args)
      @resource.send(method, *args)
    end
    
    protected
    
    def canned_response(mime_type)
      mime_type = @canned_responses.keys.first if mime_type == '*/*'
      @canned_responses[mime_type]
    end
    
  end
end

Version data entries

63 entries across 63 versions & 6 rubygems

Version Path
paul-resourceful-0.2.3 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.3.0 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.3.1 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.5.0 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.5.3 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.5.4 lib/resourceful/stubbed_resource_proxy.rb
paul-resourceful-0.6.0 lib/resourceful/stubbed_resource_proxy.rb
pezra-resourceful-0.5.4 lib/resourceful/stubbed_resource_proxy.rb
pezra-resourceful-0.6.0 lib/resourceful/stubbed_resource_proxy.rb
pezra-resourceful-0.7.0 lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.6.4 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.6.3 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.6.2 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.6.1 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.6.0 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.5.9 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.5.8 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.5.3 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
abiquo-etk-0.4.42 vendor/resourceful-1.0.1/lib/resourceful/stubbed_resource_proxy.rb
almodovar-0.5.6 vendor/resourceful-0.5.3-patched/lib/resourceful/stubbed_resource_proxy.rb