Class: ShamRack::StubWebService

Inherits:
Object
  • Object
show all
Defined in:
lib/sham_rack/stub_web_service.rb

Overview

A simple Rack app that stubs out a web service, for testing.

Instance Method Summary collapse

Constructor Details

#initializeStubWebService

Returns a new instance of StubWebService.



8
9
10
# File 'lib/sham_rack/stub_web_service.rb', line 8

def initialize
  @handlers = []
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/sham_rack/stub_web_service.rb', line 16

def call(env)
  @request = Rack::Request.new(env)
  @handlers.each do |handler|
    response = handler.call(@request)
    return response if response
  end
  return default_response
end

#handle(&block) ⇒ Object



25
26
27
# File 'lib/sham_rack/stub_web_service.rb', line 25

def handle(&block)
  @handlers.unshift(block)
end

#last_requestObject



12
13
14
# File 'lib/sham_rack/stub_web_service.rb', line 12

def last_request
  @request
end

#register_resource(path, content, content_type = "application/xml", status = 200) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/sham_rack/stub_web_service.rb', line 29

def register_resource(path, content, content_type = "application/xml", status = 200)
  handle do |request|
    request_path = request.path_info
    unless request.query_string.to_s.empty?
      request_path += "?" + request.query_string
    end
    [status, {"Content-Type" => content_type}, [content]] if request_path == path
  end
end

#resetObject



39
40
41
# File 'lib/sham_rack/stub_web_service.rb', line 39

def reset
  @handlers.clear
end