Class: RackStubs::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_stubs/middleware.rb

Defined Under Namespace

Classes: PathSpecification

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
9
# File 'lib/rack_stubs/middleware.rb', line 6

def initialize(app)
  @app = app
  @stubs = {}
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rack_stubs/middleware.rb', line 11

def call(env)
  request = Rack::Request.new(env)
  if request.path =~ /\/rack_stubs\/clear$/
    clear_all!
    ok
  elsif request.path =~ /\/rack_stubs\/list$/
    [200, {"Content-Type" => "text/plain"}, [@stubs.to_json]]
  elsif request.content_type == "application/json+rack-stub"
    @stubs[request.path] = JSON.parse(request.body.read.to_s)
    ok
  elsif @stubs.include?(request.path) &&
        @stubs[request.path].include?(request.request_method)
    @stubs[request.path][request.request_method]
  else
    @app.call(env)
  end
end

#clear_all!Object



37
38
39
# File 'lib/rack_stubs/middleware.rb', line 37

def clear_all!
  @stubs = {}
end

#get(path) ⇒ Object



29
30
31
# File 'lib/rack_stubs/middleware.rb', line 29

def get(path)
  PathSpecification.new(@stubs, path, 'GET')
end

#post(path) ⇒ Object



33
34
35
# File 'lib/rack_stubs/middleware.rb', line 33

def post(path)
  PathSpecification.new(@stubs, path, 'POST')
end