Class: GCR::Cassette

Inherits:
Object
  • Object
show all
Defined in:
lib/gcr/cassette.rb

Constant Summary collapse

VERSION =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cassette

Initialize a new cassette.

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

Returns nothing.



20
21
22
23
# File 'lib/gcr/cassette.rb', line 20

def initialize(name)
  @path = File.join(GCR.cassette_dir, "#{name}.json")
  @reqs = []
end

Instance Attribute Details

#reqsObject (readonly)

Returns the value of attribute reqs.



4
5
6
# File 'lib/gcr/cassette.rb', line 4

def reqs
  @reqs
end

Class Method Details

.delete_allObject

Delete all recorded cassettes.

Returns nothing.



9
10
11
12
13
# 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

Instance Method Details

#[](req) ⇒ Object



123
124
125
# File 'lib/gcr/cassette.rb', line 123

def [](req)
  reqs.find { |r| r == req }
end

#[]=(req, resp) ⇒ Object



127
128
129
# File 'lib/gcr/cassette.rb', line 127

def []=(req, resp)
  reqs << [req, resp]
end

#exist?Boolean

Does this cassette exist?

Returns boolean.

Returns:

  • (Boolean)


28
29
30
# File 'lib/gcr/cassette.rb', line 28

def exist?
  File.exist?(@path)
end

#loadObject

Load this cassette.

Returns nothing.



35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object

Play recorded GRPC responses.

Returns nothing.



72
73
74
75
76
77
# File 'lib/gcr/cassette.rb', line 72

def play(&blk)
  start_playing
  blk.call
ensure
  stop_playing
end

#record(&blk) ⇒ Object

Record all GRPC calls made while calling the provided block.

Returns nothing.



62
63
64
65
66
67
# File 'lib/gcr/cassette.rb', line 62

def record(&blk)
  start_recording
  blk.call
ensure
  stop_recording
end

#saveObject

Persist this cassette.

Returns nothing.



50
51
52
53
54
55
56
57
# 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_playingObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_recordingObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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_playingObject



117
118
119
120
121
# 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_recordingObject



94
95
96
97
98
99
# 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