class GCR::Cassette

Constants

VERSION

Attributes

reqs[R]

Public Class Methods

delete_all() click to toggle source

Delete all recorded cassettes.

Returns nothing.

# File lib/gcr/cassette.rb, line 9
def self.delete_all
  Dir[File.join(GCR.cassette_dir, "*.json")].each do |path|
    File.unlink(path)
  end
end
new(name) click to toggle source

Initialize a new cassette.

name - The String name of the recording, from which the path is derived.

Returns nothing.

# File lib/gcr/cassette.rb, line 20
def initialize(name)
  @path = File.join(GCR.cassette_dir, "#{name}.json")
  @reqs = []
end

Public Instance Methods

[](req) click to toggle source
# File lib/gcr/cassette.rb, line 123
def [](req)
  reqs.find { |r| r == req }
end
[]=(req, resp) click to toggle source
# File lib/gcr/cassette.rb, line 127
def []=(req, resp)
  reqs << [req, resp]
end
exist?() click to toggle source

Does this cassette exist?

Returns boolean.

# File lib/gcr/cassette.rb, line 28
def exist?
  File.exist?(@path)
end
load() click to toggle source

Load this cassette.

Returns nothing.

# File lib/gcr/cassette.rb, line 35
def load
  data = JSON.parse(File.read(@path))

  if data["version"] != VERSION
    raise "GCR cassette version #{data["version"]} not supported"
  end

  @reqs = data["reqs"].map do |req, resp|
    [GCR::Request.from_hash(req), GCR::Response.from_hash(resp)]
  end
end
play(&blk) click to toggle source

Play recorded GRPC responses.

Returns nothing.

# File lib/gcr/cassette.rb, line 72
def play(&blk)
  start_playing
  blk.call
ensure
  stop_playing
end
record(&blk) click to toggle source

Record all GRPC calls made while calling the provided block.

Returns nothing.

# File lib/gcr/cassette.rb, line 62
def record(&blk)
  start_recording
  blk.call
ensure
  stop_recording
end
request_response(*args) click to toggle source
# File lib/gcr/cassette.rb, line 83
def request_response(*args)
  orig_request_response(*args).tap do |resp|
    req = GCR::Request.from_proto(*args)
    if GCR.cassette.reqs.none? { |r, _| r == req }
      GCR.cassette.reqs << [req, GCR::Response.from_proto(resp)]
    end
  end
end
save() click to toggle source

Persist this cassette.

Returns nothing.

# File lib/gcr/cassette.rb, line 50
def save
  File.open(@path, "w") do |f|
    f.write(JSON.pretty_generate(
      "version" => VERSION,
      "reqs"    => reqs,
    ))
  end
end
start_playing() click to toggle source
# File lib/gcr/cassette.rb, line 101
def start_playing
  load

  GCR.stub.class.class_eval do
    alias_method :orig_request_response, :request_response

    def request_response(*args)
      req = GCR::Request.from_proto(*args)
      GCR.cassette.reqs.each do |other_req, resp|
        return resp.to_proto if req == other_req
      end
      raise GCR::NoRecording
    end
  end
end
start_recording() click to toggle source
# File lib/gcr/cassette.rb, line 79
def start_recording
  GCR.stub.class.class_eval do
    alias_method :orig_request_response, :request_response

    def request_response(*args)
      orig_request_response(*args).tap do |resp|
        req = GCR::Request.from_proto(*args)
        if GCR.cassette.reqs.none? { |r, _| r == req }
          GCR.cassette.reqs << [req, GCR::Response.from_proto(resp)]
        end
      end
    end
  end
end
stop_playing() click to toggle source
# File lib/gcr/cassette.rb, line 117
def stop_playing
  GCR.stub.class.class_eval do
    alias_method :request_response, :orig_request_response
  end
end
stop_recording() click to toggle source
# File lib/gcr/cassette.rb, line 94
def stop_recording
  GCR.stub.class.class_eval do
    alias_method :request_response, :orig_request_response
  end
  save
end