Module: Rackrack::Stub

Defined in:
lib/rackrack/stub.rb

Defined Under Namespace

Classes: NoStubException

Class Method Summary collapse

Class Method Details

.build(&block) ⇒ Object



8
9
10
11
12
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
# File 'lib/rackrack/stub.rb', line 8

def build(&block)
  Class.new do
    class << self
      def stub(&block)
        if block_given?
          @stub = Class.new(Sinatra::Base) do
            instance_eval(&block)
            error 404 do
              raise Rackrack::Stub::NoStubException
            end
          end
        else
          @stub ||= raise Rackrack::Stub::NoStubException
        end
      end

      def reset!
        @stub = nil
      end
    end

    def initialize(app)
      @app = app
    end

    def call(env)
      begin
        self.class.stub.call(env)
      rescue Rackrack::Stub::NoStubException
        @app.call(env)
      end
    end
  end
end