Class: GiantClient::MockAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/giant_client/mock_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#delete, #encode_query, #get, #head, #normalize_header, #normalize_header_hash, #post, #prepend_question_mark, #put, #stringify_query, #url_from_opts

Constructor Details

#initializeMockAdapter

Returns a new instance of MockAdapter.



8
9
10
11
# File 'lib/giant_client/mock_adapter.rb', line 8

def initialize
  @requests = []
  @responses = []
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



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

def requests
  @requests
end

#responsesObject

Returns the value of attribute responses.



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

def responses
  @responses
end

Instance Method Details

#last_requestObject



48
49
50
# File 'lib/giant_client/mock_adapter.rb', line 48

def last_request
  @requests[0]
end

#last_responseObject



52
53
54
# File 'lib/giant_client/mock_adapter.rb', line 52

def last_response
  @responses[0]
end

#request(method, opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/giant_client/mock_adapter.rb', line 13

def request(method, opts)
  last_request = MockRequest.new

  if BODYLESS_METHODS.include?(method)
    unless opts[:body] == ''
      last_request.raised_error = true
      last_request.error_type = GiantClient::Error::NotImplemented
    end
  end

  last_request.host = opts[:host]
  last_request.ssl = opts[:ssl]
  last_request.port = opts[:port]
  last_request.path = opts[:path]
  last_request.query = opts[:query]
  last_request.headers = opts[:headers]
  last_request.body = opts[:body]
  last_request.timeout = opts[:timeout]

  last_request.url = url_from_opts(opts)

  last_request.querystring = stringify_query(opts[:query])

  last_response = { :status_code => 200, :headers => {}, :body => nil }

  @requests.unshift(last_request)
  @responses.unshift(last_response)

  self
end

#respond_with(hash) ⇒ Object



44
45
46
# File 'lib/giant_client/mock_adapter.rb', line 44

def respond_with(hash)
  @responses[0] = @responses[0].merge(hash)
end