Sha256: 2a991d6e434beee44efaf25f6f064a4bf18a96aecaa0bf133a85644ca7cae0d7

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

require 'billy/handlers/handler'
require 'addressable/uri'

module Billy
  class StubHandler
    include Handler

    def handle_request(method, url, headers, body)
      if handles_request?(method, url, headers, body)
        if (stub = find_stub(method, url))
          query_string = Addressable::URI.parse(url).query || ''
          params = CGI.parse(query_string)
          stub.call(method, url, params, headers, body).tap do |response|
            Billy.log(:info, "puffing-billy: STUB #{method} for '#{url}'")
            return { status: response[0], headers: response[1], content: response[2] }
          end
        end
      end

      nil
    end

    def handles_request?(method, url, _headers, _body)
      !find_stub(method, url).nil?
    end

    def reset
      self.stubs = []
    end

    def stub(url, options = {})
      new_stub = ProxyRequestStub.new(url, options)
      stubs.unshift new_stub
      new_stub
    end

    private

    attr_writer :stubs

    def stubs
      @stubs ||= []
    end

    def find_stub(method, url)
      stubs.find { |stub| stub.matches?(method, url) }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
puffing-billy-0.12.0 lib/billy/handlers/stub_handler.rb
puffing-billy-0.11.1 lib/billy/handlers/stub_handler.rb
puffing-billy-0.11.0 lib/billy/handlers/stub_handler.rb
puffing-billy-0.10.1 lib/billy/handlers/stub_handler.rb
puffing-billy-0.10.0 lib/billy/handlers/stub_handler.rb
puffing-billy-0.9.2 lib/billy/handlers/stub_handler.rb
puffing-billy-0.9.1 lib/billy/handlers/stub_handler.rb
puffing-billy-0.9.0 lib/billy/handlers/stub_handler.rb
puffing-billy-0.8.0 lib/billy/handlers/stub_handler.rb