Class: GCR::Cassette
- Inherits:
-
Object
- Object
- GCR::Cassette
- Defined in:
- lib/gcr/cassette.rb
Constant Summary collapse
- VERSION =
2
Instance Attribute Summary collapse
-
#reqs ⇒ Object
readonly
Returns the value of attribute reqs.
Class Method Summary collapse
-
.delete_all ⇒ Object
Delete all recorded cassettes.
Instance Method Summary collapse
- #[](req) ⇒ Object
- #[]=(req, resp) ⇒ Object
-
#exist? ⇒ Boolean
Does this cassette exist?.
-
#initialize(name) ⇒ Cassette
constructor
Initialize a new cassette.
-
#load ⇒ Object
Load this cassette.
-
#play(&blk) ⇒ Object
Play recorded GRPC responses.
-
#record(&blk) ⇒ Object
Record all GRPC calls made while calling the provided block.
-
#save ⇒ Object
Persist this cassette.
- #start_playing ⇒ Object
- #start_recording ⇒ Object
- #stop_playing ⇒ Object
- #stop_recording ⇒ Object
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
#reqs ⇒ Object (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_all ⇒ Object
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.
28 29 30 |
# File 'lib/gcr/cassette.rb', line 28 def exist? File.exist?(@path) end |
#load ⇒ Object
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) blk.call ensure 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 |
#save ⇒ Object
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_playing ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gcr/cassette.rb', line 101 def 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 ⇒ Object
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 |