Class: Qeweney::MockAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/qeweney/mock_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_body) ⇒ MockAdapter

Returns a new instance of MockAdapter.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/qeweney/mock_adapter.rb', line 21

def initialize(request_body)
  case request_body
  when Array
    @request_body_chunks = request_body
  when nil
    @request_body_chunks = []
  else
    @request_body_chunks = [request_body]
  end
  @calls = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



43
44
45
# File 'lib/qeweney/mock_adapter.rb', line 43

def method_missing(sym, *args)
  calls << [sym, *args]
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/qeweney/mock_adapter.rb', line 5

def body
  @body
end

#callsObject (readonly)

Returns the value of attribute calls.



5
6
7
# File 'lib/qeweney/mock_adapter.rb', line 5

def calls
  @calls
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/qeweney/mock_adapter.rb', line 5

def headers
  @headers
end

Class Method Details

.mock(headers = {}, request_body = nil) ⇒ Object



47
48
49
50
51
# File 'lib/qeweney/mock_adapter.rb', line 47

def self.mock(headers = {}, request_body = nil)
  headers[':method'] ||= ''
  headers[':path'] ||= ''
  Request.new(headers, new(request_body))
end

Instance Method Details

#complete?(_req) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/qeweney/mock_adapter.rb', line 17

def complete?(_req)
  @request_body_chunks.empty?
end

#get_body(_req) ⇒ Object



11
12
13
14
15
# File 'lib/qeweney/mock_adapter.rb', line 11

def get_body(_req)
  body = @request_body_chunks.join('')
  @request_body_chunks.clear
  body
end

#get_body_chunk(_req, _buffered_only) ⇒ Object



7
8
9
# File 'lib/qeweney/mock_adapter.rb', line 7

def get_body_chunk(_req, _buffered_only)
  @request_body_chunks.shift
end

#respond(req, body, headers) ⇒ Object



33
34
35
36
37
# File 'lib/qeweney/mock_adapter.rb', line 33

def respond(req, body, headers)
  @calls << [:respond, req, body, headers]
  @body = body
  @headers = headers
end

#statusObject



39
40
41
# File 'lib/qeweney/mock_adapter.rb', line 39

def status
  headers[':status'] || Qeweney::Status::OK
end