Sha256: 3d8f4f203e38ccbe55125ee7d7b704a4caeb090ff2ede4bd9652fefd4cb09fab

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'net/http'
require 'yaml'

require 'netrecording/version'
require 'netrecording/http'
require 'netrecording/http_header'
require 'fakeweb'

module Netrecording
  @@records = []
  @@recording = false
  @@configured = false

  def self.records
    @@records
  end

  def self.start_recording!
    _configure
    @@recording = true
  end

  def self.stop_recording!
    @@recording = false
  end

  def self.recording?
    !! @@recording
  end

  def self.save!(file_path = nil)
    unless file_path
      file_path = File.join(Rails.root, 'tmp', 'netrecording', Time.current.strftime("%Y-%m-%d_%H%M%S.yml"))
      FileUtils.mkdir_p(File.dirname file_path) unless File.exist?(File.dirname file_path)
    end

    File.open(file_path, 'w') do |fp|
      fp.puts YAML.dump(@@records)
    end
    @@records = []

    file_path
  end

  def self.clear_records!
    @@records = []
  end

  def self._configure
    return if @@configured
    Net::HTTP.extend(Netrecording::NetHTTP)
    Net::HTTPHeader.extend(Netrecording::NetHTTPHeader)
    @@configured = true
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
netrecording-0.1.2 lib/netrecording.rb