Sha256: f5e50856950097cf81895d31895becaea43cddd8f2ff62c032ed3860331f4429

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 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?

        self
      end

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

      def create_proxy
        Client.from url
      end

      def stop
        return unless @process.alive?

        begin
          @process.poll_for_exit(5)
        rescue ChildProcess::TimeoutError
          @process.stop
        end
      rescue Errno::ECHILD
        # already dead
      ensure
        @process = nil
      end

      private


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

  end # Proxy
end # BrowserMob

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
browsermob-proxy-0.0.2 lib/browsermob/proxy/server.rb
browsermob-proxy-0.0.1 lib/browsermob/proxy/server.rb