Module: GCR

Extended by:
GCR
Included in:
GCR
Defined in:
lib/gcr.rb

Defined Under Namespace

Classes: Cassette, Request, Response

Constant Summary collapse

Error =
Class.new(StandardError)
ConfigError =
Class.new(Error)
RunningError =
Class.new(Error)
NoRecording =
Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#cassetteObject



77
78
79
# File 'lib/gcr.rb', line 77

def cassette
  @cassette
end

#cassette_dirObject

Where GCR stores cassettes.

Returns a String path to a directory. Raises ConfigError if not configured.



38
39
40
# File 'lib/gcr.rb', line 38

def cassette_dir
  @cassette_dir || (raise ConfigError, "no cassette dir configured")
end

#cassette_dir=(path) ⇒ Object

Specify where GCR should store cassettes.

path - The String path to a directory.

Returns nothing.

Raises:



30
31
32
33
# File 'lib/gcr.rb', line 30

def cassette_dir=(path)
  raise RunningError, "cannot configure GCR within #with_cassette block" if @running
  @cassette_dir = path
end

#ignore(*fields) ⇒ Object

Ignore these fields when matching requests.

*fields - String field names (eg. “token”).

Returns nothing.



14
15
16
# File 'lib/gcr.rb', line 14

def ignore(*fields)
  ignored_fields.concat(fields.map(&:to_s))
end

#ignored_fieldsObject

Fields that are ignored when matching requests.

Returns an Array of Strings.



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

def ignored_fields
  @ignored_fields ||= []
end

#insert(name) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/gcr.rb', line 59

def insert(name)
  @cassette = Cassette.new(name)
  if @cassette.exist?
    @cassette.start_playing
  else
    @cassette.start_recording
  end
end

#removeObject



68
69
70
71
72
73
74
75
# File 'lib/gcr.rb', line 68

def remove
  if @cassette.exist?
    @cassette.stop_playing
  else
    @cassette.stop_recording
  end
  @cassette = nil
end

#stubObject

The stub that is being mocked.

Returns a A GRPC::ClientStub instance. Raises ConfigError if not configured.



55
56
57
# File 'lib/gcr.rb', line 55

def stub
  @stub || (raise ConfigError, "no cassette dir configured")
end

#stub=(stub) ⇒ Object

Specify the stub to intercept calls to.

stub - A GRPC::ClientStub instance.

Returns nothing.

Raises:



47
48
49
50
# File 'lib/gcr.rb', line 47

def stub=(stub)
  raise RunningError, "cannot configure GCR within #with_cassette block" if @running
  @stub = stub
end

#with_cassette(name, &blk) ⇒ Object

If a cassette with the given name exists, play that cassette for the provided block. Otherwise, record a cassette with the provided block.

Returns nothing.



85
86
87
88
89
90
91
92
93
94
# File 'lib/gcr.rb', line 85

def with_cassette(name, &blk)
  @cassette = Cassette.new(name)
  if @cassette.exist?
    @cassette.play(&blk)
  else
    @cassette.record(&blk)
  end
ensure
  @cassette = nil
end