Sha256: 47bcecd8fd4a666446ad31c4748461ebcccbeb0f8fbe70cab634a2b1df3fd204

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'childprocess'
require 'socket'

module BrowserMob
  module Proxy

    class Server
      attr_reader :port

      def initialize(path, opts = {})
        unless File.exist?(path)
          raise Errno::ENOENT, path
        end

        unless File.executable?(path)
          raise Errno::EACCES, "not executable: #{path}"
        end

        @path = path
        @port = Integer(opts[:port] || 8080)

        @process = ChildProcess.new(path, "--port", port.to_s)
        @process.io.inherit! if opts[:log]
      end

      def start
        @process.start
        sleep 0.1 until listening?

        pid = Process.pid
        at_exit { stop if Process.pid == pid }

        self
      end

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

      def create_proxy
        Client.from url
      end

      def stop
        @process.stop if @process.alive?
      end

      private


      def listening?
        TCPSocket.new("localhost", port).close
        true
      rescue
        false
      end
    end # Server

  end # Proxy
end # BrowserMob

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
browsermob-proxy-0.0.6 lib/browsermob/proxy/server.rb
browsermob-proxy-0.0.5 lib/browsermob/proxy/server.rb
browsermob-proxy-0.0.4 lib/browsermob/proxy/server.rb
browsermob-proxy-0.0.3 lib/browsermob/proxy/server.rb