Sha256: dadb40c021ec3a73876cd172c710859c3890dc7be735b5397e96b2d873e290fb

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

require "tcr/cassette"
require "tcr/configuration"
require "tcr/errors"
require "tcr/recordable_tcp_socket"
require "tcr/version"
require "socket"
require "json"


module TCR
  extend self

  def configure
    yield configuration
  end

  def configuration
    @configuration ||= Configuration.new
  end

  def cassette
    @cassette
  end

  def cassette=(v)
    @cassette = v
  end

  def disabled
    @disabled || false
  end

  def disabled=(v)
    @disabled = v
  end

  def save_session
  end

  def use_cassette(name, options = {}, &block)
    raise ArgumentError, "`TCR.use_cassette` requires a block." unless block
    TCR.cassette = Cassette.new(name)
    yield
    TCR.cassette.save
    TCR.cassette.check_hits_all_sessions if options[:hit_all] || configuration.hit_all
  ensure
    TCR.cassette = nil
  end

  def turned_off(&block)
    raise ArgumentError, "`TCR.turned_off` requires a block." unless block
    current_hook_tcp_ports = configuration.hook_tcp_ports
    configuration.hook_tcp_ports = []
    yield
    configuration.hook_tcp_ports = current_hook_tcp_ports
  end
end


# The monkey patch shim
class TCPSocket
  class << self
    alias_method :real_open,  :open

    def open(address, port, *args)
      if TCR.configuration.hook_tcp_ports.include?(port)
        TCR::RecordableTCPSocket.new(address, port, TCR.cassette)
      else
        real_open(address, port)
      end
    end
  end
end

class OpenSSL::SSL::SSLSocket
  class << self
    def new(io, *args)
      if TCR::RecordableTCPSocket === io
        TCR::RecordableSSLSocket.new(io)
      else
        super
      end
    end
  end
end

class Socket
  class << self
    alias_method :real_tcp, :tcp

    def tcp(host, port, socket_opts)
      if TCR.configuration.hook_tcp_ports.include?(port)
        TCR::RecordableTCPSocket.new(host, port, TCR.cassette)
      else
        real_tcp(host, port, socket_opts)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tcr-0.2.1 lib/tcr.rb
tcr-0.2.0 lib/tcr.rb
tcr-0.1.4 lib/tcr.rb
tcr-0.1.3 lib/tcr.rb