Class: TShield::RequestVCR

Inherits:
Request
  • Object
show all
Defined in:
lib/tshield/request_vcr.rb

Overview

Module to write and read saved responses

Instance Attribute Summary

Attributes inherited from Request

#configuration

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ RequestVCR

Returns a new instance of RequestVCR.



17
18
19
20
21
22
23
24
25
26
# File 'lib/tshield/request_vcr.rb', line 17

def initialize(path, options = {})
  super()
  @path = path
  @options = options

  request_configuration = configuration.request
  @options[:timeout] = request_configuration['timeout']
  @options[:verify] = request_configuration['verify_ssl']
  request
end

Instance Method Details

#requestObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tshield/request_vcr.rb', line 28

def request
  raw_query = @options[:raw_query]
  @path = "#{@path}?#{raw_query}" unless !raw_query || raw_query.empty?

  @url = "#{domain}#{@path}"

  if exists
    response.original = false
    response
  else
    configuration.get_before_filters(domain).each do |filter|
      _method, @url, @options = filter.new.filter(method, @url, @options)
    end

    raw = HTTParty.send(method.to_s, @url, @options)

    configuration.get_after_filters(domain).each do |filter|
      raw = filter.new.filter(raw)
    end

    original_response = save(raw)
    original_response.original = true
    original_response
  end
end

#responseObject



54
55
56
57
58
# File 'lib/tshield/request_vcr.rb', line 54

def response
  @response ||= TShield::Response.new(saved_content['body'],
                                      saved_content['headers'] || [],
                                      saved_content['status'] || 200)
end