Class: Restapi::Extractor::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/extractor/recorder.rb

Defined Under Namespace

Modules: FunctionalTestRecording Classes: Middleware

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



4
5
6
# File 'lib/restapi/extractor/recorder.rb', line 4

def initialize
  @ignored_params = [:controller, :action]
end

Instance Method Details

#analyse_controller(controller) ⇒ Object



19
20
21
22
# File 'lib/restapi/extractor/recorder.rb', line 19

def analyse_controller(controller)
  @controller = controller.class
  @action = controller.params[:action]
end

#analyse_env(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/restapi/extractor/recorder.rb', line 8

def analyse_env(env)
  @verb = env["REQUEST_METHOD"].to_sym
  @path = env["PATH_INFO"].sub(/^\/*/,"/")
  @query = env["QUERY_STRING"] unless env["QUERY_STRING"].blank?
  @params = Rack::Utils.parse_nested_query(@query)
  @params.merge!(env["action_dispatch.request.request_parameters"] || {})
  if data = parse_data(env["rack.input"].read)
    @request_data = data
  end
end

#analyse_response(response) ⇒ Object



24
25
26
27
28
29
# File 'lib/restapi/extractor/recorder.rb', line 24

def analyse_response(response)
  if response.last.respond_to?(:body) && data = parse_data(response.last.body)
    @response_data = data
  end
  @code = response.first
end

#analyze_functional_test(test_context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/restapi/extractor/recorder.rb', line 31

def analyze_functional_test(test_context)
  request, response = test_context.request, test_context.response
  @verb = request.request_method.to_sym
  @path = request.path
  @params = request.request_parameters
  if [:POST, :PUT].include?(@verb)
    @request_data = @params
  else
    @query = request.query_string
  end
  @response_data = parse_data(response.body)
  @code = response.code
end

#parse_data(data) ⇒ Object



45
46
47
48
49
50
# File 'lib/restapi/extractor/recorder.rb', line 45

def parse_data(data)
  return nil if data.to_s =~ /^\s*$/
  JSON.parse(data)
rescue Exception => e
  data
end

#recordObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/restapi/extractor/recorder.rb', line 64

def record
  if @controller
    {:controller => @controller,
     :action => @action,
     :verb => @verb,
     :path => @path,
     :params => @params,
     :query => @query,
     :request_data => @request_data,
     :response_data => @response_data,
     :code => @code}
  else
    nil
  end
end

#reformat_data(data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/restapi/extractor/recorder.rb', line 52

def reformat_data(data)
  parsed = parse_data(data)
  case parsed
  when nil
    nil
  when String
    parsed
  else
    JSON.pretty_generate().gsub(/: \[\s*\]/,": []").gsub(/\{\s*\}/,"{}")
  end
end