Class: ResponseMate::Tape

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

Overview

Responsible for the recorded responses as files

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(key) ⇒ Object


45
46
47
48
# File 'lib/response_mate/tape.rb', line 45

def load(key)
  YAML.load_file(File.join(ResponseMate.configuration.output_dir, "#{key}.yml")).
    symbolize_keys
end

Instance Method Details

#write(key, request, response, meta = {}, output_dir = nil) ⇒ Object

Writes the tape as a file


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/response_mate/tape.rb', line 4

def write(key, request, response, meta = {}, output_dir = nil)
  output_dir ||= ResponseMate.configuration.output_dir

  output_path = File.join output_dir, "#{key}.yml"

  File.open(output_path, 'w') do |f|
    file_content = {
      request: request.to_hash.select { |_, v| !v.nil? },
      response: {
        status: response.status,
        headers: _utf8_encode(response.headers.to_hash),
        body: _utf8_encode(response.body)
      },
      created_at: Time.now
    }

    file_content.merge!(meta: meta) if meta.present?

    f << file_content.to_yaml
  end
rescue Errno::ENOENT
  raise ResponseMate::OutputDirError
end