Sha256: 9722bc603d33e5da5c857a8a30c17dd13d24ad8c09a25cf2610a528b0bf0a124

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require 'cgi'
require 'uri'
require 'eventmachine'

module Billy
  class Proxy
    def initialize
      reset
      @cache = Billy::Cache.new
    end

    def start(threaded = true)
      if threaded
        Thread.new { main_loop }
        sleep(0.01) while @signature.nil?
      else
        main_loop
      end
    end

    def url
      "http://#{host}:#{port}"
    end

    def host
      'localhost'
    end

    def port
      Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
    end

    def call(method, url, headers, body)
      stub = find_stub(method, url)
      unless stub.nil?
        query_string = URI.parse(url).query || ""
        params = CGI.parse(query_string)
        stub.call(params, headers, body)
      end
    end

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

    def reset
      @stubs = []
    end

    def reset_cache
      @cache.reset
    end

    def restore_cache
      @cache.reset
      @cache.load_dir
    end

    protected

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

    def main_loop
      EM.run do
        EM.error_handler do |e|
          puts e.class.name, e
          puts e.backtrace.join("\n")
        end

        @signature = EM.start_server('127.0.0.1', 0, ProxyConnection) do |p|
          p.handler = self
          p.cache = @cache
        end

        Billy.log(:info, "Proxy listening on #{url}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
puffing-billy-0.2.1 lib/billy/proxy.rb
puffing-billy-0.2.0 lib/billy/proxy.rb