Class: Amazon::WebServices::Util::MockTransport

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amazon/webservices/util/mock_transport.rb

Defined Under Namespace

Classes: MethodCall

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ MockTransport

Returns a new instance of MockTransport.



23
24
25
26
27
28
29
# File 'lib/amazon/webservices/util/mock_transport.rb', line 23

def initialize( args={} )
  @call_buffer = []
  @index = 0
  @listener = nil
  @mock_reply = args[:MockReply] || { :MockResult => { :Mock => true, :Request => {} }, :OperationRequest => {} }
  @listener = args[:MockListener] if args[:MockListener].is_a? Proc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/amazon/webservices/util/mock_transport.rb', line 55

def method_missing(method,*args)
  raise "only support one parameter" unless args.size <= 1
  the_method = MethodCall.new( method, args[0] )
  @call_buffer << the_method
  listen_result = @listener.call( the_method ) unless @listener.nil?
  response = @mock_reply.dup
  response[:MockResult][:Request] = the_method.request unless response[:MockResult].nil?
  response.merge!(listen_result) if listen_result.is_a? Hash
  response
end

Instance Attribute Details

#call_bufferObject (readonly)

Returns the value of attribute call_buffer.



31
32
33
# File 'lib/amazon/webservices/util/mock_transport.rb', line 31

def call_buffer
  @call_buffer
end

#mock_replyObject

Returns the value of attribute mock_reply.



32
33
34
# File 'lib/amazon/webservices/util/mock_transport.rb', line 32

def mock_reply
  @mock_reply
end

Instance Method Details

#each(&block) ⇒ Object

:yields: method_call



46
47
48
49
# File 'lib/amazon/webservices/util/mock_transport.rb', line 46

def each(&block) # :yields: method_call
  @call_buffer[@index..-1].each { |item| yield item }
#    yield self.next until @index >= @call_buffer.size
end

#flushObject



34
35
36
37
# File 'lib/amazon/webservices/util/mock_transport.rb', line 34

def flush
  @call_buffer = []
  @index = 0
end

#listen(&block) ⇒ Object

:yields: method_call



51
52
53
# File 'lib/amazon/webservices/util/mock_transport.rb', line 51

def listen(&block) # :yields: method_call
  @listener = block
end

#nextObject



39
40
41
42
43
44
# File 'lib/amazon/webservices/util/mock_transport.rb', line 39

def next
  return nil if @index >= @call_buffer.size
  ret = @call_buffer[@index]
  @index += 1
  ret
end