Class: MiniMock

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

Constant Summary collapse

RECORDINGS_PATH =
'mini_mock'
DEFAULT_FILE =
"mocks"
AFTER_REQUEST_CALLBACK =
Proc.new do |response|
  code = <<~RUBY
    response = Typhoeus::Response.new(
      code: #{response.code},
      status_message: "#{response.status_message}",
      body: #{response.body.inspect},
      headers: #{response.headers},
      effective_url: #{response.effective_url.inspect},
      options: #{response.options.tap{|a| a[:debug_info] = ""}.inspect}
    )
    Typhoeus.stub(#{response.request.base_url.inspect}, #{response.request.original_options})
    .and_return(response)

  RUBY
  File.write "#{RECORDINGS_PATH}/#{@@file_name}.rb", code, mode: 'a+'
end

Class Method Summary collapse

Class Method Details

.offObject



22
23
24
25
26
27
# File 'lib/mini_mock.rb', line 22

def off
  Typhoeus::Config.block_connection = false
  Typhoeus::Expectation.clear
  remove_callback
  true
end

.record(file_name = DEFAULT_FILE) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mini_mock.rb', line 7

def record file_name=DEFAULT_FILE
  FileUtils.mkdir_p(RECORDINGS_PATH)
  @@file_name = file_name
  Typhoeus::Config.block_connection = false
  add_callback
  true
end

.replay(file_name = DEFAULT_FILE) ⇒ Object



15
16
17
18
19
20
# File 'lib/mini_mock.rb', line 15

def replay file_name=DEFAULT_FILE
  Typhoeus::Config.block_connection = true
  remove_callback
  load "#{RECORDINGS_PATH}/#{file_name}.rb"
  true
end